3

I'm trying to generate a 404 response for certain requests on all sites on a server based on the HttpRequest.UserAgent.

I've configured an IHttpModule in the server's global web.config containing the code below:

private void Application_BeginRequest(Object source, EventArgs e)
{
    HttpApplication application = (HttpApplication)source;
    HttpContext context = application.Context;

    if (isBot(context.Request.UserAgent))
    {
        System.Diagnostics.EventLog.WriteEntry(
            "Application",
            "\nBotRequestChecker -- request 404d\n" + "Url: " + 
              context.Request.Url + "\nUserAgent: " + context.Request.UserAgent,
            EventLogEntryType.Information);
        context.Response.StatusCode = 404;
        context.Response.StatusDescription = "Not Found";
        context.ApplicationInstance.CompleteRequest();
    }
}

Setting the User-Agent in a browser and visiting a page results in an entry in the event log, but the page is also returned, without a 404 status.

Any thoughts on what I'm missing?

Update:

The IHttpModule does seem to work if I add it to a single site (in the site's web.config), just not for all sites.

Update 2: The IHttpModule only works on a single site on an IIS7 server, not on IIS6.

Volo
  • 28,673
  • 12
  • 97
  • 125
mike
  • 7,137
  • 2
  • 23
  • 27

1 Answers1

0

Make sure that you've subscribed to the the BeginRequest event in your module's IHttpModule.Init() implementation. The events don't get auto-wired in IHttpModule implementations the way same they do in Global.asax.

I also missed the bit about the global web.config at first.

On a 64-bit server, you'll need to make sure you make the changes in both the 32- and 64-bit configurations (depending on the bitness that your sites are running in) in all ASP.NET versions you need to support:

%windows%\Microsoft.NET\Framework\v2.0.50727\CONFIG\web.config
%windows%\Microsoft.NET\Framework64\v2.0.50727\CONFIG\web.config

%windows%\Microsoft.NET\Framework\v4.0.30319\CONFIG\web.config
%windows%\Microsoft.NET\Framework64\v4.0.30319\CONFIG\web.config

If you're targeting both IIS6 and IIS7, you'll need to make sure the module is referenced in both the <system.web>/<httpModules> element for IIS6 and the <system.webServer>/<modules> element for IIS7.

Daniel Schaffer
  • 56,753
  • 31
  • 116
  • 165
  • That's what I tried originally, with the same non-404 result. – mike Mar 31 '11 at 18:14
  • Edited my answer to remove that... based on this: http://forums.asp.net/p/1148761/1867921.aspx it looks like you're doing the right thing. Can you confirm that the code is actually getting hit? – Daniel Schaffer Mar 31 '11 at 18:16
  • Also, are you actually testing this on an IIS 6 server or are you running in the VS dev server (Cassini) for now? – Daniel Schaffer Mar 31 '11 at 18:19
  • Yep -- there's event log entries being posted fro just above when the StatusCode is set. – mike Mar 31 '11 at 18:21
  • So, regarding your addendum, are the log entries getting posted when the module is configured only at the machine level and not at the site level? – Daniel Schaffer Mar 31 '11 at 18:25
  • In both cases. Your question made me realize that my "working" case was on an IIS7 server. – mike Mar 31 '11 at 19:19
  • Ah. In that case, you'll need to add module references in the `/` element as well. – Daniel Schaffer Mar 31 '11 at 20:39