Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>E.g. if you want to run a thread in the same context as the process, you could do something similar to this:</p> <pre><code> private void ImpersonateThread() { new Thread(() =&gt; { Thread.CurrentPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent()); //Do the work... }).Start(); } </code></pre> <p>This is very useful if running a website (on MS-IIS) where the application-pool is running as one user and the authentication mode is set to windows authentication. Then you probably would like the threads to run as the user accessing the website.</p> <p><strong>UPDATE:</strong></p> <p>I just want to clarify this. The line:</p> <pre><code> Thread.CurrentPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent()); </code></pre> <p>just transfer the user (the user is still not impersonated to the thread yet) to the thread so other components may see who the user are and use the information if necessary, e.g. like this:</p> <pre><code> System.Security.Principal.WindowsImpersonationContext impersonationContext; impersonationContext = ((System.Security.Principal.WindowsIdentity)System.Threading.Thread.CurrentPrincipal.Identity).Impersonate(); //Do the work that you want within the impersonated users context. impersonationContext.Undo(); </code></pre> <p>Between the impersonation and the Undo, the user is impersonated and all code is running as this user. I know the user is the same in this sample, but imagine that the thread holds a user entirely different from the user running the process (something a user has entered or similar). Hope this helps :-)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload