次の方法で共有


方法: GenericPrincipal オブジェクトと GenericIdentity オブジェクトを作成する

注意

この記事は Windows に適用されます。

ASP.NET Core の詳細については、「ASP.NET Core セキュリティの概要」を参照してください。

Windows ドメインから独立して存在する認可スキームを作成するには、GenericIdentity クラスと GenericPrincipal クラスを組み合わせて使用できます。

GenericPrincipal オブジェクトを作成するには

  1. ID クラスの新しいインスタンスを作成し、インスタンスに付ける名前で初期化します。 新しい GenericIdentity オブジェクトを作成し、名前 MyUser で初期化するコードを次に示します。

    Dim myIdentity As New GenericIdentity("MyUser")
    
    GenericIdentity myIdentity = new GenericIdentity("MyUser");
    
  2. GenericPrincipal クラスの新しいインスタンスを作成し、前に作成した GenericIdentity オブジェクトと、プリンシパルに関連付けるロールを表す文字列の配列で、このインスタンスを初期化します。 管理者のロールとユーザーのロールを表す文字列の配列を指定するコード例を次に示します。 GenericPrincipal は、前の GenericIdentity と文字列配列で初期化されます。

    Dim myStringArray As String() = {"Manager", "Teller"}
    DIm myPrincipal As New GenericPrincipal(myIdentity, myStringArray)
    
    String[] myStringArray = {"Manager", "Teller"};
    GenericPrincipal myPrincipal = new GenericPrincipal(myIdentity, myStringArray);
    
  3. 次のコードを使用して、プリンシパルを現在のスレッドに結合します。 この方法が便利なのは、プリンシパルが 2 回以上検証される必要がある場合、アプリケーション内で実行されている他のコードによって検証される必要がある場合、または 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

関連項目