469,928 Members | 1,876 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,928 developers. It's quick & easy.

HTTPModules vs Global.asax

I am setting up Authentication that I want to put in multiple Web Sites on
my server.

I found a good article on this and am looking at moving my code from my
Global.asax file to an HTTP Module. This works fine.

But I was curious about what would happen if I left the Global.asax code in
(which is identical to the HTTPModule code) as well as added the HTTPModule.
Both call the same event and both sets of code is executed.

It seems to do the HTTPModule first and then does the Global.asax code.

I am going to take the Global.asax code out, but am curious as to what the
order is that these things are called. Why is the HTTPModule called first
and then the Global.asax?

Here is the code from the HttpModule:
***********************************************
namespace AuthModule
{
public class SetIdentity: IHttpModule
{
public SetIdentity()
{
}

public void Init(HttpApplication context)
{
context.AuthenticateRequest +=
(new EventHandler(this.Application_AuthenticateRequest) );
}

private void Application_AuthenticateRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;

// Get the authentication cookie
string cookieName = FormsAuthentication.FormsCookieName;
HttpCookie authCookie = context.Request.Cookies[cookieName];
if(authCookie==null)
return;

// Get the authentication ticket
// and rebuild the principal & identity
FormsAuthenticationTicket authTicket =
FormsAuthentication.Decrypt(authCookie.Value);
string[] roles = authTicket.UserData.Split(new Char [] {'|'});
GenericIdentity userIdentity = new GenericIdentity(authTicket.Name);
GenericPrincipal userPrincipal =
new GenericPrincipal(userIdentity, roles);
context.User = userPrincipal;
}

public void Dispose()
{
}
}
}
************************************************** ***********

Thanks,

Tom

May 3 '06 #1
2 3762
You use the HTTPModule want to intercept the call before it gets to your
application.???!!
That is why the HTTPModule is executed first.

SA
"tshad" <ts**********@ftsolutions.com> wrote in message
news:Oq**************@TK2MSFTNGP03.phx.gbl...
I am setting up Authentication that I want to put in multiple Web Sites on
my server.

I found a good article on this and am looking at moving my code from my
Global.asax file to an HTTP Module. This works fine.

But I was curious about what would happen if I left the Global.asax code
in
(which is identical to the HTTPModule code) as well as added the
HTTPModule.
Both call the same event and both sets of code is executed.

It seems to do the HTTPModule first and then does the Global.asax code.

I am going to take the Global.asax code out, but am curious as to what the
order is that these things are called. Why is the HTTPModule called first
and then the Global.asax?

Here is the code from the HttpModule:
***********************************************
namespace AuthModule
{
public class SetIdentity: IHttpModule
{
public SetIdentity()
{
}

public void Init(HttpApplication context)
{
context.AuthenticateRequest +=
(new EventHandler(this.Application_AuthenticateRequest) );
}

private void Application_AuthenticateRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;

// Get the authentication cookie
string cookieName = FormsAuthentication.FormsCookieName;
HttpCookie authCookie = context.Request.Cookies[cookieName];
if(authCookie==null)
return;

// Get the authentication ticket
// and rebuild the principal & identity
FormsAuthenticationTicket authTicket =
FormsAuthentication.Decrypt(authCookie.Value);
string[] roles = authTicket.UserData.Split(new Char [] {'|'});
GenericIdentity userIdentity = new GenericIdentity(authTicket.Name);
GenericPrincipal userPrincipal =
new GenericPrincipal(userIdentity, roles);
context.User = userPrincipal;
}

public void Dispose()
{
}
}
}
************************************************** ***********

Thanks,

Tom

May 3 '06 #2
I use HttpModules because they are far more reusable. For example, I have
an Fuel.Web.ErrorModule.

which hooks into application.Error and does some error logging (via
log4net). I've reused the module across countless applications.

Similarly, I have a Fuel.Web.LocalizationModule which is at the core of the
localization stuff i use
(http://openmymind.net/index.aspx?doc...=4#urlrewrite). Again, all
because they are highly reusable.

Karl
--
http://www.openmymind.net/
http://www.fuelindustries.com/
"MSDN" <sq**********@hotmail.com> wrote in message
news:Om**************@TK2MSFTNGP04.phx.gbl...
You use the HTTPModule want to intercept the call before it gets to your
application.???!!
That is why the HTTPModule is executed first.

SA
"tshad" <ts**********@ftsolutions.com> wrote in message
news:Oq**************@TK2MSFTNGP03.phx.gbl...
I am setting up Authentication that I want to put in multiple Web Sites on
my server.

I found a good article on this and am looking at moving my code from my
Global.asax file to an HTTP Module. This works fine.

But I was curious about what would happen if I left the Global.asax code
in
(which is identical to the HTTPModule code) as well as added the
HTTPModule.
Both call the same event and both sets of code is executed.

It seems to do the HTTPModule first and then does the Global.asax code.

I am going to take the Global.asax code out, but am curious as to what
the
order is that these things are called. Why is the HTTPModule called
first
and then the Global.asax?

Here is the code from the HttpModule:
***********************************************
namespace AuthModule
{
public class SetIdentity: IHttpModule
{
public SetIdentity()
{
}

public void Init(HttpApplication context)
{
context.AuthenticateRequest +=
(new EventHandler(this.Application_AuthenticateRequest) );
}

private void Application_AuthenticateRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;

// Get the authentication cookie
string cookieName = FormsAuthentication.FormsCookieName;
HttpCookie authCookie = context.Request.Cookies[cookieName];
if(authCookie==null)
return;

// Get the authentication ticket
// and rebuild the principal & identity
FormsAuthenticationTicket authTicket =
FormsAuthentication.Decrypt(authCookie.Value);
string[] roles = authTicket.UserData.Split(new Char [] {'|'});
GenericIdentity userIdentity = new GenericIdentity(authTicket.Name);
GenericPrincipal userPrincipal =
new GenericPrincipal(userIdentity, roles);
context.User = userPrincipal;
}

public void Dispose()
{
}
}
}
************************************************** ***********

Thanks,

Tom


May 3 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Ian Turner | last post: by
2 posts views Thread by Aurel | last post: by
3 posts views Thread by msnews.microsoft.com | last post: by
1 post views Thread by Anonieko | last post: by
reply views Thread by GoogleNewsReaderMan | last post: by
8 posts views Thread by Rob T | last post: by
1 post views Thread by Samuel R. Neff | last post: by
2 posts views Thread by DamienS | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.