AppDomain.SetPrincipalPolicy Method (PrincipalPolicy)


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.

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

<SecurityPermissionAttribute(SecurityAction.Demand, Flags := SecurityPermissionFlag.ControlPrincipal)>
Public Sub SetPrincipalPolicy (
	policy As PrincipalPolicy


Type: System.Security.Principal.PrincipalPolicy

One of the PrincipalPolicy values that specifies the type of the principal object to attach to threads.

Exception Condition

The operation is attempted on an unloaded application domain.

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

The following example shows the effect on threads of using the SetPrincipalPolicy method to change the principal policy of the application domain. It also shows the effect of using the SetThreadPrincipal method to change the principal that is available for attaching to threads in 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))

        ' Set the principal policy to WindowsPrincipal.
        Dim currentDomain As AppDomain = AppDomain.CurrentDomain

        ' The new thread will have a Windows principal representing the
        ' current user.
        t = New Thread(New ThreadStart(AddressOf PrintPrincipalInformation))

        ' Create a principal to use for new threads.
        Dim identity = New GenericIdentity("NewUser")
        Dim principal = New GenericPrincipal(identity, Nothing)

        ' Create a new thread with the principal created above.
        t = New Thread(New ThreadStart(AddressOf PrintPrincipalInformation))

        ' Wait for user input before terminating.
    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)

        End If
    End Sub 'PrintPrincipalInformation
End Class 'ADPrincipal 


for ability to manipulate the principal object. Associated enumeration: SecurityPermissionFlag.ControlPrincipal. Security action: Demand.

.NET Framework
Available since 1.1
Return to top