Thread agility(switching) and ASP.NET

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
 
G

Göran Andersson

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top