Lire en anglais

Partager via


GenericPrincipal(IIdentity, String[]) Constructeur

Définition

Initialise une nouvelle instance de la classe GenericPrincipal à partir d’une identité utilisateur et un tableau de noms de rôles auquel appartient l’utilisateur représenté par cette identité.

C#
public GenericPrincipal (System.Security.Principal.IIdentity identity, string[] roles);
C#
public GenericPrincipal (System.Security.Principal.IIdentity identity, string[]? roles);

Paramètres

identity
IIdentity

Une implémentation de base de IIdentity qui représente n’importe quel utilisateur.

roles
String[]

Un tableau de noms de rôles auquel l’utilisateur représenté par le paramètre identity appartient.

Exceptions

Le paramètre identity a la valeur null.

Exemples

Le code suivant montre l’utilisation du GenericPrincipal constructeur. Cet exemple de code fait partie d’un exemple plus grand fourni pour la GenericPrincipal classe .

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

S’applique à

Produit Versions
.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
.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