473,499 Members | 1,716 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What's the use of having HTTPModules ?

Hi... I'm learning about HTTPModules, so please allow me a beginner's
question.

I read that "you gain low-level access to the HTTPRequests and responses
processed by the ASP.NET framework". But... I'm confused, because in this
example they use "AuthenticateRequest" and "EndRequest"... (they write code
for redirecting the user to a login page if the address is like
"YourSite.com/contents.aspx" instead of
"YourSite.com/contents.aspx?Username=Blah").

Imports System.Web
Public Class AuthenticationModule
Implements IHttpModule

Public Sub Init(ByVal context As System.Web.HttpApplication) Implements
System.Web.IHttpModule.Init
AddHandler context.AuthenticateRequest, AddressOf Me.OnEnter
AddHandler context.EndRequest, AddressOf Me.OnLeave
End Sub

Public Sub Dispose() Implements System.Web.IHttpModule.Dispose
End Sub

Public Sub OnEnter(ByVal s As Object, ByVal e As EventArgs)
End Sub

Public Sub OnLeave(ByVal s As Object, ByVal e As EventArgs)
End Sub
End Class

But my question is this: why do I need to write a HTTPModule at all ?
Really... I can write the exact same code in Global.asax, right ? Or is it
*NOT* the same ?
Thank you,
Alex
Nov 19 '05 #1
3 3213
Hello msnews.microsoft.com,

Yes, you can place this code in Global.asax and it will work just fine.

However, let me give a little different example:

In a recent project, we handled our authentication code in global.asax. However,
requirements changed (don't they always) and we ended up needing our authentication
code to be different per client.

So, we could do one of two things:

1) Bastardize the global.asax file with an if statement that handled this
specific client or
2) Factor the authentication out of the global.asax and create an AuthenticationModule.

Now, for the one-off client we were able to create a completely separate
assembly that included this HttpModule and then for this client, we switch
the web.config to use this separate HttpModule.

This is truly the object oriented way of accomplishing the requirement.

I hope this clears things up a bit...

--
Matt Berther
http://www.mattberther.com
Hi... I'm learning about HTTPModules, so please allow me a beginner's
question.

I read that "you gain low-level access to the HTTPRequests and
responses processed by the ASP.NET framework". But... I'm confused,
because in this example they use "AuthenticateRequest" and
"EndRequest"... (they write code for redirecting the user to a login
page if the address is like "YourSite.com/contents.aspx" instead of
"YourSite.com/contents.aspx?Username=Blah").

Imports System.Web
Public Class AuthenticationModule
Implements IHttpModule
Public Sub Init(ByVal context As System.Web.HttpApplication)
Implements
System.Web.IHttpModule.Init
AddHandler context.AuthenticateRequest, AddressOf Me.OnEnter
AddHandler context.EndRequest, AddressOf Me.OnLeave
End Sub
Public Sub Dispose() Implements System.Web.IHttpModule.Dispose
End Sub
Public Sub OnEnter(ByVal s As Object, ByVal e As EventArgs)
End Sub
Public Sub OnLeave(ByVal s As Object, ByVal e As EventArgs)
End Sub
End Class
But my question is this: why do I need to write a HTTPModule at all ?
Really... I can write the exact same code in Global.asax, right ? Or
is it
*NOT* the same ?
Thank you,
Alex


Nov 19 '05 #2
Yes, you implement the same functionality from Global.asax, but,
HttpModules do offer additional flexibility. You can add and remove
modules from the processing pipeline just by modifying web.config, for
instance, and you can package your module in it's own assembly to use
across multiple projects if the need arises.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Tue, 1 Feb 2005 21:59:49 -0500, "msnews.microsoft.com"
<RE***********************@yahoo.com> wrote:
Hi... I'm learning about HTTPModules, so please allow me a beginner's
question.

I read that "you gain low-level access to the HTTPRequests and responses
processed by the ASP.NET framework". But... I'm confused, because in this
example they use "AuthenticateRequest" and "EndRequest"... (they write code
for redirecting the user to a login page if the address is like
"YourSite.com/contents.aspx" instead of
"YourSite.com/contents.aspx?Username=Blah").

Imports System.Web
Public Class AuthenticationModule
Implements IHttpModule

Public Sub Init(ByVal context As System.Web.HttpApplication) Implements
System.Web.IHttpModule.Init
AddHandler context.AuthenticateRequest, AddressOf Me.OnEnter
AddHandler context.EndRequest, AddressOf Me.OnLeave
End Sub

Public Sub Dispose() Implements System.Web.IHttpModule.Dispose
End Sub

Public Sub OnEnter(ByVal s As Object, ByVal e As EventArgs)
End Sub

Public Sub OnLeave(ByVal s As Object, ByVal e As EventArgs)
End Sub
End Class

But my question is this: why do I need to write a HTTPModule at all ?
Really... I can write the exact same code in Global.asax, right ? Or is it
*NOT* the same ?
Thank you,
Alex


Nov 19 '05 #3
Thank you, now it's much clearer - the purpose of doing something is almost
*never* explained in my book, just the technique :-(((

PS. Matt, see how lazy I am - I noticed that I have to change my account's
properties in outlook, but I haven't done it yet, therefore my alias is stil
msnews.microsoft.com :-))))
I'll change it right now !

Thank you both.
Alex.
Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2039
by: Aurel | last post by:
Hi, I have create a httpmodule but I have some problems. I can't access to the session. Even if I create my class like this public class myModule: IHttpModule, IRequiresSessionState {} ...
1
1130
by: Craig Neuwirt | last post by:
I have 2 HttpModules in my app. I use CallContext to set some thread specific info. Recently, I ran into a problem in which it appeared that the 2 HttpModules were executed in multiple threads. ...
0
1158
by: tshad | last post by:
I noticed in my Http modules that all the BeginRequests are handled for each Module before the EndRequests is handled (at least that seems to be the case). I have 2 HttpModules each with...
1
3267
by: Anonieko | last post by:
Global.asax? Use HttpModules Instead! In a previous post, I talked about HttpHandlers - an underused but incredibly useful feature of ASP.NET. Today I want to talk about HttpModules, which are...
1
1904
by: Asela Gunawardena | last post by:
Hi all, we have a webservice as a seperate virtual directory placed under a Web Site named GRSCS in IIS. Both are .NET applications and uses MS application blocks as the data layer. Recently an...
0
1176
by: andy6 via DotNetMonster.com | last post by:
I want to display a webpage. The user selects a value from a drop down. This value is put into a custom header and added to the http pipeline. Then the page is redirected in the EndRequest method...
2
2381
by: Mikael Syska | last post by:
Hi, Google gives alot of hits on httpmodules but I can't seem to find any useful on my problem ... I have a site where I'm using my own auth system ... ( guess it could be better but its...
3
2117
by: =?Utf-8?B?Tm9yZW1hYw==?= | last post by:
Hi, We are writing a Web SSO service for all of our websites through Forms Authentication. We also want to provide our websites with the ability to protect different parts of their website and...
1
3172
by: Samuel R. Neff | last post by:
We have a problem with Web.config inheritance in two of our applications. We have an old app which is poorly written and must be in the root of the server. We have a newer app which runs from a...
0
7128
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7006
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7169
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7215
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6892
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7385
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4917
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3088
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
661
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.