Skip to content

Instantly share code, notes, and snippets.

@einarwh
Last active December 14, 2015 07:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save einarwh/5052332 to your computer and use it in GitHub Desktop.
Save einarwh/5052332 to your computer and use it in GitHub Desktop.
Impersonation using using.
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