【问题标题】:Custom HttpModule working in IIS7 integrated, but not classic mode自定义 HttpModule 在 IIS7 集成中工作,但不是经典模式
【发布时间】:2014-01-30 19:59:05
【问题描述】:

我有一个用 asp.net 编写的应用程序,并且我有一些旧的经典 asp 页面集成到该站点中。该站点使用 Windows 身份验证。因为我无法使用角色管理 .asp 页面,所以我编写了一个自定义 HttpModule 来检查用户是否有权查看这些页面,否则它会重定向到“拒绝访问”页面。主要问题是应用程序需要在 IIS7 上以“经典模式”运行。我的模块在集成模式下工作,但不是在经典模式下。这段代码是否也不能在经典模式下工作?提前致谢。

这是模块的代码,很简单:

public class MyModule: IHttpModule
{
    public void Init(HttpApplication application)
    {
        application.PostAuthenticateRequest += new EventHandler(Application_PostAuthenticateRequest);
    }
    void Application_PostAuthenticateRequest(object source, EventArgs e)
    {
        HttpApplication app = (HttpApplication)source;
        HttpContext context = ((HttpApplication)source).Context;

        if (context.Request.RawUrl.Contains("/protected-subfolder/"))
        {
            // gets user from windows authentication
            string currentUser = Convert.ToString(context.User.Identity.Name);

            if (!isAdmin(currentUser))
            {
                //deny access
                (context.Response).Redirect(VirtualPathUtility.ToAbsolute("~/AccessDenied.aspx"));
            }
        }
    }

public void Dispose(){ }

这是 web.config 中经典模式的设置(不工作):

<configuration>
    <system.web>
        <httpModules>
            <add name="MyModule" type="MyModule" />
        </httpModules>
    </system.web>
</configuration>

以及集成模式的设置(工作):

<configuration>
    <system.webServer>
        <modules>
            <add name="MyModule" type="MyModule"/>
        </modules>
        <validation validateIntegratedModeConfiguration="false" />
    </system.webServer>
</configuration>

【问题讨论】:

  • 启用失败的请求跟踪,您将更好地了解正在发生的事情。见iis.net/learn/troubleshoot/using-failed-request-tracing/…
  • 我实际上启用了失败的请求跟踪 - 问题是请求没有失败,但让用户直接进入他/她不应该访问的页面,这似乎暗示 httpmodule 根本没有运行。
  • 当你像这样添加一个 httpModule 时,它​​也是最后一个要执行的模块。这可能是个问题。您可以查看从系统范围的 web.config 继承的模块,然后在配置中查看 ,确保您的模块在列表中的第一位,然后添加从系统 web.config 收集的模块。
  • 另外,查看 asp.net 2.0 的重大更改,特别是关于 - 您在集成模式下禁用配置验证正在回避这一点。集成管道中的用户可能不是您认为的用户。
  • 奇怪的是,这是 web.config 中唯一的模块——我在系统范围的 web.config 中找不到任何其他模块。当我删除 &lt;validation validateIntegratedModeConfiguration="false" /&gt; 时,我会得到与以前相同的行为。我怀疑我的问题与此有关:markcz.wordpress.com/2011/12/31/…

标签: asp.net .net iis-7 httpmodule


【解决方案1】:

在集成模式下,IIS 应用程序池允许 任何 请求 URL 进入 ASP.NET ISAPI,但是,在经典模式下,您需要第三方 ISAPI,否则请求将被直接发送到页面。

在集成中,模块在实际请求内容之前首先被检查。

所以:

  1. 集成模式:http://www.yoursite.com/myfile.html 首先通过你的 http 模块和在 http 模块和 global.asax 中配置的路由(你的 Request.Url 应该有上面的 URL)

  2. 经典模式:http://www.yoursite.com/myfile.html 检查是否确实存在名为 myfile.html 的文件,如果没有,则转到 404 页面。除非你有一个自定义的 URLRewrite 模块。

希望这对您有所帮助。

【讨论】:

    猜你喜欢
    • 2013-12-13
    • 2018-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-17
    • 1970-01-01
    相关资源
    最近更新 更多