Last active
December 14, 2015 07:39
-
-
Save einarwh/5052332 to your computer and use it in GitHub Desktop.
Impersonation using using.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
static void Main() | |
{ | |
WindowsIdentity id = ...; | |
using (new Persona(id)) | |
{ | |
// Act as id. | |
} | |
} | |
} | |
class Persona : IDisposable | |
{ | |
private readonly WindowsImpersonationContext _; | |
public Persona(WindowsIdentity id) | |
{ | |
_ = id.Impersonate(); | |
} | |
public void Dispose() | |
{ | |
_.Undo(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment