Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

AppDomain.SetPrincipalPolicy Method

Specifies how principal and identity objects should be attached to a thread if the thread attempts to bind to a principal while executing in this application domain.

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

Parameters

policy
The type of the principal object to attach to threads.

Implements

_AppDomain.SetPrincipalPolicy

Exceptions

Exception Type Condition
AppDomainUnloadedException Operations are attempted on an unloaded application domain.
SecurityException The caller does not have the correct permissions. See the requirements section.

Remarks

Setting this value will only be effective if you set it before using Thread.CurrentPrincipal. For example, if you set Thread.CurrentPrincipal to a given principal (for example, a generic principal) and then set the PrincipalPolicy using SetPrincipalPolicy to WindowsPrincipal, the current principal will remain the generic principal.

Example

[Visual Basic] 
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] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

AppDomain Class | AppDomain Members | System Namespace

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft