title: Impersonating a User in Asp.Net
body: <p>My DBA requires all database access to be done through trusted domain account.  This can be done if you set the web.config  .  This requires the user to login or to be on the domain for IE pass the credentials through.  I want to impersonate a user by using code.  I am using the code found in this knowledgebase article:</p>
  
  <p><a href="http://support.microsoft.com/kb/306158" rel="nofollow noreferrer">http://support.microsoft.com/kb/306158</a></p>
  
  <p>It works great, I pass in the credentials, impersonate the user, then make the call to the database and data is returned.  </p>
  
  <p>The problem is if I go to another page, I lose my impersonated credentials.  This means every time I make a call to the database I have to run the impersonate code.   </p>
  
  <p>If IIS can impersonate a domain user for all pages, then why can I not impersonate a user while using code?</p>
  
  <p>It seems to be something with thread context switching.  I have tried setting the alwaysFlowImpersonatingPolicy  in the Aspnet.config file and it did not work.</p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/ms229553.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/ms229553.aspx</a></p>
  
  <p>Any suggestion? Is it even possible to do what I want?</p>