WindowsIdentity.GetCurrent Method

Definition

Important

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

Returns a WindowsIdentity object that represents the current Windows user.

Overloads

GetCurrent(TokenAccessLevels)

Returns a WindowsIdentity object that represents the current Windows user, using the specified desired token access level.

GetCurrent(Boolean)

Returns a WindowsIdentity object that represents the Windows identity for either the thread or the process, depending on the value of the ifImpersonating parameter.

GetCurrent()

Returns a WindowsIdentity object that represents the current Windows user.

GetCurrent(TokenAccessLevels)

Returns a WindowsIdentity object that represents the current Windows user, using the specified desired token access level.

C#
public static System.Security.Principal.WindowsIdentity GetCurrent(System.Security.Principal.TokenAccessLevels desiredAccess);

Parameters

desiredAccess
TokenAccessLevels

A bitwise combination of the enumeration values.

Returns

An object that represents the current user.

Remarks

The desiredAccess parameter specifies an access mask that identifies the requested types of access to the access token. These requested access types are compared with the token's discretionary access control list (DACL) to determine which types of access are granted or denied.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5

GetCurrent(Boolean)

Returns a WindowsIdentity object that represents the Windows identity for either the thread or the process, depending on the value of the ifImpersonating parameter.

C#
public static System.Security.Principal.WindowsIdentity? GetCurrent(bool ifImpersonating);
C#
public static System.Security.Principal.WindowsIdentity GetCurrent(bool ifImpersonating);

Parameters

ifImpersonating
Boolean

true to return the WindowsIdentity only if the thread is currently impersonating; false to return the WindowsIdentity of the thread if it is impersonating or the WindowsIdentity of the process if the thread is not currently impersonating.

Returns

An object that represents a Windows user.

Remarks

If ifImpersonating is true and the thread is not impersonating, the returned WindowsIdentity object has no value. If ifImpersonating is false and the thread is impersonating, the WindowsIdentity for the thread is returned. If ifImpersonating is false and the thread is not impersonating, the WindowsIdentity for the process is returned.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5

GetCurrent()

Returns a WindowsIdentity object that represents the current Windows user.

C#
public static System.Security.Principal.WindowsIdentity GetCurrent();

Returns

An object that represents the current user.

Exceptions

The caller does not have the correct permissions.

Examples

The following code shows the use of the GetCurrent method to return a WindowsIdentity object that represents the current Windows user. This code example is part of a larger example provided for the WindowsIdentity class.

C#
IntPtr accountToken = WindowsIdentity.GetCurrent().Token;
Console.WriteLine( "Token number is: " + accountToken.ToString());

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5