Lue englanniksi Muokkaa

Jaa


GenericPrincipal Class

Definition

Represents a generic principal.

C#
public class GenericPrincipal : System.Security.Claims.ClaimsPrincipal
C#
[System.Serializable]
public class GenericPrincipal : System.Security.Principal.IPrincipal
C#
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class GenericPrincipal : System.Security.Principal.IPrincipal
C#
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class GenericPrincipal : System.Security.Claims.ClaimsPrincipal
Inheritance
GenericPrincipal
Inheritance
GenericPrincipal
Derived
Attributes
Implements

Examples

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

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

Remarks

This class represents the roles of the current user.

Constructors

GenericPrincipal(IIdentity, String[])

Initializes 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.

Properties

Claims

Gets a collection that contains all of the claims from all of the claims identities associated with this claims principal.

(Inherited from ClaimsPrincipal)
CustomSerializationData

Contains any additional data provided by a derived type. Typically set when calling WriteTo(BinaryWriter, Byte[]).

(Inherited from ClaimsPrincipal)
Identities

Gets a collection that contains all of the claims identities associated with this claims principal.

(Inherited from ClaimsPrincipal)
Identity

Gets the GenericIdentity of the user represented by the current GenericPrincipal.

Methods

AddIdentities(IEnumerable<ClaimsIdentity>)

Adds the specified claims identities to this claims principal.

(Inherited from ClaimsPrincipal)
AddIdentity(ClaimsIdentity)

Adds the specified claims identity to this claims principal.

(Inherited from ClaimsPrincipal)
Clone()

Returns a copy of this instance.

(Inherited from ClaimsPrincipal)
CreateClaimsIdentity(BinaryReader)

Creates a new claims identity.

(Inherited from ClaimsPrincipal)
Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
FindAll(Predicate<Claim>)

Retrieves all of the claims that are matched by the specified predicate.

(Inherited from ClaimsPrincipal)
FindAll(String)

Retrieves all or the claims that have the specified claim type.

(Inherited from ClaimsPrincipal)
FindFirst(Predicate<Claim>)

Retrieves the first claim that is matched by the specified predicate.

(Inherited from ClaimsPrincipal)
FindFirst(String)

Retrieves the first claim with the specified claim type.

(Inherited from ClaimsPrincipal)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetObjectData(SerializationInfo, StreamingContext)

Populates the SerializationInfo with data needed to serialize the current ClaimsPrincipal object.

(Inherited from ClaimsPrincipal)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
HasClaim(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)
HasClaim(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)
IsInRole(String)

Determines whether the current GenericPrincipal belongs to the specified role.

MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

(Inherited from Object)
WriteTo(BinaryWriter, Byte[])

Serializes using a BinaryWriter.

(Inherited from ClaimsPrincipal)
WriteTo(BinaryWriter)

Serializes using a BinaryWriter.

(Inherited from ClaimsPrincipal)

Applies to

Tuote Versiot
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0