problem with httpmodule

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

  1. Gorge

    Gorge Guest

    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.. }
    Gorge, Jun 4, 2009
    #1
    1. Advertising

  2. Gorge

    Steven Cheng Guest

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

    ==================================================
    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" <>
    >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.. }
    >
    >
    >
    Steven Cheng, Jun 4, 2009
    #2
    1. Advertising

  3. Gorge

    Gorge Guest

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

    "Angel J. Hernández M." <> wrote in message
    news:...
    > 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" <> wrote in message
    > news:...
    >> 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.. }
    >>
    Gorge, Jun 4, 2009
    #3
  4. Gorge

    Gorge Guest

    ""Steven Cheng"" <> wrote in message
    news:...
    > 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:
    > .
    >
    > ==================================================
    > 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" <>
    >>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.. }
    >>
    >>
    >>

    >
    Gorge, Jun 4, 2009
    #4
  5. Gorge

    Steven Cheng Guest

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

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


    --------------------
    >From: "Gorge" <>
    >References: <>

    <>
    >Subject: Re: problem with httpmodule
    >Date: Thu, 4 Jun 2009 17:50:16 +1200


    >
    >""Steven Cheng"" <> wrote in message
    >news:...
    >> 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.

    >
    Steven Cheng, Jun 4, 2009
    #5
  6. Gorge

    Gorge Guest

    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,

    ""Steven Cheng"" <> wrote in message
    news:UV%...
    > 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:
    > .
    >
    > ==================================================
    > Get notification to my posts through email? Please refer to
    > http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
    >
    >
    > --------------------
    >>From: "Gorge" <>
    >>References: <>

    > <>
    >>Subject: Re: problem with httpmodule
    >>Date: Thu, 4 Jun 2009 17:50:16 +1200

    >
    >>
    >>""Steven Cheng"" <> wrote in message
    >>news:...
    >>> 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.

    >>

    >
    Gorge, Jun 4, 2009
    #6
  7. Gorge

    Jeff Johnson Guest

    "Gorge" <> wrote in message
    news:e1u%...

    > 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.
    Jeff Johnson, Jun 4, 2009
    #7
  8. Gorge

    Steven Cheng Guest

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

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


    --------------------
    >From: "Gorge" <>
    >References: <>

    <>
    <#>
    <UV#>
    >Subject: Re: problem with httpmodule
    >Date: Thu, 4 Jun 2009 19:58:23 +1200


    >
    >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,
    >
    >""Steven Cheng"" <> wrote in message
    >news:UV%...
    >> 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:
    >> .
    >>
    >> ==================================================
    >> Get notification to my posts through email? Please refer to
    >>

    http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
    >>
    >>
    >> --------------------
    >>>From: "Gorge" <>
    >>>References: <>

    >> <>
    >>>Subject: Re: problem with httpmodule
    >>>Date: Thu, 4 Jun 2009 17:50:16 +1200

    >>
    >>>
    >>>""Steven Cheng"" <> wrote in message
    >>>news:...
    >>>> 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.
    >>>

    >>

    >
    >
    >
    Steven Cheng, Jun 8, 2009
    #8
    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

    Some problem with HttpModule

    Hardy Wang, Jun 7, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    379
    Hardy Wang
    Jun 7, 2005
  2. Andreas Zita

    Annoying HttpModule/QueryString Problem

    Andreas Zita, Nov 29, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    521
    Andreas Zita
    Nov 29, 2005
  3. Daniel Fisher\(lennybacon\)

    Re: Annoying HttpModule/QueryString Problem

    Daniel Fisher\(lennybacon\), Nov 30, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    591
    Daniel Fisher\(lennybacon\)
    Nov 30, 2005
  4. Thomas
    Replies:
    0
    Views:
    1,311
    Thomas
    Jun 11, 2006
  5. Greg Hurlman
    Replies:
    1
    Views:
    1,982
    Shawn Wildermuth
    Jul 16, 2006
Loading...

Share This Page