Export (0) Print
Expand All

GenericPrincipal.GenericPrincipal Constructor

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.

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

public GenericPrincipal (
	IIdentity identity,
	string[] roles
)
public GenericPrincipal (
	IIdentity identity, 
	String[] roles
)
public function GenericPrincipal (
	identity : IIdentity, 
	roles : String[]
)
Not applicable.

Parameters

identity

A basic implementation of IIdentity that represents any user.

roles

An array of role names to which the user represented by the identity parameter belongs.

Exception typeCondition

ArgumentNullException

The identity parameter is a null reference (Nothing in Visual Basic).

The following code shows the use of the GenericPrincipal constructor. This code example is part of a larger example provided for the GenericPrincipal class.

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

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

Windows Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft