다음을 통해 공유


Thread.CurrentPrincipal 속성

정의

스레드의 현재 보안 주체(역할 기반 보안용)를 가져오거나 설정합니다.

public:
 static property System::Security::Principal::IPrincipal ^ CurrentPrincipal { System::Security::Principal::IPrincipal ^ get(); void set(System::Security::Principal::IPrincipal ^ value); };
public static System.Security.Principal.IPrincipal? CurrentPrincipal { get; set; }
public static System.Security.Principal.IPrincipal CurrentPrincipal { get; set; }
member this.CurrentPrincipal : System.Security.Principal.IPrincipal with get, set
Public Shared Property CurrentPrincipal As IPrincipal

속성 값

IPrincipal

보안 컨텍스트를 나타내는 IPrincipal 값입니다.

예외

호출자에게 보안 주체를 설정하는 데 필요한 사용 권한이 없습니다.

예제

다음 코드 예제에서는 스레드의 보안 주체를 설정 하 고 검색 하는 방법을 보여 줍니다.

using namespace System;
using namespace System::Security;
using namespace System::Security::Permissions;
using namespace System::Security::Principal;
using namespace System::Threading;

int main()
{
   array<String^>^rolesArray = {"managers","executives"};
   try
   {
      
      // Set the principal to a new generic principal.
      Thread::CurrentPrincipal = gcnew GenericPrincipal( gcnew GenericIdentity( "Bob","Passport" ),rolesArray );
   }
   catch ( SecurityException^ secureException ) 
   {
      Console::WriteLine( "{0}: Permission to set Principal "
      "is denied.", secureException->GetType()->Name );
   }

   IPrincipal^ threadPrincipal = Thread::CurrentPrincipal;
   Console::WriteLine( "Name: {0}\nIsAuthenticated: {1}"
   "\nAuthenticationType: {2}", threadPrincipal->Identity->Name, threadPrincipal->Identity->IsAuthenticated.ToString(), threadPrincipal->Identity->AuthenticationType );
}
using System;
using System.Security;
using System.Security.Permissions;
using System.Security.Principal;
using System.Threading;

class Principal
{
    static void Main()
    {
        string[] rolesArray = {"managers", "executives"};
        try
        {
            // Set the principal to a new generic principal.
            Thread.CurrentPrincipal = 
                new GenericPrincipal(new GenericIdentity(
                "Bob", "Passport"), rolesArray);
        }
        catch(SecurityException secureException)
        {
            Console.WriteLine("{0}: Permission to set Principal " +
                "is denied.", secureException.GetType().Name);
        }

        IPrincipal threadPrincipal = Thread.CurrentPrincipal;
        Console.WriteLine("Name: {0}\nIsAuthenticated: {1}" +
            "\nAuthenticationType: {2}", 
            threadPrincipal.Identity.Name, 
            threadPrincipal.Identity.IsAuthenticated,
            threadPrincipal.Identity.AuthenticationType);
    }
}
Option Explicit
Option Strict

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

Public Class Principal

    <MTAThread> _
    Shared Sub Main()
    
        Dim rolesArray As String() = {"managers", "executives"}
        Try
            ' Set the principal to a new generic principal.
            Thread.CurrentPrincipal = _
                New GenericPrincipal(New GenericIdentity( _
                "Bob", "Passport"), rolesArray)

        Catch secureException As SecurityException
            Console.WriteLine("{0}: Permission to set Principal " & _
                "is denied.", secureException.GetType().Name)
        End Try

        Dim threadPrincipal As IPrincipal = Thread.CurrentPrincipal
        Console.WriteLine( _
            "Name: {0}" & vbCrLf & "IsAuthenticated:" & _
            " {1}" & vbCrLf & "AuthenticationType: {2}", _
            threadPrincipal.Identity.Name, _
            threadPrincipal.Identity.IsAuthenticated, _
            threadPrincipal.Identity.AuthenticationType)
    
    End Sub
End Class

적용 대상