Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to set Thread.CurrentPrincipal for use throughout the application?
    text
    copied!<p>In an ASP.net application I'm using a Login control with a custom membership provider that I wrote. What I want to do is to set <code>Thread.CurrentPrincipal</code> to my custom Principal object, just after the user is authenticated.</p> <p>I'm using the setter: <code>Thread.CurrentPrincipal</code> and it sets the Principal object for me but, on all the consequent threads this CurrentPrincipal is overridden with the default one.</p> <p>Here is my code for the Authenticate event of the Login control:</p> <pre><code>protected void Login1_Authenticate(object sender, AuthenticateEventArgs e) { string username = Login1.UserName; string password = Login1.Password; if (Membership.ValidateUser(username, password)) { var login = sender as Login; var phoenixIdentity = new PhoenixIdentity("B", "Forms" , true); var principal = new PhoenixPrincipal(phoenixIdentity); Thread.CurrentPrincipal = principal; AppDomain.CurrentDomain.SetThreadPrincipal(principal); HttpContext.Current.User = principal; e.Authenticated = true; } } </code></pre> <p>For example, imagine that I login with the username A, everything goes well... Validation passes, but I hardcode the user with the username B in the Identity object which is set to the Principal object I set as the <code>CurrentPrincipal</code> object.</p> <p>When I check which user is set to the <code>CurrentPrincipal</code> Identity at the end of this method it says it's user B. But when I load another page and then check what the Identity of the <code>CurrentPrincipal</code> is, it says it's user A.</p> <p>So, how can I make my <code>CurrentPrincipal</code> object to be persistent across all other threads, and where/when does this Login control set the <code>CurrentPrincipal</code> object of the Thread?</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