Share via


作法:建立 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 物件驗證。 您仍然可以在主體物件上執行角色型驗證,而不需將它附加至執行緒。 如需詳細資訊,請參閱取代 Principal 物件

    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

另請參閱