Export (0) Print
Expand All

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)

public void SetThreadPrincipal (
	IPrincipal principal
)
public final void SetThreadPrincipal (
	IPrincipal principal
)
public final function SetThreadPrincipal (
	principal : IPrincipal
)

Parameters

principal

The principal object to attach to threads.

Exception typeCondition

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.

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 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft