次の方法で共有


AppDomain.SetPrincipalPolicy メソッド

アプリケーション ドメインでスレッドを実行中に、スレッドがプリンシパルにバインドしようとした場合に、プリンシパル オブジェクトと ID オブジェクトをそのスレッドに関連付ける方法を指定します。

Public Overridable Sub SetPrincipalPolicy( _
   ByVal policy As PrincipalPolicy _) Implements _AppDomain.SetPrincipalPolicy
[C#]
public virtual void SetPrincipalPolicy(PrincipalPolicypolicy);
[C++]
public: virtual void SetPrincipalPolicy(PrincipalPolicypolicy);
[JScript]
public function SetPrincipalPolicy(
   policy : PrincipalPolicy);

パラメータ

  • policy
    スレッドに関連付けるプリンシパル オブジェクトの型。

実装

_AppDomain.SetPrincipalPolicy

例外

例外の種類 条件
AppDomainUnloadedException 操作が、アンロードされたアプリケーション ドメインで試行されています。
SecurityException 呼び出し元に、正しいアクセス許可がありません。要件のセクションを参照してください。

解説

この値の設定は、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 

[C#] 
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();
        }
    }

[C++] 
#using <mscorlib.dll>

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

__gc class ADPrincipal {
public:
   static void PrintPrincipalInformation() {
      IPrincipal* curPrincipal = Thread::CurrentPrincipal;
      if (curPrincipal != 0) {
         Console::WriteLine(S"Type: {0}", curPrincipal->GetType()->Name);
         Console::WriteLine(S"Name: {0}", curPrincipal->Identity->Name);
         Console::WriteLine(S"Authenticated: {0}", __box(curPrincipal->Identity->IsAuthenticated));
         Console::WriteLine();
      }
   }
};

int main() {
   // Create a new thread with a generic principal.
   Thread* t = new Thread(new ThreadStart(0, 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 = new Thread(new ThreadStart(0, ADPrincipal::PrintPrincipalInformation));
   t->Start();
   t->Join();

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

   // Create a new thread with the principal created above.
   t = new Thread(new ThreadStart(0, ADPrincipal::PrintPrincipalInformation));
   t->Start();
   t->Join();

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

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

.NET Framework セキュリティ:

参照

AppDomain クラス | AppDomain メンバ | System 名前空間