problem with httpmodule

G

Gorge

I have a http module that works fine. What it does, any incoming request it
checks against a ip database and filters it by country. It rejects
connectections if they are not from my country. This is all good, but it
only seems to work for the .Aspx pages. I need it to work for images. So,
under the configuration of the site, I added the extenstion .jpg and .jpeg
to point to c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll.

Now when viewing the image I get a link... so the code isn't work at all
for images if I point it to this dll. Is this the correct way I should be
doing it?

I'm quite lost. I thought the httpmodule would intercept any request to the
website.

Thanks



Code


a.. public class CountryFilter : IHttpModule
a.. {
a.. public void Dispose()
a.. {
a.. }
a..
a.. public void Init(HttpApplication context)
a.. {
a.. context.BeginRequest += new EventHandler(context_BeginRequest);
a.. }
a..
a.. private void context_BeginRequest(object sender, EventArgs e)
a.. {
a..
a.. HttpApplication app = (HttpApplication)sender;
a.. HttpContext context = ((HttpApplication)sender).Context;
a..
a.. // start checking country code
a.. GeoIPCountry cl = new GeoIPCountry(@"c:\\GeoIP.dat");
a..
a.. if
(cl.GetCountryCode(IPAddress.Parse(app.Context.Request.ServerVariables["REMOTE_ADDR"]))
!= "NZ")
a.. {
a.. context.Response.StatusCode = 403;
a.. context.Response.End();
a.. }
a.. }
a.. }
 
S

Steven Cheng

Hi Gorge,

Are you using IIS 6 or any newer version of IIS?

For IIS6 or former version, only those ASP.NET specific document
extensions (such as .aspx, .ashx, .ascx ....) are forwarded to ASP.NET
runtime for processing. Those static content such as txt, htm, css or image
files are directly served by IIS server(for performance consideration).

If you want to let ASP.NET runtime process them, you can manually modify
the extension mapping for your ASP.NET web application's virtual directory.
Here is a tech article discusses on this:

#Configure IIS for Wildcard Extensions in ASP.NET
http://professionalaspnet.com/archive/2007/07/27/Configure-IIS-for-Wildcard-
Extensions-in-ASP.NET.aspx

If you have anything unclear, please feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
From: "Gorge" <[email protected]>
Subject: problem with httpmodule
Date: Thu, 4 Jun 2009 13:27:48 +1200
Now when viewing the image I get a link... so the code isn't work at all
for images if I point it to this dll. Is this the correct way I should be
doing it?

I'm quite lost. I thought the httpmodule would intercept any request to the
website.

Thanks



Code


a.. public class CountryFilter : IHttpModule
a.. {
a.. public void Dispose()
a.. {
a.. }
a..
a.. public void Init(HttpApplication context)
a.. {
a.. context.BeginRequest += new EventHandler(context_BeginRequest);
a.. }
a..
a.. private void context_BeginRequest(object sender, EventArgs e)
a.. {
a..
a.. HttpApplication app = (HttpApplication)sender;
a.. HttpContext context = ((HttpApplication)sender).Context;
a..
a.. // start checking country code
a.. GeoIPCountry cl = new GeoIPCountry(@"c:\\GeoIP.dat");
a..
a.. if
(cl.GetCountryCode(IPAddress.Parse(app.Context.Request.ServerVariables["REM OTE_ADDR"]))
!= "NZ")
a.. {
a.. context.Response.StatusCode = 403;
a.. context.Response.End();
a.. }
a.. }
a.. }
 
G

Gorge

Ok, I actually think I need a httphandler... Are you sure?

Angel J. Hernández M. said:
Hi there George,

If I'm not mistaken you need to implement an HttpHandler instead of a
HttpModule

http://msdn.microsoft.com/en-us/library/bb398986.aspx
http://support.microsoft.com/kb/307985


Regards,



--
Angel J. Hernández M
MCP,MCAD,MCSD,MCDBA
Microsoft MVP
http://www.ajhsis.com
http://msmvps.com/blogs/angelhernandez



Gorge said:
I have a http module that works fine. What it does, any incoming request
it checks against a ip database and filters it by country. It rejects
connectections if they are not from my country. This is all good, but it
only seems to work for the .Aspx pages. I need it to work for images. So,
under the configuration of the site, I added the extenstion .jpg and
.jpeg to point to
c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll.

Now when viewing the image I get a link... so the code isn't work at all
for images if I point it to this dll. Is this the correct way I should be
doing it?

I'm quite lost. I thought the httpmodule would intercept any request to
the website.

Thanks



Code


