Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Impersonation occurs at the level of the thread. Impersonation causes the access token of the thread, which is usually inherited from the process, to be replaced with another. The best practice is to revert the effect of impersonation and thus the token as soon as you are done with the operation(s) for which it was needed. The story is no different with IIS or ASP.NET. Each request is usually handled by a distinct thread so you will have to make each thread impersonate the user.</p> <blockquote> <p>This means every time I make a call to the database I have to run the impersonate code.</p> </blockquote> <p>So that is correct.</p> <blockquote> <p>If IIS can impersonate a domain user for all pages, then why can I not impersonate a user while using code?</p> </blockquote> <p>IIS does not do it any differently and so it may only be a perceived illusion. It cannot impersonate a user for all pages unless all pages are being served by the same thread and where the impersonated token has never been reverted as each page is served.</p> <blockquote> <p>It seems to be something with thread context switching.</p> </blockquote> <p>Not really. Unless you are doing asynchronous processing (which you don't state you do in your question), the flow of the impersonation context won't be relevant. You only need to worry about flowing the impersonation context if you are causing a thread switch either directly or indirectly during the processing of a single request. If you want that the work done by a secondary (worker) thread continues to occur under the impersonation context of the primary one then you need to make sure the secondary thread borrows the impersonation token. In .NET Framework 1.1, you would have to take great care and manually orchestrate the flow of the impersonation context. With .NET 2.0, however, the <a href="http://msdn.microsoft.com/en-us/library/system.threading.executioncontext.aspx" rel="nofollow noreferrer">ExecutionContext</a> API was introduced and does a lot of the heavy-lifting.</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