简体   繁体   English

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

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

I have an application that is written in asp.net and I have some legacy classic asp pages integrated into the site. 我有一个用asp.net编写的应用程序,并且将一些传统的经典asp页面集成到该站点中。 The site uses Windows authentication. 该站点使用Windows身份验证。 Because I cannot manage .asp pages with roles, I've written a custom HttpModule to check if a user has permissions to view those pages, otherwise it redirects to an "access denied" page. 因为我无法使用角色管理.asp页面,所以我编写了一个自定义HttpModule来检查用户是否具有查看这些页面的权限,否则它将重定向到“拒绝访问”页面。 The main issue is that the application needs to run in "classic mode" on IIS7. 主要问题是应用程序需要在IIS7上以“经典模式”运行。 My module works in integrated mode, but not in classic mode. 我的模块在集成模式下工作,但在经典模式下不工作。 Is there any reason this code shouldn't work in classic mode as well? 出于某种原因,该代码也不应在经典模式下运行吗? Thanks in advance. 提前致谢。

Here is the code for the module, it's pretty simple: 这是该模块的代码,非常简单:

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

Here is the setting in web.config for classic mode (not working): 这是web.config中经典模式的设置(不起作用):

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

And the setting for integrated mode (working): 以及集成模式的设置(工作):

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

In integrated mode, IIS App pools allow Any request URL to come in to the ASP.NET ISAPI, however, in classic mode, you would need a third-party ISAPI or the request will be sent directly to the page. 在集成模式下,IIS应用程序池允许任何请求URL进入ASP.NET ISAPI,但是,在经典模式下,您将需要第三方ISAPI或将请求直接发送到页面。

In integrated, the module gets checked FIRST before the actual request content. 在集成中,该模块在实际请求内容之前首先被检查。

SO: 所以:

  1. Integrated Mode: http://www.yoursite.com/myfile.html first goes through your http modules and routes configured in http modules and global.asax (your Request.Url should have the URL above) 集成模式: http : //www.yoursite.com/myfile.html首先浏览您的http模块以及在http模块和global.asax中配置的路由(您的Request.Url应该具有上面的URL)

  2. Classic Mode: http://www.yoursite.com/myfile.html checks to see if there is actually a file called myfile.html, and if not, then it goes to a 404 page. 经典模式: http : //www.yoursite.com/myfile.html检查是否确实存在名为myfile.html的文件,如果没有,则转到404页。 UNLESS, again, you have a custom URLRewrite module. 再次,除非您有一个自定义URLRewrite模块。

Hope this helps ya. 希望这对您有帮助。

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

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