如何从全局 web.config 中配置的 IHttpModule 生成 404 响应?

发布于 2024-10-28 05:11:20 字数 1383 浏览 1 评论 0原文

我正在尝试根据服务器上所有站点上的某些请求生成 404 响应 HttpRequest.UserAgent

我已经在服务器的全局 web.config 中配置了一个 IHttpModule,其中包含以下代码:

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

在浏览器中设置 User-Agent 并访问页面会在事件日志中生成一个条目,但也会返回该页面,但没有 404 状态。

对我所缺少的有什么想法吗?

更新:

如果我将IHttpModule添加到单个站点(在站点的web.config中),IHttpModule似乎确实可以工作,但不适用于所有人网站。

更新2: IHttpModule 仅适用于 IIS7 服务器上的单个站点,不适用于 IIS6

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

椵侞 2024-11-04 05:11:20

确保您已订阅模块的 IHttpModule.Init() 实现中的 BeginRequest 事件。这些事件在 IHttpModule 实现中不会像在 Global.asax 中那样自动连接。

我一开始也错过了有关全局 web.config 的部分。

在 64 位服务器上,您需要确保在需要支持的所有 ASP.NET 版本中对 32 位和 64 位配置(取决于您的站点运行的位数)进行更改:

%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

如果您同时针对 IIS6 和 IIS7,则需要确保在 / 元素中引用该模块IIS6 和 IIS7 的 / 元素。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文