gpt4 book ai didi

asp.net - 自定义 HttpModule 在 IIS7 集成中工作,但不是经典模式

转载 作者:行者123 更新时间:2023-12-02 11:17:19 25 4
gpt4 key购买 nike

我有一个用 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,否则请求将直接发送到页面。

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

所以:

  • 集成模式:http://www.yoursite.com/myfile.html首先检查您在 http 模块和 global.asax 中配置的 http 模块和路由(您的 Request.Url 应该有上面的 URL)
  • 经典模式:http://www.yoursite.com/myfile.html检查是否确实有一个名为 myfile.html 的文件,如果没有,则转到 404 页面。除非你有一个自定义的 URLRewrite 模块。

  • 希望这对你有帮助。

    关于asp.net - 自定义 HttpModule 在 IIS7 集成中工作,但不是经典模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21466318/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com