Application_AuthenticateRequest

G

Guest

Hello All,

Does the Application_AuthenticateRequest event get fired for every request
made to the application? According to my understanding, YES it does get fired
for each and every request. Please correct me if I am wrong.

But does the event get fired multiple times for the same request? I do not
understand why in my application is it being called multiple times for the
same request. Every time it is retrieving the role information for the user
and associating that with the principal.

I would like to get recommendations/pointers from you folks as to what I am
doing wrong and also best practices. Below please find the code for this
event.

if ( HttpContext.Current.User != null )
{

string PrincipalIdentityName =
HttpContext.Current.User.Identity.Name.ToLower();

System.Web.Security.FormsIdentity id;
id =
(System.Web.Security.FormsIdentity)HttpContext.Current.User.Identity;

//Added on October 26, 2006
string FormsIdentityName = id.Name.ToLower();

String[] myRoles = new String[10];
int i = 0;

if(HttpContext.Current.User.Identity.Name.ToUpper() ==
"SuperAdmin".ToUpper())
{
myRoles = "PowerUser";
i++;
}

if(objRolesservice.IsRegionalUser(PrincipalIdentityName))
{
myRoles = "RegionalUser";
i++;
}

HttpContext.Current.User = new
System.Security.Principal.GenericPrincipal(id, myRoles);

}


The above loop is being executed multiple times for the same request. I
would like to optimize it. All I am doing in the loop is retrieving his role
information.

At the beginning of the loop I have a condition which is
if ( HttpContext.Current.User != null ) do I need to change this condition
to something else so that after role information is associated to principal,
this loop is not executed multiple times for the same request?

Thanks for all your pointers!!!!
 
L

Laurent Bugnion

Hi,
Hello All,

Does the Application_AuthenticateRequest event get fired for every request
made to the application? According to my understanding, YES it does get fired
for each and every request. Please correct me if I am wrong.

But does the event get fired multiple times for the same request? I do not
understand why in my application is it being called multiple times for the
same request. Every time it is retrieving the role information for the user
and associating that with the principal.

Note that one postback (or simply loading one page) may create many
requests. First one request is sent for the main (HTML, ASPX...) page.
Then the browser parses the page and for each external object found
(included JavaScript files, CSS files, images, other OBJECTs, ...) a new
request will be sent (except if a valid cached version is found).

If you want to see for sure what is happening on the line, I recommend
the tool Fiddler (http://www.fiddlertool.com/fiddler/) which will show
each request. That should help finding the "problem" (if there is one)
or to improve your design if needed.

HTH,
Laurent
 

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

Members online

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top