Dieser Artikel wurde noch nicht bewertet - Dieses Thema bewerten.

Thread.CurrentPrincipal-Eigenschaft

Ruft den aktuellen Principal des Threads (für rollenbasierte Sicherheit) ab oder legt diesen fest.

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

public static IPrincipal CurrentPrincipal { get; set; }
/** @property */
public static IPrincipal get_CurrentPrincipal ()

/** @property */
public static void set_CurrentPrincipal (IPrincipal value)

public static function get CurrentPrincipal () : IPrincipal

public static function set CurrentPrincipal (value : IPrincipal)

Eigenschaftenwert

Ein IPrincipal-Wert, der den Sicherheitskontext darstellt.
Ausnahmetyp Bedingung

SecurityException

Der Aufrufer verfügt nicht über die erforderliche Berechtigung zum Festlegen des Prinzipals.

Das folgende Codebeispiel veranschaulicht das Festlegen und Abrufen des Principals eines Threads.

using System;
using System.Security;
using System.Security.Permissions;
using System.Security.Principal;
using System.Threading;

// Request permission to set thread principal.
[assembly: SecurityPermissionAttribute(
    SecurityAction.RequestOptional, ControlPrincipal = true)]
class Principal
{
    static void Main()
    {
        string[] rolesArray = {"managers", "executives"};
        try
        {
            // Set the principal to a new generic principal.
            Thread.CurrentPrincipal = 
                new GenericPrincipal(new GenericIdentity(
                "Bob", "Passport"), rolesArray);
        }
        catch(SecurityException secureException)
        {
            Console.WriteLine("{0}: Permission to set Principal " +
                "is denied.", secureException.GetType().Name);
        }

        IPrincipal threadPrincipal = Thread.CurrentPrincipal;
        Console.WriteLine("Name: {0}\nIsAuthenticated: {1}" +
            "\nAuthenticationType: {2}", 
            threadPrincipal.Identity.Name, 
            threadPrincipal.Identity.IsAuthenticated,
            threadPrincipal.Identity.AuthenticationType);
    }
}

import System.*;
import System.Security.*;
import System.Security.Permissions.*;
import System.Security.Principal.*;
import System.Threading.*;
import System.Threading.Thread;
import System.Security.SecurityManager;

// Request permission to set thread principal.
/** @class.assembly SecurityPermissionAttribute(SecurityAction.RequestOptional,
     ControlPrincipal = true)
 */

class Principal
{
    public static void main(String[] args)
    {
        String rolesArray[] = new String[] { "managers", "executives" };

        try {
            // Set the principal to a new generic principal.
            Thread.set_CurrentPrincipal(new GenericPrincipal
                (new GenericIdentity("Bob", "Passport"), rolesArray));
        }
        catch (SecurityException secureException) {
            Console.WriteLine("{0}: Permission to set Principal " +
                "is denied.", secureException.GetType().get_Name());
        }

        IPrincipal threadPrincipal = Thread.get_CurrentPrincipal();
        Console.WriteLine("Name: {0}\nIsAuthenticated: {1}" +
            "\nAuthenticationType: {2}",
            threadPrincipal.get_Identity().get_Name(),
            String.valueOf(threadPrincipal.get_Identity().get_IsAuthenticated()
            ),threadPrincipal.get_Identity().get_AuthenticationType());
    } //main
} //Principal

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0
Fanden Sie dies hilfreich?
(1500 verbleibende Zeichen)