Skip to content

Commit c019ece

Browse files
authored
Add notes about using impersonation with async/await (#3994)
Added a warning about using `WindowsIdentity.Impersonate` with the async/await pattern, recommending `RunImpersonated` instead.
1 parent ce7fbe3 commit c019ece

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

xml/System.Security.Principal/WindowsIdentity.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,6 +1241,9 @@ Application code does not call this method; it is automatically invoked during g
12411241
## Remarks
12421242
On Windows NT platforms, the current user must have sufficient rights to allow impersonation.
12431243
1244+
> [!WARNING]
1245+
> Avoid using this method with the async/await pattern. In some cases it may lead to reliability issues due to impersonation not being reverted even if the resulting `WindowsImpersonationContext` is disposed. Use `RunImpersonated` instead.
1246+
12441247
12451248
12461249
## Examples
@@ -1306,6 +1309,9 @@ Application code does not call this method; it is automatically invoked during g
13061309
13071310
For more information about calls to unmanaged code, see [Consuming Unmanaged DLL Functions](~/docs/framework/interop/consuming-unmanaged-dll-functions.md).
13081311
1312+
> [!WARNING]
1313+
> Avoid using this method with the async/await pattern. In some cases it may lead to reliability issues due to impersonation not being reverted even if the resulting `WindowsImpersonationContext` is disposed. Use `RunImpersonated` instead.
1314+
13091315
13101316
13111317
## Examples
@@ -1709,6 +1715,13 @@ Application code does not call this method; it is automatically invoked during g
17091715
<remarks>
17101716
<format type="text/markdown"><![CDATA[
17111717
1718+
## Remarks
1719+
1720+
> [!NOTE]
1721+
> This method may be used reliably with the async/await pattern, unlike `Impersonate`. In an async method, the generic overload of this method may be used with an async delegate argument so that the resulting task may be awaited.
1722+
1723+
1724+
17121725
## Examples
17131726
The following example demonstrates the use of the <xref:System.Security.Principal.WindowsIdentity> class to impersonate a user.
17141727
@@ -1834,6 +1847,13 @@ public class ImpersonationDemo
18341847
<remarks>
18351848
<format type="text/markdown"><![CDATA[
18361849
1850+
## Remarks
1851+
1852+
> [!NOTE]
1853+
> This method may be used reliably with the async/await pattern, unlike `Impersonate`. In an async method, this method may be used with an async delegate argument so that the resulting task may be awaited.
1854+
1855+
1856+
18371857
## Examples
18381858
The following example demonstrates the use of the <xref:System.Security.Principal.WindowsIdentity> class to impersonate a user.
18391859

0 commit comments

Comments
 (0)