如何:创建 GenericPrincipal 和 GenericIdentity 对象

更新:2007 年 11 月

可将 GenericIdentity 类和 GenericPrincipal 类结合起来使用,以创建独立于 Windows NT 或 Windows 2000 域的授权方案。

创建 GenericPrincipal 对象

  1. 创建标识类的一个新实例,并用希望它持有的名称对其进行初始化。以下代码创建一个新的 GenericIdentity 对象,并用名称 MyUser 对其进行初始化。

    Dim MyIdentity As New GenericIdentity("MyUser")
    
    GenericIdentity MyIdentity = new GenericIdentity("MyUser");
    
  2. 创建 GenericPrincipal 类的一个新实例,并用先前创建的 GenericIdentity 对象和表示希望与此主体关联的角色的字符串数组对其进行初始化。下面的代码示例指定表示一个管理员角色和一个用户角色的字符串数组。然后用前面的 GenericIdentity 和该字符串数组对 GenericPrincipal 进行初始化。

    Dim MyStringArray As String() = {"Manager", "Teller"}
    DIm MyPrincipal As New GenericPrincipal(MyIdentity, MyStringArray)
    
    String[] MyStringArray = {"Manager", "Teller"};
    GenericPrincipal MyPrincipal = new GenericPrincipal(MyIdentity, MyStringArray);
    
  3. 使用以下代码将主体附加到当前线程中。这在以下几种情形中很有用:必须对主体进行多次验证,必须通过应用程序中运行的其他代码对主体进行验证,或必须由 PrincipalPermission 对象对主题进行验证。不将主体附加到线程中,仍可对主体对象执行基于角色的验证。有关更多信息,请参见替换主体对象

    Thread.CurrentPrincipal = MyPrincipal
    
    Thread.CurrentPrincipal = MyPrincipal;
    

示例

下面的代码示例说明如何创建 GenericPrincipalGenericIdentity 的实例。此代码将这些对象的值显示到控制台。

Imports System
Imports System.Security.Principal
Imports System.Threading

Public Class Class1
    
    Public Shared Sub Main()
        ' Create generic identity.
        Dim MyIdentity As New GenericIdentity("MyIdentity")
        
        ' Create generic principal.
        Dim MyStringArray As String() =  {"Manager", "Teller"}
        Dim MyPrincipal As New GenericPrincipal(MyIdentity, MyStringArray)
        
        ' Attach the principal to the current thread.
        ' This is not required unless repeated validation must occur,
        ' other code in your application must validate, or the 
        ' PrincipalPermisson object is used. 
        Thread.CurrentPrincipal = MyPrincipal
        
        ' Print values to the console.
        Dim Name As String = MyPrincipal.Identity.Name
        Dim Auth As Boolean = MyPrincipal.Identity.IsAuthenticated
        Dim IsInRole As Boolean = MyPrincipal.IsInRole("Manager")
        
        Console.WriteLine("The Name is: {0}", Name)
        Console.WriteLine("The IsAuthenticated is: {0}", Auth)
        Console.WriteLine("Is this a Manager? {0}", IsInRole)
        
    End Sub
    
End Class
using System;
using System.Security.Principal;
using System.Threading;

public class Class1
{
    public static int Main(string[] args)
    {
    // Create generic identity.
    GenericIdentity MyIdentity = new GenericIdentity("MyIdentity");

    // Create generic principal.
    String[] MyStringArray = {"Manager", "Teller"};
    GenericPrincipal MyPrincipal = 
        new GenericPrincipal(MyIdentity, MyStringArray);

    // Attach the principal to the current thread.
    // This is not required unless repeated validation must occur,
    // other code in your application must validate, or the 
    // PrincipalPermisson object is used. 
    Thread.CurrentPrincipal = MyPrincipal;

    // Print values to the console.
    String Name =  MyPrincipal.Identity.Name;
    bool Auth =  MyPrincipal.Identity.IsAuthenticated; 
    bool IsInRole =  MyPrincipal.IsInRole("Manager");

    Console.WriteLine("The Name is: {0}", Name);
    Console.WriteLine("The IsAuthenticated is: {0}", Auth);
    Console.WriteLine("Is this a Manager? {0}", IsInRole);

    return 0;
    }
}

执行时,应用程序显示类似于下面这样的输出。

The Name is: MyIdentity
The IsAuthenticated is: True
Is this a Manager? True

请参见

概念

替换 Principal 对象

主体和标识对象

参考

GenericIdentity

GenericPrincipal

PrincipalPermission