WindowsIdentity 构造函数

定义

初始化 WindowsIdentity 类的新实例。

重载

WindowsIdentity(IntPtr)

为指定的 Windows 帐户标记表示的用户初始化 WindowsIdentity 类的新实例。

WindowsIdentity(WindowsIdentity)

使用指定的 WindowsIdentity 对象初始化 WindowsIdentity 类的新实例。

WindowsIdentity(String)

初始化以指定用户主名称 (UPN) 表示的用户的 WindowsIdentity 类的新实例。

WindowsIdentity(IntPtr, String)

为指定的 Windows 帐户标记和指定的身份验证类型表示的用户初始化 WindowsIdentity 类的新实例。

WindowsIdentity(SerializationInfo, StreamingContext)
已过时.

初始化 SerializationInfo 流中的信息所表示的用户的 WindowsIdentity 类的新实例。

WindowsIdentity(String, String)

初始化以指定用户主名称 (UPN) 和指定身份验证类型共同表示的用户的 WindowsIdentity 类的新实例。

WindowsIdentity(IntPtr, String, WindowsAccountType)

为指定的 Windows 帐户标记、指定的身份验证类型和指定的 Windows 帐户类型表示的用户初始化 WindowsIdentity 类的新实例。

WindowsIdentity(IntPtr, String, WindowsAccountType, Boolean)

为指定的 Windows 帐户标记、指定的身份验证类型、指定的 Windows 帐户类型和指定的身份验证状态表示的用户初始化 WindowsIdentity 类的新实例。

WindowsIdentity(IntPtr)

为指定的 Windows 帐户标记表示的用户初始化 WindowsIdentity 类的新实例。

public:
 WindowsIdentity(IntPtr userToken);
public WindowsIdentity (IntPtr userToken);
new System.Security.Principal.WindowsIdentity : nativeint -> System.Security.Principal.WindowsIdentity
Public Sub New (userToken As IntPtr)

参数

userToken
IntPtr

nativeint

用户的帐户标记,代码当前即以该用户的名义运行。

例外

userToken 为 0。

userToken 是重复的,对于模拟无效。

调用方没有正确的权限。

出现 Win32 错误。

注解

下表显示了 实例 WindowsIdentity的初始属性值。

属性 初始值
AuthenticationType Negotiate
WindowsAccountType Normal
IsAuthenticated false

注意

可以通过调用非托管代码(如 Windows API LogonUser 函数)来检索由 userToken 表示的令牌。 始终通过调用 Windows API CloseHandle 函数释放userToken。 有关调用非托管代码的详细信息,请参阅 使用非托管 DLL 函数

适用于

WindowsIdentity(WindowsIdentity)

使用指定的 WindowsIdentity 对象初始化 WindowsIdentity 类的新实例。

protected:
 WindowsIdentity(System::Security::Principal::WindowsIdentity ^ identity);
protected WindowsIdentity (System.Security.Principal.WindowsIdentity identity);
new System.Security.Principal.WindowsIdentity : System.Security.Principal.WindowsIdentity -> System.Security.Principal.WindowsIdentity
Protected Sub New (identity As WindowsIdentity)

参数

identity
WindowsIdentity

根据其构造 WindowsIdentity 新实例的对象。

适用于

WindowsIdentity(String)

初始化以指定用户主名称 (UPN) 表示的用户的 WindowsIdentity 类的新实例。

public:
 WindowsIdentity(System::String ^ sUserPrincipalName);
public WindowsIdentity (string sUserPrincipalName);
new System.Security.Principal.WindowsIdentity : string -> System.Security.Principal.WindowsIdentity
Public Sub New (sUserPrincipalName As String)

参数

sUserPrincipalName
String

代码运行时所代表用户的 UPN。

例外

Windows 返回 Windows NT 状态代码 STATUS_ACCESS_DENIED。

没有足够的可用内存。

调用方没有正确的权限。

计算机未连接到 Windows 2003 或更高版本的域。

计算机没有运行 Windows 2003 或更高版本。

用户不是计算机所连接域的成员。

注解

UPN 的格式为 用户名@domainname.com,即电子邮件地址。 中 sUserPrincipalName 标识的 UPN 用于通过 Windows API LsaLogonUser 函数检索该用户的令牌。 反过来,该令牌用于标识用户。 由于无法使用提供的 UPN 登录,可能会返回异常。

注意

此构造函数仅适用于已加入 Windows Server 2003 或更高版本域的计算机。 对于较早的域类型,会引发异常。 此限制是由于此构造函数使用 Windows Server 2003 中首次引入 的 KERB_S4U_LOGON 结构。 此外,此构造函数需要对目标用户帐户上的 token-groups-global-and-universal (TGGAU) 属性 具有读取访问权限。

