如何:创建 GenericPrincipal 和 GenericIdentity 对象

注意

本文适用于 Windows。

有关 ASP.NET Core 的信息,请参阅 ASP.NET Core 安全概述

你可以将 GenericIdentity 类与 GenericPrincipal 类结合使用,以创建一个独立于 Windows 域而存在的授权方案。

创建 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.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
        ' PrincipalPermission 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
    // PrincipalPermission 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

另请参阅