GenericPrincipal 构造函数

从用户标识和角色名称数组(标识表示的用户属于该数组)初始化 GenericPrincipal 类的新实例。

**命名空间:**System.Security.Principal
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Sub New ( _
    identity As IIdentity, _
    roles As String() _
)
用法
Dim identity As IIdentity
Dim roles As String()

Dim instance As New GenericPrincipal(identity, roles)
public GenericPrincipal (
    IIdentity identity,
    string[] roles
)
public:
GenericPrincipal (
    IIdentity^ identity, 
    array<String^>^ roles
)
public GenericPrincipal (
    IIdentity identity, 
    String[] roles
)
public function GenericPrincipal (
    identity : IIdentity, 
    roles : String[]
)

参数

  • identity
    表示任何用户的 IIdentity 的基实现。
  • roles
    identity 参数表示的用户所属的角色名称数组。

异常

异常类型 条件

ArgumentNullException

identity 参数为 空引用(在 Visual Basic 中为 Nothing)。

示例

下面的代码演示 GenericPrincipal 构造函数的用法。此代码示例摘自一个为 GenericPrincipal 类提供的更大的示例。

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

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

GenericPrincipal 类
GenericPrincipal 成员
System.Security.Principal 命名空间