다음을 통해 공유


AppDomain.SetThreadPrincipal 메서드

이 응용 프로그램 도메인에서 실행되는 동안 스레드가 특정 보안 주체에 바인딩하려는 경우 스레드에 연결되는 기본 Principal 개체를 설정합니다.

네임스페이스: System
어셈블리: mscorlib(mscorlib.dll)

구문

‘선언
Public Sub SetThreadPrincipal ( _
    principal As IPrincipal _
)
‘사용 방법
Dim instance As AppDomain
Dim principal As IPrincipal

instance.SetThreadPrincipal(principal)
public void SetThreadPrincipal (
    IPrincipal principal
)
public:
virtual void SetThreadPrincipal (
    IPrincipal^ principal
) sealed
public final void SetThreadPrincipal (
    IPrincipal principal
)
public final function SetThreadPrincipal (
    principal : IPrincipal
)

매개 변수

  • principal
    스레드에 연결할 Principal 개체입니다.

예외

예외 형식 조건

ArgumentNullException

principal이 Null 참조(Visual Basic의 경우 Nothing)인 경우

PolicyException

스레드 보안 주체가 이미 설정되어 있는 경우

AppDomainUnloadedException

언로드된 응용 프로그램 도메인에서 작업을 시도한 경우

예제

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

Class ADPrincipal
    Overloads Shared Sub Main(ByVal args() As String)
        ' Create a new thread with a generic principal.
        Dim t As New Thread(New ThreadStart(AddressOf PrintPrincipalInformation))
        t.Start()
        t.Join()

        ' Set the principal policy to WindowsPrincipal.
        Dim currentDomain As AppDomain = AppDomain.CurrentDomain
        currentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal)

        ' The new thread will have a Windows principal representing the
        ' current user.
        t = New Thread(New ThreadStart(AddressOf PrintPrincipalInformation))
        t.Start()
        t.Join()

        ' Create a principal to use for new threads.
        Dim identity = New GenericIdentity("NewUser")
        Dim principal = New GenericPrincipal(identity, Nothing)
        currentDomain.SetThreadPrincipal(principal)

        ' Create a new thread with the principal created above.
        t = New Thread(New ThreadStart(AddressOf PrintPrincipalInformation))
        t.Start()
        t.Join()

        ' Wait for user input before terminating.
        Console.ReadLine()
    End Sub 'Main


    Shared Sub PrintPrincipalInformation()
        Dim curPrincipal As IPrincipal = Thread.CurrentPrincipal
        If Not (curPrincipal Is Nothing) Then
            Console.WriteLine("Type: " & CType(curPrincipal, Object).GetType().Name)
            Console.WriteLine("Name: " & curPrincipal.Identity.Name)
            Console.WriteLine("Authenticated: " & curPrincipal.Identity.IsAuthenticated)
            Console.WriteLine()

        End If
    End Sub 'PrintPrincipalInformation
End Class 'ADPrincipal 
using System;
using System.Security.Principal;
using System.Threading;

class ADPrincipal
{
    static void Main(string[] args)
    {
        // Create a new thread with a generic principal.
        Thread t = new Thread(new ThreadStart(PrintPrincipalInformation));
        t.Start();
        t.Join();

        // Set the principal policy to WindowsPrincipal.
        AppDomain currentDomain = AppDomain.CurrentDomain;
        currentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
            
        // The new thread will have a Windows principal representing the
        // current user.
        t = new Thread(new ThreadStart(PrintPrincipalInformation));
        t.Start();
        t.Join();

        // Create a principal to use for new threads.
        IIdentity identity = new GenericIdentity("NewUser");
        IPrincipal principal = new GenericPrincipal(identity, null);
        currentDomain.SetThreadPrincipal(principal);
            
        // Create a new thread with the principal created above.
        t = new Thread(new ThreadStart(PrintPrincipalInformation));
        t.Start();
        t.Join();
        
        // Wait for user input before terminating.
        Console.ReadLine();
    }

