Thread agility(switching) and ASP.NET

  • Thread starter Thread starter Oscar Thornell
  • Start date Start date
O

Oscar Thornell

Hi,

I am trying to figure out if I've got it wrong in the following code...
Could I lose the principal in the business layer(not HttpContext aware) if
ASP.NET switches executing thread?
I would presume that if a switch occur the principal/context would be
migrated to the new thread. Am I wrong??
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
GenericPrincipal principal = GetGenericPrincipal();


if (principal != null)
{
//Attach the new principal object to the current HttpContext object
Context.User = principal;

// Set the current thread's principal to the HttpContext principal
// This means that we can get the caller's principal in the business
layer
// without requiring knowledge of the HttpContext
System.Threading.Thread.CurrentPrincipal = Context.User;
}
}



Regards
/Oscar
 
Oscar said:
Hi,

I am trying to figure out if I've got it wrong in the following code...
Could I lose the principal in the business layer(not HttpContext aware) if
ASP.NET switches executing thread?
I would presume that if a switch occur the principal/context would be
migrated to the new thread. Am I wrong??
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
GenericPrincipal principal = GetGenericPrincipal();


if (principal != null)
{
//Attach the new principal object to the current HttpContext object
Context.User = principal;

// Set the current thread's principal to the HttpContext principal
// This means that we can get the caller's principal in the business
layer
// without requiring knowledge of the HttpContext
System.Threading.Thread.CurrentPrincipal = Context.User;
}
}



Regards
/Oscar

According to the first few comments in this blog post, you won't lose
the CurrentPrincipal when ASP.NET switches threads:

http://piers7.blogspot.com/2005/11/threadstatic-callcontext-and_02.html
 
Back
Top