By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,837 Members | 1,648 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,837 IT Pros & Developers. It's quick & easy.

HTTPModules vs Global.asax

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.