<p>I have an app that has impersonation used throughout.  But when a user is logged in as an admin, a few operation require them to write to the server itself.  Now if these users do not have rights on the actual server (some don't) it will not let them write. </p>

<p>What I want to do is turn off impersonation for just a couple commands.</p>

<p>Is there a way to do something like this?</p>

<pre><code>using(HostingEnvironment.Impersonate.Off())
  //I know this isn't a command, but you get the idea?
</code></pre>

<p>Thank you.</p>

I am not sure if this is the preferred approach but when I wanted to do this I new'd up an instance of a
WindowsIdentity
and called the
Impersonate
method.  This allows subsequent code to impersonate a different Windows user. It returns a
WindowsImpersonationContext
that has an
Undo
method which reverts the impersonation context back again.