AppDomain.SetThreadPrincipal(IPrincipal) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
如果執行緒在這個應用程式定義域中執行時嘗試繫結至預設的主體,則設定預設的主體物件附加至執行緒。
public:
void SetThreadPrincipal(System::Security::Principal::IPrincipal ^ principal);
public:
virtual void SetThreadPrincipal(System::Security::Principal::IPrincipal ^ principal);
public void SetThreadPrincipal (System.Security.Principal.IPrincipal principal);
member this.SetThreadPrincipal : System.Security.Principal.IPrincipal -> unit
abstract member SetThreadPrincipal : System.Security.Principal.IPrincipal -> unit
override this.SetThreadPrincipal : System.Security.Principal.IPrincipal -> unit
Public Sub SetThreadPrincipal (principal As IPrincipal)
參數
- principal
- IPrincipal
要附加至執行緒的主體物件。
實作
例外狀況
principal
為 null
。
主要執行緒已經設定。
嘗試對卸載的應用程式定義域執行作業。
範例
下列範例顯示使用 SetThreadPrincipal 方法來變更主體,這些主體可用於附加至在應用程式域中執行的執行緒。 它也會顯示使用 SetPrincipalPolicy 方法變更應用程式域主體原則之 執行緒的影響。
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();
}
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();
}
}
}
open System
open System.Security.Principal
open System.Threading
let printPrincipalInformation () =
let curPrincipal = Thread.CurrentPrincipal
if curPrincipal <> null then
printfn $"Type: {curPrincipal.GetType().Name}"
printfn $"Name: {curPrincipal.Identity.Name}"
printfn $"Authenticated: {curPrincipal.Identity.IsAuthenticated}\n"
[<EntryPoint>]
let main _ =
// Create a new thread with a generic principal.
let t = Thread(ThreadStart printPrincipalInformation)
t.Start()
t.Join()
// Set the principal policy to WindowsPrincipal.
let currentDomain = AppDomain.CurrentDomain
currentDomain.SetPrincipalPolicy PrincipalPolicy.WindowsPrincipal
// The new thread will have a Windows principal representing the
// current user.
let t = Thread(ThreadStart printPrincipalInformation)
t.Start()
t.Join()
// Create a principal to use for new threads.
let identity = GenericIdentity "NewUser"
let principal = GenericPrincipal(identity, null)
currentDomain.SetThreadPrincipal principal
// Create a new thread with the principal created above.
let t = Thread(ThreadStart printPrincipalInformation)
t.Start()
t.Join()
// Wait for user input before terminating.
Console.ReadLine() |> ignore
0
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
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
End Class