WindowsIdentity WindowsIdentity WindowsIdentity WindowsIdentity Constructors

Definition

Overloads

WindowsIdentity(IntPtr) WindowsIdentity(IntPtr) WindowsIdentity(IntPtr) WindowsIdentity(IntPtr)

Initializes a new instance of the WindowsIdentity class for the user represented by the specified Windows account token.

WindowsIdentity(WindowsIdentity) WindowsIdentity(WindowsIdentity) WindowsIdentity(WindowsIdentity) WindowsIdentity(WindowsIdentity)

Initializes a new instance of the WindowsIdentity class by using the specified WindowsIdentity object.

WindowsIdentity(String) WindowsIdentity(String) WindowsIdentity(String) WindowsIdentity(String)

Initializes a new instance of the WindowsIdentity class for the user represented by the specified User Principal Name (UPN).

WindowsIdentity(IntPtr, String) WindowsIdentity(IntPtr, String) WindowsIdentity(IntPtr, String) WindowsIdentity(IntPtr, String)

Initializes a new instance of the WindowsIdentity class for the user represented by the specified Windows account token and the specified authentication type.

WindowsIdentity(SerializationInfo, StreamingContext) WindowsIdentity(SerializationInfo, StreamingContext) WindowsIdentity(SerializationInfo, StreamingContext) WindowsIdentity(SerializationInfo, StreamingContext)

Initializes a new instance of the WindowsIdentity class for the user represented by information in a SerializationInfo stream.

WindowsIdentity(String, String) WindowsIdentity(String, String) WindowsIdentity(String, String) WindowsIdentity(String, String)

Initializes a new instance of the WindowsIdentity class for the user represented by the specified User Principal Name (UPN) and the specified authentication type.

WindowsIdentity(IntPtr, String, WindowsAccountType) WindowsIdentity(IntPtr, String, WindowsAccountType) WindowsIdentity(IntPtr, String, WindowsAccountType) WindowsIdentity(IntPtr, String, WindowsAccountType)

Initializes a new instance of the WindowsIdentity class for the user represented by the specified Windows account token, the specified authentication type, and the specified Windows account type.

WindowsIdentity(IntPtr, String, WindowsAccountType, Boolean) WindowsIdentity(IntPtr, String, WindowsAccountType, Boolean) WindowsIdentity(IntPtr, String, WindowsAccountType, Boolean) WindowsIdentity(IntPtr, String, WindowsAccountType, Boolean)

Initializes a new instance of the WindowsIdentity class for the user represented by the specified Windows account token, the specified authentication type, the specified Windows account type, and the specified authentication status.

WindowsIdentity(IntPtr) WindowsIdentity(IntPtr) WindowsIdentity(IntPtr) WindowsIdentity(IntPtr)

Initializes a new instance of the WindowsIdentity class for the user represented by the specified Windows account token.

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

Parameters

userToken
IntPtr IntPtr IntPtr IntPtr

The account token for the user on whose behalf the code is running.

Exceptions

userToken is 0.

-or-

userToken is duplicated and invalid for impersonation.

The caller does not have the correct permissions.

-or-

A Win32 error occurred.

Remarks

The following table shows initial property values for an instance of WindowsIdentity.

Property Initial value
AuthenticationType Negotiate
WindowsAccountType Normal
IsAuthenticated false

Note

You can retrieve the token represented by userToken by calling unmanaged code such as the Windows API LogonUser function. Always release userToken by calling the Windows API CloseHandle function. For more information on calling unmanaged code, see Consuming Unmanaged DLL Functions.

Security

SecurityPermission
for the ability to access unmanaged code. Associated enumeration: UnmanagedCode.

WindowsIdentity(WindowsIdentity) WindowsIdentity(WindowsIdentity) WindowsIdentity(WindowsIdentity) WindowsIdentity(WindowsIdentity)

Initializes a new instance of the WindowsIdentity class by using the specified WindowsIdentity object.

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)

Parameters

identity
WindowsIdentity WindowsIdentity WindowsIdentity WindowsIdentity

The object from which to construct the new instance of WindowsIdentity.

WindowsIdentity(String) WindowsIdentity(String) WindowsIdentity(String) WindowsIdentity(String)

Initializes a new instance of the WindowsIdentity class for the user represented by the specified User Principal Name (UPN).

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)

Parameters

sUserPrincipalName
String String String String

