次の方法で共有


プリンシパル オブジェクトの置き換え

認証サービスを提供するアプリケーションでは、特定のスレッドの プリンシパル オブジェクト (IPrincipal) を置換する必要があります。 さらに、虚偽の ID やロールを要求することにより、悪意をもってアタッチされた不適切な プリンシパル がアプリケーションのセキュリティに問題を生じさせるため、セキュリティ システムを活用して プリンシパル オブジェクトを置き換える機能を保護する必要があります。 そのため、 プリンシパル オブジェクトを置き換える機能を必要とするアプリケーションに、プリンシパルを制御するための System.Security.Permissions.SecurityPermission オブジェクトを付与する必要があります。 (ロール ベースのセキュリティ チェックを実行する、または プリンシパル オブジェクトを作成するために、このアクセス許可は必要がないことに注意してください。)

次のタスクを実行することによって、現在の プリンシパル オブジェクトを置き換えることができます。

  1. 置き換える プリンシパル オブジェクトと関連付けられている ID オブジェクトを作成します。

  2. 新しい プリンシパル オブジェクトを呼び出しコンテキストにアタッチします。

次の例では、汎用プリンシパル オブジェクトを作成し、それを使用してスレッドのプリンシパルを設定する方法を示します。

using System;
using System.Threading;
using System.Security.Permissions;
using System.Security.Principal;

class SecurityPrincipalDemo
{
    public static void Main()
    {
        // Retrieve a GenericPrincipal that is based on the current user's
        // WindowsIdentity.
        GenericPrincipal genericPrincipal = GetGenericPrincipal();

        // Retrieve the generic identity of the GenericPrincipal object.
        GenericIdentity principalIdentity =
            (GenericIdentity)genericPrincipal.Identity;

        // Display the identity name and authentication type.
        if (principalIdentity.IsAuthenticated)
        {
            Console.WriteLine(principalIdentity.Name);
            Console.WriteLine("Type:" + principalIdentity.AuthenticationType);
        }

        // Verify that the generic principal has been assigned the
        // NetworkUser role.
        if (genericPrincipal.IsInRole("NetworkUser"))
        {
            Console.WriteLine("User belongs to the NetworkUser role.");
        }

        Thread.CurrentPrincipal = genericPrincipal;
    }

    // Create a generic principal based on values from the current
    // WindowsIdentity.
    private static GenericPrincipal GetGenericPrincipal()
    {
        // Use values from the current WindowsIdentity to construct
        // a set of GenericPrincipal roles.
        WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();
        string[] roles = new string[10];
        if (windowsIdentity.IsAuthenticated)
        {
            // Add custom NetworkUser role.
            roles[0] = "NetworkUser";
        }

        if (windowsIdentity.IsGuest)
        {
            // Add custom GuestUser role.
            roles[1] = "GuestUser";
        }

        if (windowsIdentity.IsSystem)
        {
            // Add custom SystemUser role.
            roles[2] = "SystemUser";
        }

        // Construct a GenericIdentity object based on the current Windows
        // identity name and authentication type.
        string authenticationType = windowsIdentity.AuthenticationType!;
        string userName = windowsIdentity.Name;
        GenericIdentity genericIdentity =
            new GenericIdentity(userName, authenticationType);

        // Construct a GenericPrincipal object based on the generic identity
        // and custom roles for the user.
        GenericPrincipal genericPrincipal =
            new GenericPrincipal(genericIdentity, roles);

        return genericPrincipal;
    }
}
Imports System.Threading
Imports System.Security.Permissions
Imports System.Security.Principal



Class SecurityPrincipalDemo

    ' Create a generic principal based on values from the current
    ' WindowsIdentity.
    Private Shared Function GetGenericPrincipal() As GenericPrincipal
        ' Use values from the current WindowsIdentity to construct
        ' a set of GenericPrincipal roles.
        Dim windowsIdentity As WindowsIdentity = windowsIdentity.GetCurrent()
        Dim roles(9) As String
        If windowsIdentity.IsAuthenticated Then
            ' Add custom NetworkUser role.
            roles(0) = "NetworkUser"
        End If

        If windowsIdentity.IsGuest Then
            ' Add custom GuestUser role.
            roles(1) = "GuestUser"
        End If

        If windowsIdentity.IsSystem Then
            ' Add custom SystemUser role.
            roles(2) = "SystemUser"
        End If

        ' Construct a GenericIdentity object based on the current Windows
        ' identity name and authentication type.
        Dim authenticationType As String = windowsIdentity.AuthenticationType
        Dim userName As String = windowsIdentity.Name
        Dim genericIdentity As New GenericIdentity(userName, authenticationType)

        ' Construct a GenericPrincipal object based on the generic identity
        ' and custom roles for the user.
        Dim genericPrincipal As New GenericPrincipal(genericIdentity, roles)

        Return genericPrincipal

    End Function 'GetGenericPrincipal

    Public Shared Sub Main()
        ' Retrieve a GenericPrincipal that is based on the current user's
        ' WindowsIdentity.
        Dim genericPrincipal As GenericPrincipal = GetGenericPrincipal()

        ' Retrieve the generic identity of the GenericPrincipal object.
        Dim principalIdentity As GenericIdentity = CType(genericPrincipal.Identity, GenericIdentity)

        ' Display the identity name and authentication type.
        If principalIdentity.IsAuthenticated Then
            Console.WriteLine(principalIdentity.Name)
            Console.WriteLine("Type:" + principalIdentity.AuthenticationType)
        End If

        ' Verify that the generic principal has been assigned the
        ' NetworkUser role.
        If genericPrincipal.IsInRole("NetworkUser") Then
            Console.WriteLine("User belongs to the NetworkUser role.")
        End If

        Thread.CurrentPrincipal = genericPrincipal

    End Sub



End Class

関連項目