适用于

WindowsIdentity(IntPtr, String)

为指定的 Windows 帐户标记和指定的身份验证类型表示的用户初始化 WindowsIdentity 类的新实例。

public:
 WindowsIdentity(IntPtr userToken, System::String ^ type);
public WindowsIdentity (IntPtr userToken, string type);
new System.Security.Principal.WindowsIdentity : nativeint * string -> System.Security.Principal.WindowsIdentity
Public Sub New (userToken As IntPtr, type As String)

参数

userToken
IntPtr

nativeint

用户的帐户标记,代码当前即以该用户的名义运行。

type
String

(仅供参考。)用于标识用户的身份验证类型。

例外

userToken 为 0。

userToken 是重复的,对于模拟无效。

调用方没有正确的权限。

出现 Win32 错误。

示例

以下代码演示如何使用 WindowsIdentity 构造函数为指定的 Windows 帐户令牌和指定的身份验证类型表示的用户创建 类的新实例 WindowsIdentity 。 此代码示例是为 WindowsIdentity 类提供的一个更大示例的一部分。

void IntPtrStringConstructor( IntPtr logonToken )
{
   
   // Construct a WindowsIdentity object using the input account token 
   // and the specified authentication type.
   String^ authenticationType = "WindowsAuthentication";
   WindowsIdentity^ windowsIdentity = gcnew WindowsIdentity( logonToken,authenticationType );
   
   Console::WriteLine( "Created a Windows identity object named {0}.", windowsIdentity->Name );
}
private static void IntPtrStringConstructor(IntPtr logonToken)
{
    // Construct a WindowsIdentity object using the input account token 
    // and the specified authentication type.
    string authenticationType = "WindowsAuthentication";
    WindowsIdentity windowsIdentity =
                    new WindowsIdentity(logonToken, authenticationType);

    Console.WriteLine("Created a Windows identity object named " +
        windowsIdentity.Name + ".");
}
Private Sub IntPtrStringConstructor(ByVal logonToken As IntPtr)
    ' Construct a WindowsIdentity object using the input account token 
    ' and the specified authentication type
    Dim authenticationType = "WindowsAuthentication"
    Dim windowsIdentity As _
        New WindowsIdentity(logonToken, authenticationType)

    WriteLine("Created a Windows identity object named " + _
        windowsIdentity.Name + ".")
End Sub

注解

下表显示了 实例 WindowsIdentity的初始属性值。

属性 初始值
WindowsAccountType Normal
IsAuthenticated false

参数的值 type 用于设置 AuthenticationType 参数。 如果 typenull,则安全系统在 Windows Vista 和更高版本的 Windows 操作系统上将 设置为 AuthenticationTypeNegotiate ,在早期版本的 Windows 操作系统上将 设置为 Kerberos 。 安全系统不使用此值;它仅供参考。

注意

可以通过调用非托管代码(如 Windows API LogonUser 函数)来检索由 userToken 表示的令牌。 始终通过调用 Windows API CloseHandle 函数释放userToken。 有关调用非托管代码的详细信息,请参阅 使用非托管 DLL 函数

适用于

WindowsIdentity(SerializationInfo, StreamingContext)

注意

This API supports obsolete formatter-based serialization. It should not be called or extended by application code.

初始化 SerializationInfo 流中的信息所表示的用户的 WindowsIdentity 类的新实例。

public:
 WindowsIdentity(System::Runtime::Serialization::SerializationInfo ^ info, System::Runtime::Serialization::StreamingContext context);
