GenericPrincipal(IIdentity, String[]) 构造函数

定义

从用户标识和一组由该标识代表的用户所属的角色名称初始化 GenericPrincipal 类的新实例。

public:
 GenericPrincipal(System::Security::Principal::IIdentity ^ identity, cli::array <System::String ^> ^ roles);
public GenericPrincipal (System.Security.Principal.IIdentity identity, string[] roles);
public GenericPrincipal (System.Security.Principal.IIdentity identity, string[]? roles);
new System.Security.Principal.GenericPrincipal : System.Security.Principal.IIdentity * string[] -> System.Security.Principal.GenericPrincipal
Public Sub New (identity As IIdentity, roles As String())

参数

identity
IIdentity

代表任何用户的 IIdentity 的基本实现。

roles
String[]

一个角色名称数组,其中包含 identity 参数代表的用户。

例外

identity 参数为 null

示例

以下代码演示如何使用 GenericPrincipal 构造函数。 此代码示例是为 GenericPrincipal 类提供的一个更大示例的一部分。

WindowsIdentity^ windowsIdentity = WindowsIdentity::GetCurrent();
array<String^>^roles = gcnew array<String^>(10);
if ( windowsIdentity->IsAuthenticated )
{
   
   // Add custom NetworkUser role.
   roles[ 0 ] = L"NetworkUser";
}

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

if ( windowsIdentity->IsSystem )
{
   
   // Add custom SystemUser role.
   roles[ 2 ] = L"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 = gcnew GenericIdentity(
   userName,authenticationType );

// Construct a GenericPrincipal object based on the generic identity
// and custom roles for the user.
GenericPrincipal^ genericPrincipal = gcnew GenericPrincipal(
   genericIdentity,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);
Dim roles(10) As String
Dim windowsIdentity As WindowsIdentity = windowsIdentity.GetCurrent()

If (windowsIdentity.IsAuthenticated) Then
    ' Add custom NetworkUser role.
    roles(0) = "NetworkUser"
End If

If (windowsIdentity.IsGuest) Then
    ' Add custom GuestUser role.
    roles(1) = "GuestUser"
End If


If (windowsIdentity.IsSystem) Then
    ' Add custom SystemUser role.
    roles(2) = "SystemUser"
End If

' Construct a GenericIdentity object based on the current Windows
' identity name and authentication type.
Dim authenticationType As String = windowsIdentity.AuthenticationType
Dim userName As String = windowsIdentity.Name
Dim genericIdentity = _
    New GenericIdentity(userName, authenticationType)

' Construct a GenericPrincipal object based on the generic identity
' and custom roles for the user.
Dim genericPrincipal As New GenericPrincipal(genericIdentity, roles)

适用于