AppDomain.SetPrincipalPolicy(PrincipalPolicy) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
指定在此应用程序域中执行时如果线程尝试绑定到用户,用户和标识对象应如何附加到该线程。
public:
void SetPrincipalPolicy(System::Security::Principal::PrincipalPolicy policy);
public:
virtual void SetPrincipalPolicy(System::Security::Principal::PrincipalPolicy policy);
public void SetPrincipalPolicy (System.Security.Principal.PrincipalPolicy policy);
member this.SetPrincipalPolicy : System.Security.Principal.PrincipalPolicy -> unit
abstract member SetPrincipalPolicy : System.Security.Principal.PrincipalPolicy -> unit
override this.SetPrincipalPolicy : System.Security.Principal.PrincipalPolicy -> unit
Public Sub SetPrincipalPolicy (policy As PrincipalPolicy)
参数
- policy
- PrincipalPolicy
PrincipalPolicy 值之一,指定要附加到线程的主体对象类型。
实现
例外
在卸载的应用程序域上尝试该操作。
示例
以下示例演示了使用 SetPrincipalPolicy 方法更改应用程序域的主体策略对线程的影响。 它还显示了使用 SetThreadPrincipal 方法更改可用于附加到应用程序域中线程的主体的效果。
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
注解
只有在使用 Thread.CurrentPrincipal 属性之前设置此值时,设置此值才有效。 例如,如果将 设置为 Thread.CurrentPrincipal 给定主体 (泛型主体) 然后使用 SetPrincipalPolicy 方法将 设置为 PrincipalPolicyWindowsPrincipal,则当前主体仍为泛型主体。