AppDomain.SetPrincipalPolicy 方法

指定在此应用程序域中执行时如果线程尝试绑定到用户,用户和标识对象应如何附加到该线程。

**命名空间:**System
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Sub SetPrincipalPolicy ( _
    policy As PrincipalPolicy _
)
用法
Dim instance As AppDomain
Dim policy As PrincipalPolicy

instance.SetPrincipalPolicy(policy)
public void SetPrincipalPolicy (
    PrincipalPolicy policy
)
public:
virtual void SetPrincipalPolicy (
    PrincipalPolicy policy
) sealed
public final void SetPrincipalPolicy (
    PrincipalPolicy policy
)
public final function SetPrincipalPolicy (
    policy : PrincipalPolicy
)

参数

  • policy
    PrincipalPolicy 值之一,指定要附加到线程的主体对象类型。

异常

异常类型 条件

AppDomainUnloadedException

试图对已卸载的应用程序域进行操作。

备注

仅当使用 Thread.CurrentPrincipal 属性前设置此值才有效。例如,如果将 Thread.CurrentPrincipal 设置为给定主体(例如,一般主体),然后使用 SetPrincipalPolicy 方法将 PrincipalPolicy 设置为 WindowsPrincipal,则当前主体保持为一般主体。

示例

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 命名空间