Export (0) Print
Expand All

GenericPrincipal Class

Represents a generic principal.

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

[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public class GenericPrincipal : IPrincipal
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public class GenericPrincipal implements IPrincipal
SerializableAttribute 
ComVisibleAttribute(true) 
public class GenericPrincipal implements IPrincipal

This class represents the roles of the current user.

The following example shows the use of members of the GenericPrincipal class.

using System;
using System.Security.Principal;

class GenericPrincipalMembers
{
    [STAThread]
    static void Main(string[] args)
    {
        // Retrieve a GenericPrincipal that is based on the current user's
        // WindowsIdentity.
        GenericPrincipal genericPrincipal = GetGenericPrincipal();

        // Retrieve the generic identity of the GenericPrincipal object.
        GenericIdentity principalIdentity = 
            (GenericIdentity)genericPrincipal.Identity;

        // Display the identity name and authentication type.
        if (principalIdentity.IsAuthenticated)
        {
            Console.WriteLine(principalIdentity.Name);
            Console.WriteLine("Type:"+principalIdentity.AuthenticationType);
        }

        // Verify that the generic principal has been assigned the
        // NetworkUser role.
        if (genericPrincipal.IsInRole("NetworkUser"))
        {
            Console.WriteLine("User belongs to the NetworkUser role.");
        }

        Console.WriteLine("The sample completed successfully; " +
            "press Enter to continue.");
        Console.ReadLine();
    }

    // Create a generic principal based on values from the current
    // WindowsIdentity.
    private static GenericPrincipal GetGenericPrincipal()
    {
        // Use values from the current WindowsIdentity to construct
        // a set of GenericPrincipal roles.
        WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();
        string[] roles = new string[10];
        if (windowsIdentity.IsAuthenticated)
        {
            // Add custom NetworkUser role.
            roles[0] = "NetworkUser";
        }

        if (windowsIdentity.IsGuest)
        {
            // Add custom GuestUser role.
            roles[1] = "GuestUser";
        }

        if (windowsIdentity.IsSystem)
        {
            // Add custom SystemUser role.
            roles[2] = "SystemUser";
        }

        // Construct a GenericIdentity object based on the current Windows
        // identity name and authentication type.
        string authenticationType = windowsIdentity.AuthenticationType;
        string userName = windowsIdentity.Name;
        GenericIdentity genericIdentity =
            new GenericIdentity(userName, authenticationType);

        // Construct a GenericPrincipal object based on the generic identity
        // and custom roles for the user.
        GenericPrincipal genericPrincipal =
            new GenericPrincipal(genericIdentity, roles);

        return genericPrincipal;
    }
}


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

class GenericPrincipalMembers
{
    /** @attribute STAThread()
     */
    public static void main(String[] args)
    {
        // Retrieve a GenericPrincipal that is based on the current user's
        // WindowsIdentity.
        GenericPrincipal genericPrincipal = GetGenericPrincipal();
        // Retrieve the generic identity of the GenericPrincipal object.
        GenericIdentity principalIdentity = (GenericIdentity)
            (genericPrincipal.get_Identity());
        // Display the identity name and authentication type.
        if (principalIdentity.get_IsAuthenticated()) {
            Console.WriteLine(principalIdentity.get_Name());
            Console.WriteLine("Type:" + principalIdentity.
                get_AuthenticationType());
        }
        // Verify that the generic principal has been assigned the
        // NetworkUser role.
        if (genericPrincipal.IsInRole("NetworkUser")) {
            Console.WriteLine("User belongs to the NetworkUser role.");
        }

        Console.WriteLine("The sample completed successfully; " 
            + "press Enter to continue.");
        Console.ReadLine();
    } //main

    // Create a generic principal based on values from the current
    // WindowsIdentity.
    private static GenericPrincipal GetGenericPrincipal()
    {
        // Use values from the current WindowsIdentity to construct
        // a set of GenericPrincipal roles.
        WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();
        String roles[] = new String[10];
        if (windowsIdentity.get_IsAuthenticated()) {
            // Add custom NetworkUser role.
            roles.set_Item(0, "NetworkUser");
        }

        if (windowsIdentity.get_IsGuest()) {
            // Add custom GuestUser role.
            roles.set_Item(1, "GuestUser");
        }

        if (windowsIdentity.get_IsSystem()) {
            // Add custom SystemUser role.
            roles.set_Item(2, "SystemUser");
        }
        // Construct a GenericIdentity object based on the current Windows
        // identity name and authentication type.
        String authenticationType = windowsIdentity.get_AuthenticationType();
        String userName = windowsIdentity.get_Name();
        GenericIdentity genericIdentity = new GenericIdentity(userName, 
            authenticationType);
        // Construct a GenericPrincipal object based on the generic identity
        // and custom roles for the user.
        GenericPrincipal genericPrincipal = new GenericPrincipal(genericIdentity,
            roles);
        return genericPrincipal;
    } //GetGenericPrincipal
} //GenericPrincipalMembers


System.Object
  System.Security.Principal.GenericPrincipal
     System.Web.Security.PassportPrincipal

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

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