

























VB.NET:
Imports System.Security.Principal
Public Class JoeyIdentity
Implements IIdentityPublic ReadOnly Property AuthenticationType() As String Implements System.Security.Principal.IIdentity.AuthenticationType
Get
Return "Authenticated by Joey"
End Get
End PropertyPublic ReadOnly Property IsAuthenticated() As Boolean Implements System.Security.Principal.IIdentity.IsAuthenticated
Get
Return True
End Get
End PropertyPublic ReadOnly Property Name() As String Implements System.Security.Principal.IIdentity.Name
Get
Return "Joey"
End Get
End Property
End Class
Imports System.Security.Principal
Public Class JoeyPrincipal
Implements IPrincipal
Private _Identity As IIdentity
Private _Roles() As StringPublic Sub New(ByVal Identify As IIdentity, ByVal Roles As String())
_Identity = Identify
_Roles = Array.CreateInstance(GetType(String), Roles.Length)
Roles.CopyTo(_Roles, 0)
Array.Sort(_Roles)
End SubPublic ReadOnly Property Identity() As System.Security.Principal.IIdentity Implements System.Security.Principal.IPrincipal.Identity
Get
Return _Identity
End Get
End PropertyPublic Function IsInRole(ByVal role As String) As Boolean Implements System.Security.Principal.IPrincipal.IsInRole
Return Array.BinarySearch(_Roles, role) >= 0
End Function
End Class
Imports System.Threading
Imports System.Security.PermissionsModule Module1Sub Main()
Dim jIdentity As New JoeyIdentity
Dim roles As String() = {"Developer"}
Dim jPrincipal As New JoeyPrincipal(jIdentity, roles)
Thread.CurrentPrincipal = jPrincipal
Try
TestDeveloper()
TestSucker()
Catch ex As Exception
Console.WriteLine(ex.GetType.ToString + " caused by " + Thread.CurrentPrincipal.Identity.Name)
End Try
Try
Dim pp As New PrincipalPermission("Joey", "Developer")
pp.Demand()
Console.WriteLine(Thread.CurrentPrincipal.Identity.Name + " is indeed a developer.")
pp = New PrincipalPermission("Joey", "Sucker")
pp.Demand()
Catch ex As Exception
Console.WriteLine(Thread.CurrentPrincipal.Identity.Name + " is indeed not a sucker.")
End Try
Console.Read()
End Sub<PrincipalPermissionAttribute(SecurityAction.Demand, role:="Developer")> _
Private Sub TestDeveloper()
Console.WriteLine(Thread.CurrentPrincipal.Identity.Name + " is a developer.")
End Sub<PrincipalPermissionAttribute(SecurityAction.Demand, role:="Sucker")> _
Private Sub TestSucker()
Console.WriteLine(Thread.CurrentPrincipal.Identity.Name + " is a sucker.")
End SubEnd Module
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Principal;namespace UserAndDataSecurityCS
{
class JoeyIdentity : IIdentity
{
#region IIdentity Memberspublic string AuthenticationType
{
get
{
return "Authenticated by Joey";
}
}public bool IsAuthenticated
{
get
{
return true;
}
}public string Name
{
get
{
return "Joey";
}
}#endregion
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Principal;namespace UserAndDataSecurityCS
{
class JoeyPrincipal : IPrincipal
{
private IIdentity _Identity;
private string[] _Roles;public JoeyPrincipal(IIdentity Identity, string[] Roles)
{
_Identity = Identity;
_Roles = new string[Roles.Length];
Roles.CopyTo(_Roles, 0);
Array.Sort(_Roles);
}#region IPrincipal Memberspublic IIdentity Identity
{
get
{
return _Identity;
}
}public bool IsInRole(string role)
{
return Array.BinarySearch<string>(_Roles, role) >= 0;
}#endregion
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Permissions;
using System.Threading;namespace UserAndDataSecurityCS
{
class Program
{
static void Main(string[] args)
{
JoeyIdentity jIdentify = new JoeyIdentity();
string[] roles = { "Developer" };
JoeyPrincipal jPrincipal = new JoeyPrincipal(jIdentify, roles);
Thread.CurrentPrincipal = jPrincipal;
try
{
TestDeveloper();
TestSucker();
}
catch(Exception ex)
{
Console.WriteLine(ex.GetType().ToString() + " caused by " + Thread.CurrentPrincipal.Identity.Name);
}
try
{
PrincipalPermission pp = new PrincipalPermission("Joey", "Developer");
pp.Demand();
Console.WriteLine(Thread.CurrentPrincipal.Identity.Name + " is indeed a developer.");
pp = new PrincipalPermission("Joey", "Sucker");
pp.Demand();
}
catch
{
Console.WriteLine(Thread.CurrentPrincipal.Identity.Name + " is indeed not a sucker.");
}
Console.Read();
}
[PrincipalPermissionAttribute(SecurityAction.Demand, Role
= "Developer")][PrincipalPermissionAttribute(SecurityAction.Demand, Role
= "Sucker")]此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。