The UPN for the user on whose behalf the code is running.

Exceptions

The caller does not have the correct permissions.

-or-

The computer is not attached to a Windows 2003 or later domain.

-or-

The computer is not running Windows 2003 or later.

-or-

The user is not a member of the domain the computer is attached to.

Remarks

A UPN has the format username@domainname.com, in other words, an email address. The UPN identified in sUserPrincipalName is used to retrieve a token for that user through the Windows API LsaLogonUser function. In turn that token is used to identify the user. An exception might be returned due to the inability to log on using the supplied UPN.

Note

This constructor is intended for use only on computers joined to Windows Server 2003 or later domains. An exception is thrown for earlier domain types. This restriction is due to the fact that this constructor uses the KERB_S4U_LOGON structure, which was first introduced in Windows Server 2003.

Security

SecurityPermission
for the ability to access unmanaged code. Associated enumeration: UnmanagedCode.

WindowsIdentity(IntPtr, String) WindowsIdentity(IntPtr, String) WindowsIdentity(IntPtr, String) WindowsIdentity(IntPtr, String)

Initializes a new instance of the WindowsIdentity class for the user represented by the specified Windows account token and the specified authentication type.

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)

Parameters

userToken
IntPtr IntPtr IntPtr IntPtr

The account token for the user on whose behalf the code is running.

type
String String String String

(Informational use only.) The type of authentication used to identify the user.

Exceptions

userToken is 0.

-or-

userToken is duplicated and invalid for impersonation.

The caller does not have the correct permissions.

-or-

A Win32 error occurred.

Examples

The following code shows the use of the WindowsIdentity constructor to create a new instance of the WindowsIdentity class for the user represented by the specified Windows account token and the specified authentication type. This code example is part of a larger example provided for the WindowsIdentity class.

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

Remarks

The following table shows initial property values for an instance of WindowsIdentity.

Property Initial value
WindowsAccountType Normal
IsAuthenticated false

The value of the type parameter is used to set the AuthenticationType parameter. If type is null, the security system sets AuthenticationType to Negotiate on Windows Vista and later versions of the Windows operating system, and to Kerberos on earlier versions of the Windows operating system. The security system does not use this value; it is for informational use only.

Note

You can retrieve the token represented by userToken by calling unmanaged code such as the Windows API LogonUser function. Always release userToken by calling the Windows API CloseHandle function. For more information on calling unmanaged code, see Consuming Unmanaged DLL Functions.

Security

SecurityPermission
for the ability to access unmanaged code. Associated enumeration: UnmanagedCode.

WindowsIdentity(SerializationInfo, StreamingContext) WindowsIdentity(SerializationInfo, StreamingContext) WindowsIdentity(SerializationInfo, StreamingContext) WindowsIdentity(SerializationInfo, StreamingContext)

Initializes a new instance of the WindowsIdentity class for the user represented by information in a SerializationInfo stream.

public:
 WindowsIdentity(System::Runtime::Serialization::SerializationInfo ^ info, System::Runtime::Serialization::StreamingContext context);
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
Public Sub New (info As SerializationInfo, context As StreamingContext)

Parameters

info
SerializationInfo SerializationInfo SerializationInfo SerializationInfo

The object containing the account information for the user.

context
StreamingContext StreamingContext StreamingContext StreamingContext

An object that indicates the stream characteristics.

Exceptions

The caller does not have the correct permissions.

-or-

A Win32 error occurred.

Remarks

Important

Calling this method with untrusted data is a security risk. Call this method only with trusted data. For more information, see Data Validation.

Security

SecurityPermission
for ability to manipulate the principal object. Associated enumeration: ControlPrincipal.

WindowsIdentity(String, String) WindowsIdentity(String, String) WindowsIdentity(String, String) WindowsIdentity(String, String)

Initializes a new instance of the WindowsIdentity class for the user represented by the specified User Principal Name (UPN) and the specified authentication type.

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)

Parameters

sUserPrincipalName
String String String String

The UPN for the user on whose behalf the code is running.

type
String String String String

(Informational use only.) The type of authentication used to identify the user.

Exceptions

The caller does not have the correct permissions.

-or-

The computer is not attached to a Windows 2003 or later domain.

-or-

The computer is not running Windows 2003 or later.

-or-

The user is not a member of the domain the computer is attached to.

Remarks