public WindowsIdentity (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
[System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public WindowsIdentity (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
new System.Security.Principal.WindowsIdentity : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> System.Security.Principal.WindowsIdentity
[<System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
new System.Security.Principal.WindowsIdentity : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> System.Security.Principal.WindowsIdentity
Public Sub New (info As SerializationInfo, context As StreamingContext)

参数

info
SerializationInfo

包含用户帐户信息的对象。

context
StreamingContext

指示流特征的对象。

属性

例外

WindowsIdentity 无法跨进程序列化。

调用方没有正确的权限。

出现 Win32 错误。

注解

重要

使用不受信任的数据调用此方法存在安全风险。 仅使用受信任的数据调用此方法。 有关详细信息,请参阅 验证所有输入

适用于

WindowsIdentity(String, String)

初始化以指定用户主名称 (UPN) 和指定身份验证类型共同表示的用户的 WindowsIdentity 类的新实例。

public:
 WindowsIdentity(System::String ^ sUserPrincipalName, System::String ^ type);
public WindowsIdentity (string sUserPrincipalName, string type);
new System.Security.Principal.WindowsIdentity : string * string -> System.Security.Principal.WindowsIdentity
Public Sub New (sUserPrincipalName As String, type As String)

参数

sUserPrincipalName
String

代码运行时所代表用户的 UPN。

type
String

(仅供参考。)用于标识用户的身份验证类型。

例外

Windows 返回 Windows NT 状态代码 STATUS_ACCESS_DENIED。

没有足够的可用内存。

调用方没有正确的权限。

计算机未连接到 Windows 2003 或更高版本的域。

计算机没有运行 Windows 2003 或更高版本。

用户不是计算机所连接域的成员。

注解

参数的值 type 用于设置 AuthenticationType 参数。 如果 typenull,则安全系统在 Windows Vista 和更高版本的 Windows 操作系统上设置为 AuthenticationTypeNegotiate ,在早期版本的 Windows 操作系统上设置为 Kerberos 。 安全系统不使用此值;它仅供参考。

sUserPrincipalName 标识的 UPN 用于通过 Windows API LsaLogonUser 函数检索该用户的令牌。 反过来,该令牌用于标识用户。 由于无法使用提供的 UPN 登录,可能会返回异常。

注意

此构造函数仅适用于已加入 Windows Server 2003 或更高版本域的计算机。 对于较早的域类型,会引发异常。 此限制是由于此构造函数使用 Windows Server 2003 中首次引入 的 KERB_S4U_LOGON 结构。 此外,此构造函数需要对目标用户帐户上的 token-groups-global-and-universal (TGGAU) 属性 具有读取访问权限。

适用于

WindowsIdentity(IntPtr, String, WindowsAccountType)

为指定的 Windows 帐户标记、指定的身份验证类型和指定的 Windows 帐户类型表示的用户初始化 WindowsIdentity 类的新实例。

public:
 WindowsIdentity(IntPtr userToken, System::String ^ type, System::Security::Principal::WindowsAccountType acctType);
public WindowsIdentity (IntPtr userToken, string type, System.Security.Principal.WindowsAccountType acctType);
new System.Security.Principal.WindowsIdentity : nativeint * string * System.Security.Principal.WindowsAccountType -> System.Security.Principal.WindowsIdentity
Public Sub New (userToken As IntPtr, type As String, acctType As WindowsAccountType)

参数

userToken
IntPtr

nativeint

用户的帐户标记,代码当前即以该用户的名义运行。

type
String

(仅供参考。)用于标识用户的身份验证类型。

acctType
WindowsAccountType

枚举值之一。

例外

userToken 为 0。

userToken 是重复的,对于模拟无效。

调用方没有正确的权限。

出现 Win32 错误。

示例

下面的代码演示如何使用 WindowsIdentity 构造函数为由指定的 Windows 帐户令牌、指定的身份验证类型和指定的 Windows 帐户类型表示的用户创建 类的新实例 WindowsIdentity 。 此代码示例是为 WindowsIdentity 类提供的一个更大示例的一部分。

void IntPtrStringTypeConstructor( IntPtr logonToken )
{
   
   // Construct a WindowsIdentity object using the input account token,
   // and the specified authentication type and Windows account type.
   String^ authenticationType = "WindowsAuthentication";
   WindowsAccountType guestAccount = WindowsAccountType::Guest;
   WindowsIdentity^ windowsIdentity = gcnew WindowsIdentity( logonToken,authenticationType,guestAccount );
   
   Console::WriteLine( "Created a Windows identity object named {0}.", windowsIdentity->Name );
}
private static void IntPtrStringTypeConstructor(IntPtr logonToken)
{
    // Construct a WindowsIdentity object using the input account token,
    // and the specified authentication type, and Windows account type.
    string authenticationType = "WindowsAuthentication";
    WindowsAccountType guestAccount = WindowsAccountType.Guest;
    WindowsIdentity windowsIdentity =
        new WindowsIdentity(logonToken, authenticationType, guestAccount);

    Console.WriteLine("Created a Windows identity object named " +
        windowsIdentity.Name + ".");
}
Private Sub IntPtrStringTypeConstructor(ByVal logonToken As IntPtr)
    ' Construct a WindowsIdentity object using the input account token,
    ' and the specified authentication type and Windows account type.
    Dim authenticationType As String = "WindowsAuthentication"
    Dim guestAccount As WindowsAccountType = WindowsAccountType.Guest
    Dim windowsIdentity As _
        New WindowsIdentity(logonToken, authenticationType, guestAccount)

    WriteLine("Created a Windows identity object named " + _
        windowsIdentity.Name + ".")
End Sub

注解

下表显示了 实例 WindowsIdentity的初始属性值。

属性 初始值
IsAuthenticated false

参数的值 type 用于设置 AuthenticationType 参数。 如果 typenull,则安全系统在 Windows Vista 和更高版本的 Windows 操作系统上设置为 AuthenticationTypeNegotiate ,在早期版本的 Windows 操作系统上设置为 Kerberos 。 安全系统不使用此值;它仅供参考。

注意

可以通过调用非托管代码(如 Windows API LogonUser 函数)来检索由 userToken 表示的令牌。 始终通过调用 Windows API CloseHandle 函数释放userToken。 有关调用非托管代码的详细信息,请参阅 使用非托管 DLL 函数

适用于

WindowsIdentity(IntPtr, String, WindowsAccountType, Boolean)

为指定的 Windows 帐户标记、指定的身份验证类型、指定的 Windows 帐户类型和指定的身份验证状态表示的用户初始化 WindowsIdentity 类的新实例。

public:
 WindowsIdentity(IntPtr userToken, System::String ^ type, System::Security::Principal::WindowsAccountType acctType, bool isAuthenticated);
public WindowsIdentity (IntPtr userToken, string type, System.Security.Principal.WindowsAccountType acctType, bool isAuthenticated);
new System.Security.Principal.WindowsIdentity : nativeint * string * System.Security.Principal.WindowsAccountType * bool -> System.Security.Principal.WindowsIdentity
Public Sub New (userToken As IntPtr, type As String, acctType As WindowsAccountType, isAuthenticated As Boolean)

参数

userToken
IntPtr

nativeint

用户的帐户标记,代码当前即以该用户的名义运行。

type
String

(仅供参考。)用于标识用户的身份验证类型。

acctType
WindowsAccountType

枚举值之一。

isAuthenticated
Boolean

true 指示用户已经过身份验证,否则为 false

例外

userToken 为 0。

userToken 是重复的,对于模拟无效。

调用方没有正确的权限。

出现 Win32 错误。

示例

以下代码演示如何使用 WindowsIdentity 构造函数为由指定的 Windows 帐户令牌、指定的身份验证类型、指定的 Windows 帐户类型和指定的身份验证状态表示的用户创建 类的新实例 WindowsIdentity 。 此代码示例是为 WindowsIdentity 类提供的一个更大示例的一部分。

void IntPrtStringTypeBoolConstructor( IntPtr logonToken )
{
   
   // Construct a WindowsIdentity object using the input account token,
   // and the specified authentication type, Windows account type, and
   // authentication flag.
   String^ authenticationType = "WindowsAuthentication";
   WindowsAccountType guestAccount = WindowsAccountType::Guest;
   bool isAuthenticated = true;
   WindowsIdentity^ windowsIdentity = gcnew WindowsIdentity( logonToken,authenticationType,guestAccount,isAuthenticated );
   
   Console::WriteLine( "Created a Windows identity object named {0}.", windowsIdentity->Name );
}
private static void IntPrtStringTypeBoolConstructor(IntPtr logonToken)
{
    // Construct a WindowsIdentity object using the input account token,
    // and the specified authentication type, Windows account type, and
    // authentication flag.
    string authenticationType = "WindowsAuthentication";
    WindowsAccountType guestAccount = WindowsAccountType.Guest;
    bool isAuthenticated = true;
    WindowsIdentity windowsIdentity = new WindowsIdentity(
        logonToken, authenticationType, guestAccount, isAuthenticated);

    Console.WriteLine("Created a Windows identity object named " +
        windowsIdentity.Name + ".");
}
Private Sub IntPrtStringTypeBoolConstructor(ByVal logonToken As IntPtr)
    ' Construct a WindowsIdentity object using the input account token,
    ' and the specified authentication type, Windows account type, and
    ' authentication flag.
    Dim authenticationType As String = "WindowsAuthentication"
    Dim guestAccount As WindowsAccountType = WindowsAccountType.Guest
    Dim isAuthenticated As Boolean = True
    Dim windowsIdentity As New WindowsIdentity( _
        logonToken, authenticationType, guestAccount, isAuthenticated)

    WriteLine("Created a Windows identity object named " + _
        windowsIdentity.Name + ".")
End Sub

注解

参数的值 type 用于设置 AuthenticationType 参数。 如果 typenull,则安全系统在 Windows Vista 和更高版本的 Windows 操作系统上设置为 AuthenticationTypeNegotiate ,在早期版本的 Windows 操作系统上设置为 Kerberos 。 安全系统不使用此值;它仅供参考。

可以通过调用非托管代码(如 Windows API LogonUser 函数)来检索由 userToken 表示的令牌。 始终通过调用 Windows API CloseHandle 函数释放userToken。 有关调用非托管代码的详细信息,请参阅 使用非托管 DLL 函数

适用于