HostingEnvironment.Impersonate Method

Definition

Allows code to impersonate a different user.

Overloads

Impersonate()

Impersonates the user represented by the application identity.

Impersonate(IntPtr)

Impersonates the user represented by the specified user token.

Impersonate(IntPtr, String)

Impersonates the user specified by the configuration settings for the specified virtual path, or the specified user token.

Impersonate()

Impersonates the user represented by the application identity.

C#
public static IDisposable Impersonate();

Returns

An IDisposable object that represents the Windows user prior to impersonation; this object can be used to revert to the original user's context.

Exceptions

The process cannot impersonate.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.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

Impersonate(IntPtr)

Impersonates the user represented by the specified user token.

C#
public static IDisposable Impersonate(IntPtr token);

Parameters

token
IntPtr

The handle of a Windows account token.

Returns

An IDisposable object that represents the Windows user prior to impersonation; this object can be used to revert to the original user's context.

Exceptions

The process cannot impersonate.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.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

Impersonate(IntPtr, String)

Impersonates the user specified by the configuration settings for the specified virtual path, or the specified user token.

C#
public static IDisposable Impersonate(IntPtr userToken, string virtualPath);

Parameters

userToken
IntPtr

The handle of a Windows account token.

virtualPath
String

The path to the requested resource.

Returns

An IDisposable object that represents the Windows user prior to impersonation; this object can be used to revert to the original user's context.

Exceptions

The process cannot impersonate.

Remarks

The Impersonate method impersonates the user that results after applying all elements in the configuration settings affecting the specified virtual path. If an identity element is not specified for the virtual path, the Impersonate method impersonates the specified Windows account.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.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