Back to issue with httpmodule

G

Gorge

Hi Guys,

Still have a problem with httpmodule.

What I am trying to do is intercept all requests to my web app (images, html
pages etc linked via asp net dll). This works fine on my test machine.

The problem I am trying to solve:

A request comes in for an image. I check the IP address of incoming request.
If IP is not from my country, kill connection. This part works fine.
However, if the IP is from my country, I want normally processing to
contune. This doesn't seem to work. When I view the image, the browser says
content image/jpeg and all I have is a link of the url I am trying to
request with a 0 byte size.

So, I think I have something slightly wrong here. i have debugged it locally
and it seems fine, what am I doing wrong?

Below is my httpmodule (for a better view http://www.pastebin.ca/1450291):

public class CountryFilter : IHttpModule
{
public void Dispose()
{
}

public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}

private void context_BeginRequest(object sender, EventArgs e)
{

HttpApplication app = (HttpApplication)sender;
HttpContext context = ((HttpApplication)sender).Context;

// start checking country code
GeoIPCountry cl = new GeoIPCountry(@"e:\GeoIP.dat");
context.Response.Write(IPAddress.Parse(app.Context.Request.ServerVariables["REMOTE_ADDR"]));
//cl.GetCountryCode(IPAddress.Parse(app.Context.Request.ServerVariables["REMOTE_ADDR"]))
!= "NZ"
if (false)
{
context.Response.End();
}

}
}
 
G

Gorge

Wrong code

public class CountryFilter : IHttpModule
{
public void Dispose()
{
}

public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}

private void context_BeginRequest(object sender, EventArgs e)
{

HttpApplication app = (HttpApplication)sender;
HttpContext context = ((HttpApplication)sender).Context;

// start checking country code
GeoIPCountry cl = new
GeoIPCountry(@"c:\inetpub\wwwroot\iforce.co.nz\GeoIP.dat");
context.Response.Write(IPAddress.Parse(app.Context.Request.ServerVariables["REMOTE_ADDR"]));

if
(cl.GetCountryCode(IPAddress.Parse(app.Context.Request.ServerVariables["REMOTE_ADDR"]))
!= "NZ")
{
context.Response.End();
}

}
}
 
S

Steven Cheng

Hi Gorge,

I've looked at the code you provided from the former thread and the code
logic should be ok. One thing I feel a bit unexpected is that you said the
request of image will display the path and name of the image file instead
of the image itself(after you've change the IIS extension mapping to
ASP.Net isapi ).

I've posted in the former thread and suggest you also check the global
web.config file to ensure that the image requests can correctly get handled
by the "StaticFileHandler". Here is my reply content in the former thread:

===============
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.

--------------------
From: "Gorge" <[email protected]>
Subject: Back to issue with httpmodule
Date: Sun, 7 Jun 2009 14:18:39 +1200
Hi Guys,

Still have a problem with httpmodule.

What I am trying to do is intercept all requests to my web app (images, html
pages etc linked via asp net dll). This works fine on my test machine.

The problem I am trying to solve:

A request comes in for an image. I check the IP address of incoming request.
If IP is not from my country, kill connection. This part works fine.
However, if the IP is from my country, I want normally processing to
contune. This doesn't seem to work. When I view the image, the browser says
content image/jpeg and all I have is a link of the url I am trying to
request with a 0 byte size.

So, I think I have something slightly wrong here. i have debugged it locally
and it seems fine, what am I doing wrong?

Below is my httpmodule (for a better view http://www.pastebin.ca/1450291):

public class CountryFilter : IHttpModule
{
public void Dispose()
{
}

public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}

private void context_BeginRequest(object sender, EventArgs e)
{

HttpApplication app = (HttpApplication)sender;
HttpContext context = ((HttpApplication)sender).Context;

// start checking country code
GeoIPCountry cl = new GeoIPCountry(@"e:\GeoIP.dat");
context.Response.Write(IPAddress.Parse(app.Context.Request.ServerVariables["
REMOTE_ADDR"]));
//cl.GetCountryCode(IPAddress.Parse(app.Context.Request.ServerVariables["REM
OTE_ADDR"]))
 

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