    static void PrintPrincipalInformation()
    {
        IPrincipal curPrincipal = Thread.CurrentPrincipal;
        if(curPrincipal != null)
        {
            Console.WriteLine("Type: " + curPrincipal.GetType().Name);
            Console.WriteLine("Name: " + curPrincipal.Identity.Name);
            Console.WriteLine("Authenticated: " +
                curPrincipal.Identity.IsAuthenticated);
            Console.WriteLine();
        }
    }
}
using namespace System;
using namespace System::Security::Principal;
using namespace System::Threading;
ref class ADPrincipal
{
public:
   static void PrintPrincipalInformation()
   {
      IPrincipal^ curPrincipal = Thread::CurrentPrincipal;
      if ( curPrincipal != nullptr )
      {
         Console::WriteLine( "Type: {0}", curPrincipal->GetType()->Name );
         Console::WriteLine( "Name: {0}", curPrincipal->Identity->Name );
         Console::WriteLine( "Authenticated: {0}", curPrincipal->Identity->IsAuthenticated );
         Console::WriteLine();
      }
   }

};

int main()
{
   
   // Create a new thread with a generic principal.
   Thread^ t = gcnew Thread( gcnew ThreadStart( ADPrincipal::PrintPrincipalInformation ) );
   t->Start();
   t->Join();
   
   // Set the principal policy to WindowsPrincipal.
   AppDomain^ currentDomain = AppDomain::CurrentDomain;
   currentDomain->SetPrincipalPolicy( PrincipalPolicy::WindowsPrincipal );
   
   // The new thread will have a Windows principal representing the
   // current user.
   t = gcnew Thread( gcnew ThreadStart( ADPrincipal::PrintPrincipalInformation ) );
   t->Start();
   t->Join();
   
   // Create a principal to use for new threads.
   IIdentity^ identity = gcnew GenericIdentity( "NewUser" );
   IPrincipal^ principal = gcnew GenericPrincipal( identity,nullptr );
   currentDomain->SetThreadPrincipal( principal );
   
   // Create a new thread with the principal created above.
   t = gcnew Thread( gcnew ThreadStart( ADPrincipal::PrintPrincipalInformation ) );
   t->Start();
   t->Join();
   
   // Wait for user input before terminating.
   Console::ReadLine();
}
import System.*;
import System.Security.Principal.*;
import System.Threading.*;

class ADPrincipal
{
    public static void main(String[] args)
    {
        // Create a new thread with a generic principal.
        System.Threading.Thread t = new System.Threading.Thread(
            new ThreadStart(PrintPrincipalInformation));
        t.Start();
        t.Join();

        // Set the principal policy to WindowsPrincipal.
        AppDomain currentDomain = AppDomain.get_CurrentDomain();
        currentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);

        // The new thread will have a Windows principal representing the
        // current user.
        t = new System.Threading.Thread(
            new ThreadStart(PrintPrincipalInformation));
        t.Start();
        t.Join();

        // Create a principal to use for new threads.
        IIdentity identity = new GenericIdentity("NewUser");
        IPrincipal principal = new GenericPrincipal(identity, null);
        currentDomain.SetThreadPrincipal(principal);

        // Create a new thread with the principal created above.
        t = new System.Threading.Thread(
            new ThreadStart(PrintPrincipalInformation));
        t.Start();
        t.Join();

        // Wait for user input before terminating.
        Console.ReadLine();
    } //main

    static void PrintPrincipalInformation()
    {
        IPrincipal curPrincipal = 
            System.Threading.Thread.get_CurrentPrincipal();

        if (curPrincipal != null) {
            Console.WriteLine("Type: " + curPrincipal.GetType().get_Name());
            Console.WriteLine("Name: " 
                + curPrincipal.get_Identity().get_Name());
            Console.WriteLine("Authenticated: " 
                + (System.Boolean)curPrincipal.get_Identity().
                get_IsAuthenticated());
            Console.WriteLine();
        }
    } //PrintPrincipalInformation

.NET Framework 보안

플랫폼

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

참고 항목

참조

AppDomain 클래스
AppDomain 멤버
System 네임스페이스