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.
This topic has not yet been rated - Rate this topic

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.

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

public void SetPrincipalPolicy (
	PrincipalPolicy policy
)
public final void SetPrincipalPolicy (
	PrincipalPolicy policy
)
public final function SetPrincipalPolicy (
	policy : PrincipalPolicy
)
Not applicable.

Parameters

policy

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

Exception typeCondition

AppDomainUnloadedException

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.

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();
		}
	}
}

import System.*;
import System.Security.Principal.*;
import System.Threading.*;

class ADPrincipal
{
    public static void main(String[] args)
    {
        // Create a new thread with a generic principal.
        System.Threading.Thread t = new System.Threading.Thread(
            new ThreadStart(PrintPrincipalInformation));
        t.Start();
        t.Join();

        // Set the principal policy to WindowsPrincipal.
        AppDomain currentDomain = AppDomain.get_CurrentDomain();
        currentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);

        // The new thread will have a Windows principal representing the
        // current user.
        t = new System.Threading.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 System.Threading.Thread(
            new ThreadStart(PrintPrincipalInformation));
        t.Start();
        t.Join();

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

    static void PrintPrincipalInformation()
    {
        IPrincipal curPrincipal = 
            System.Threading.Thread.get_CurrentPrincipal();

        if (curPrincipal != null) {
            Console.WriteLine("Type: " + curPrincipal.GetType().get_Name());
            Console.WriteLine("Name: " 
                + curPrincipal.get_Identity().get_Name());
            Console.WriteLine("Authenticated: " 
                + (System.Boolean)curPrincipal.get_Identity().
                get_IsAuthenticated());
            Console.WriteLine();
        }
    } //PrintPrincipalInformation

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft. All rights reserved.