a.. public class CountryFilter : IHttpModule
a.. {
a.. public void Dispose()
a.. {
a.. }
a..
a.. public void Init(HttpApplication context)
a.. {
a.. context.BeginRequest += new
EventHandler(context_BeginRequest);
a.. }
a..
a.. private void context_BeginRequest(object sender, EventArgs e)
a.. {
a..
a.. HttpApplication app = (HttpApplication)sender;
a.. HttpContext context = ((HttpApplication)sender).Context;
a..
a.. // start checking country code
a.. GeoIPCountry cl = new GeoIPCountry(@"c:\\GeoIP.dat");
a..
a.. if
(cl.GetCountryCode(IPAddress.Parse(app.Context.Request.ServerVariables["REMOTE_ADDR"]))
!= "NZ")
a.. {
a.. context.Response.StatusCode = 403;
a.. context.Response.End();
a.. }
a.. }
a.. }
 
G

Gorge

"Steven Cheng" said:
Hi Gorge,

Are you using IIS 6 or any newer version of IIS?

Using IIS 6
For IIS6 or former version, only those ASP.NET specific document
extensions (such as .aspx, .ashx, .ascx ....) are forwarded to ASP.NET
runtime for processing. Those static content such as txt, htm, css or
image
files are directly served by IIS server(for performance consideration).

If you want to let ASP.NET runtime process them, you can manually modify
the extension mapping for your ASP.NET web application's virtual
directory.
Here is a tech article discusses on this:

#Configure IIS for Wildcard Extensions in ASP.NET
http://professionalaspnet.com/archive/2007/07/27/Configure-IIS-for-Wildcard-
Extensions-in-ASP.NET.aspx

If you have anything unclear, please feel free to post here.

I've done that. Seems to be working... BUT when displaying images, it just
displays the url, no image. What do I need to change to pass the request
back to IIS to process the image?
Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support
Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.

--------------------
From: "Gorge" <[email protected]>
Subject: problem with httpmodule
Date: Thu, 4 Jun 2009 13:27:48 +1200
Now when viewing the image I get a link... so the code isn't work at all
for images if I point it to this dll. Is this the correct way I should be
doing it?

I'm quite lost. I thought the httpmodule would intercept any request to the
website.

Thanks



Code


a.. public class CountryFilter : IHttpModule
a.. {
a.. public void Dispose()
a.. {
a.. }
a..
a.. public void Init(HttpApplication context)
a.. {
a.. context.BeginRequest += new
EventHandler(context_BeginRequest);
a.. }
a..
a.. private void context_BeginRequest(object sender, EventArgs e)
a.. {
a..
a.. HttpApplication app = (HttpApplication)sender;
a.. HttpContext context = ((HttpApplication)sender).Context;
a..
a.. // start checking country code
a.. GeoIPCountry cl = new GeoIPCountry(@"c:\\GeoIP.dat");
a..
a.. if
(cl.GetCountryCode(IPAddress.Parse(app.Context.Request.ServerVariables["REM OTE_ADDR"]))
!= "NZ")
a.. {
a.. context.Response.StatusCode = 403;
a.. context.Response.End();
a.. }
a.. }
a.. }
 
S

Steven Cheng

Hi Gorge,

I'm curious at how your httpmodule is written, would you show some of its
processing code logic?

Generally, after you configured the certain static files to be served by
ASP.NET runtime, by default they'll be handled by a static file handler.

If you want to switch back to let IIS process the request, just remove the
extension mapping you previously added.

BTW, a better solution is to write an IIS isapi filter to do the IP
restriction, it can handle all the requests no matter whether they're
ASP.NET specific or not. But, the problem is that we cannot use .NET
managed code to write IIS isapi filter, they need to be created via C++ .

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.


--------------------
 
G

Gorge

The complete code is here:

http://www.pastebin.ca/1447261

Basically, what I am trying to achieve is when a user from another country
tries to get an image, the connection is blocked. However, if the person is
from a specific country, serve the image.

Thanks,
 
J

Jeff Johnson

Ok, I actually think I need a httphandler... Are you sure?

This question isn't C#-specific, it's really about ASP.NET. You'd be better
served by re-posting in an ASP.NET group.
 
S

Steven Cheng

Hi Gorge,

I've had a look at the httpmodule code you provided, seems it is ok. Your
code logic is simply block any request(send 403) if it is from a certain
forbidden country and just do nothing if the user is allowed, correct?

One thing you can still check is open the global machine level web.config
file on your server machine(with IIS and ASP.NET), inspect the
<httphandlers> section in it and make sure those image file requests will
be able to get handled by the "System.Web.StaticFileHandler" according to
the handler configuration there. The machine level config file (for .NET
2.0 and 3.X ) is at the following location:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG


Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.


--------------------
<[email protected]>
 

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

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top