WindowsIdentity 建構函式

定義

初始化 WindowsIdentity 類別的新執行個體。

多載

WindowsIdentity(IntPtr)

為指定 Windows 帳戶語彙基元所表示的使用者,初始化 WindowsIdentity 類別的新執行個體。

WindowsIdentity(WindowsIdentity)

使用指定的 WindowsIdentity 物件,初始化 WindowsIdentity 類別的新執行個體。

WindowsIdentity(String)

為指定使用者主要名稱 (UPN) 所表示的使用者,初始化 WindowsIdentity 類別的新執行個體。

WindowsIdentity(IntPtr, String)

為指定 Windows 帳戶語彙基元和指定驗證 (Authentication) 類型所表示的使用者,初始化 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

注意

您可以藉由呼叫 Unmanaged 程式碼,例如 Windows API LogonUser 函式來擷取所代表 userToken 的權杖。 一律透過呼叫 Windows API 函 CloseHandle 式來釋放 userToken 。 如需呼叫 Unmanaged 程式碼的詳細資訊,請參閱 取用非受控 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 的格式為@username 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 帳戶語彙基元和指定驗證 (Authentication) 類型所表示的使用者,初始化 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 。 安全性系統不會使用此值;僅供參考之用。

注意

您可以藉由呼叫 Unmanaged 程式碼,例如 Windows API LogonUser 函式來擷取所代表 userToken 的權杖。 一律透過呼叫 Windows API 函 CloseHandle 式來釋放 userToken 。 如需呼叫 Unmanaged 程式碼的詳細資訊,請參閱 取用非受控 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 。 如需呼叫 Unmanaged 程式碼的詳細資訊,請參閱 使用 Unmanaged 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 。 如需呼叫 Unmanaged 程式碼的詳細資訊,請參閱 使用 Unmanaged DLL 函式

適用於