The value of the type parameter is used to set the AuthenticationType parameter. If type is null, the security system sets AuthenticationType to Negotiate on Windows Vista and later versions of the Windows operating system, and to Kerberos on earlier versions of the Windows operating system. The security system does not use this value; it is for informational use only.

The UPN identified in sUserPrincipalName is used to retrieve a token for that user through the Windows API LsaLogonUser function. In turn that token is used to identify the user. An exception might be returned due to the inability to log on using the supplied UPN.

Note

This constructor is intended for use only on computers joined to Windows Server 2003 or later domains. An exception is thrown for earlier domain types. This restriction is due to the fact that this constructor uses the KERB_S4U_LOGON structure, which was first introduced in Windows Server 2003.

Security

SecurityPermission
for the ability to access unmanaged code. Associated enumeration: UnmanagedCode.

WindowsIdentity(IntPtr, String, WindowsAccountType) WindowsIdentity(IntPtr, String, WindowsAccountType) WindowsIdentity(IntPtr, String, WindowsAccountType) WindowsIdentity(IntPtr, String, WindowsAccountType)

Initializes a new instance of the WindowsIdentity class for the user represented by the specified Windows account token, the specified authentication type, and the specified Windows account type.

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)

Parameters

userToken
IntPtr IntPtr IntPtr IntPtr

The account token for the user on whose behalf the code is running.

type
String String String String

(Informational use only.) The type of authentication used to identify the user.

Exceptions

userToken is 0.

-or-

userToken is duplicated and invalid for impersonation.

The caller does not have the correct permissions.

-or-

A Win32 error occurred.

Examples

The following code shows the use of the WindowsIdentity constructor to create a new instance of the WindowsIdentity class for the user represented by the specified Windows account token, the specified authentication type, and the specified Windows account type. This code example is part of a larger example provided for the WindowsIdentity class.

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

Remarks

The following table shows initial property values for an instance of WindowsIdentity.

Property Initial Value
IsAuthenticated false

The value of the type parameter is used to set the AuthenticationType parameter. If type is null, the security system sets AuthenticationType to Negotiate on Windows Vista and later versions of the Windows operating system, and to Kerberos on earlier versions of the Windows operating system. The security system does not use this value; it is for informational use only.

Note

You can retrieve the token represented by userToken by calling unmanaged code such as the Windows API LogonUser function. Always release userToken by calling the Windows API CloseHandle function. For more information on calling unmanaged code, see Consuming Unmanaged DLL Functions.

Security

SecurityPermission
for the ability to access unmanaged code. Associated enumeration: UnmanagedCode.

WindowsIdentity(IntPtr, String, WindowsAccountType, Boolean) WindowsIdentity(IntPtr, String, WindowsAccountType, Boolean) WindowsIdentity(IntPtr, String, WindowsAccountType, Boolean) WindowsIdentity(IntPtr, String, WindowsAccountType, Boolean)

Initializes a new instance of the WindowsIdentity class for the user represented by the specified Windows account token, the specified authentication type, the specified Windows account type, and the specified authentication status.

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)

Parameters

userToken
IntPtr IntPtr IntPtr IntPtr

The account token for the user on whose behalf the code is running.

type
String String String String

(Informational use only.) The type of authentication used to identify the user.

isAuthenticated
Boolean Boolean Boolean Boolean

true to indicate that the user is authenticated; otherwise, false.

Exceptions

userToken is 0.

-or-

userToken is duplicated and invalid for impersonation.

The caller does not have the correct permissions.

-or-

A Win32 error occurred.

Examples

The following code shows the use of the WindowsIdentity constructor to create a new instance of the WindowsIdentity class for the user represented by the specified Windows account token, the specified authentication type, the specified Windows account type, and the specified authentication status. This code example is part of a larger example provided for the WindowsIdentity class.

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

Remarks

The value of the type parameter is used to set the AuthenticationType parameter. If type is null, the security system sets AuthenticationType to Negotiate on Windows Vista and later versions of the Windows operating system, and to Kerberos on earlier versions of the Windows operating system. The security system does not use this value; it is for informational use only.

You can retrieve the token represented by userToken by calling unmanaged code such as the Windows API LogonUser function. Always release userToken by calling the Windows API CloseHandle function. For more information on calling unmanaged code, see Consuming Unmanaged DLL Functions.

Security

SecurityPermission
for the ability to access unmanaged code. Associated enumeration: UnmanagedCode.

Applies to