Back to issue with httpmodule

Discussion in 'ASP .Net' started by Gorge, Jun 7, 2009.

  1. Gorge

    Gorge Guest

    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();
    }

    }
    }
     
    Gorge, Jun 7, 2009
    #1
    1. Advertising

  2. Gorge

    Gorge Guest

    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();
    }

    }
    }
     
    Gorge, Jun 7, 2009
    #2
    1. Advertising

  3. Gorge

    Steven Cheng Guest

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

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

    --------------------
    >From: "Gorge" <>
    >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"]))
    >!= "NZ"
    > if (false)
    > {
    > context.Response.End();
    > }
    >
    > }
    >}
    >
    >
    >
     
    Steven Cheng, Jun 8, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hardy Wang
    Replies:
    3
    Views:
    4,662
    Hardy Wang
    Apr 22, 2004
  2. Joby
    Replies:
    0
    Views:
    1,662
  3. Robert Strickland

    Returning back to client from HttpModule

    Robert Strickland, Nov 12, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    372
    Robert Strickland
    Nov 12, 2004
  4. sylvia sil
    Replies:
    1
    Views:
    650
    Curt_C [MVP]
    Dec 29, 2004
  5. Max
    Replies:
    1
    Views:
    705
    John Timney \( MVP \)
    May 27, 2006
Loading...

Share This Page