Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

AppDomain.SetThreadPrincipal Method

Sets the default principal object to be attached to threads if they attempt to bind to a principal while executing in this application domain.

[Visual Basic]
Public Overridable Sub SetThreadPrincipal( _
   ByVal principal As IPrincipal _
) Implements _AppDomain.SetThreadPrincipal
[C#]
public virtual void SetThreadPrincipal(
 IPrincipal principal
);
[C++]
public: virtual void SetThreadPrincipal(
 IPrincipal* principal
);
[JScript]
public function SetThreadPrincipal(
   principal : IPrincipal
);

Parameters

principal
The principal object to attach to threads.

Implements

_AppDomain.SetThreadPrincipal

Exceptions

Exception Type Condition
ArgumentNullException principal is a null reference (Nothing in Visual Basic).
PolicyException The thread principal has already been set.
AppDomainUnloadedException Operations are attempted on an unloaded application domain.
SecurityException The caller does not have the correct permissions. See the requirements section.

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

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.