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.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
<SecurityPermissionAttribute(SecurityAction.Demand, Flags := SecurityPermissionFlag.ControlPrincipal)> _
Public Sub SetThreadPrincipal ( _
	principal As IPrincipal _
)

Parameters

principal
Type: System.Security.Principal.IPrincipal

The principal object to attach to threads.

Implements

_AppDomain.SetThreadPrincipal(IPrincipal)

ExceptionCondition
ArgumentNullException

principal is Nothing.

PolicyException

The thread principal has already been set.

AppDomainUnloadedException

The operation is attempted on an unloaded application domain.

The following example shows the effect of using the SetThreadPrincipal method to change the principal that is available for attaching to threads that are executing in the application domain. It also shows the effect on threads of using the SetPrincipalPolicy method to change the principal policy of the application domain.

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 

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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