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 : ClaimsPrincipal

The GenericPrincipal type exposes the following members.

  NameDescription
Public methodGenericPrincipalInitializes a new instance of the GenericPrincipal class from a user identity and an array of role names to which the user represented by that identity belongs.
Top

  NameDescription
Public propertyClaimsGets a collection that contains all of the claims from all of the claims identities associated with this claims principal. (Inherited from ClaimsPrincipal.)
Public propertyIdentitiesGets a collection that contains all of the claims identities associated with this claims principal. (Inherited from ClaimsPrincipal.)
Public propertyIdentityGets the GenericIdentity of the user represented by the current GenericPrincipal. (Overrides ClaimsPrincipal.Identity.)
Top

  NameDescription
Public methodAddIdentitiesAdds the specified claims identities to this claims principal. (Inherited from ClaimsPrincipal.)
Public methodAddIdentityAdds the specified claims identity to this claims principal. (Inherited from ClaimsPrincipal.)
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodFindAll(Predicate<Claim>)Retrieves all of the claims that are matched by the specified predicate. (Inherited from ClaimsPrincipal.)
Public methodFindAll(String)Retrieves all or the claims that have the specified claim type. (Inherited from ClaimsPrincipal.)
Public methodFindFirst(Predicate<Claim>)Retrieves the first claim that is matched by the specified predicate. (Inherited from ClaimsPrincipal.)
Public methodFindFirst(String)Retrieves the first claim with the specified claim type. (Inherited from ClaimsPrincipal.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Protected methodGetObjectDataPopulates the SerializationInfo with data needed to serialize the current ClaimsPrincipal object. (Inherited from ClaimsPrincipal.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodHasClaim(Predicate<Claim>)Determines whether any of the claims identities associated with this claims principal contains a claim that is matched by the specified predicate. (Inherited from ClaimsPrincipal.)
Public methodHasClaim(String, String)Determines whether any of the claims identities associated with this claims principal contains a claim with the specified claim type and value. (Inherited from ClaimsPrincipal.)
Public methodIsInRoleDetermines whether the current GenericPrincipal belongs to the specified role. (Overrides ClaimsPrincipal.IsInRole(String).)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

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

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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