繁体   English   中英

自定义HttpModule可在IIS7中集成,但不是经典模式

[英]Custom HttpModule working in IIS7 integrated, but not classic mode

我有一个用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应用程序池允许任何请求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模块。

希望这对您有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM