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 具有 用户名@域名格式.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 参数。 null如果是type,则安全系统将AuthenticationType设置为 Negotiate Windows Vista 和更高版本的 Windows 操作系统,以及 Kerberos Windows 操作系统的早期版本。 安全系统不使用此值;仅供参考。

注释

可以通过调用非托管代码(如 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);
[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);
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}")>]
new System.Security.Principal.WindowsIdentity : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> System.Security.Principal.WindowsIdentity
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

一个指示流特征的对象。

属性

例外

不能跨进程序列化 A WindowsIdentity

调用方没有正确的权限。

-或-

发生了 Win32 错误。

注解

Important

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

适用于

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 参数。 null如果是type,则安全系统将AuthenticationType设置为 Negotiate Windows Vista 和更高版本的 Windows 操作系统,以及 Kerberos Windows 操作系统的早期版本。 安全系统不使用此值;仅供参考。

标识的 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 参数。 null如果是type,则安全系统将AuthenticationType设置为 Negotiate Windows Vista 和更高版本的 Windows 操作系统,以及 Kerberos Windows 操作系统的早期版本。 安全系统不使用此值;仅供参考。

注释

可以通过调用非托管代码(如 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 参数。 null如果是type,则安全系统将AuthenticationType设置为 Negotiate Windows Vista 和更高版本的 Windows 操作系统,以及 Kerberos Windows 操作系统的早期版本。 安全系统不使用此值;仅供参考。

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

适用于