【发布时间】: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 中找不到任何其他模块。当我删除
<validation validateIntegratedModeConfiguration="false" />时,我会得到与以前相同的行为。我怀疑我的问题与此有关:markcz.wordpress.com/2011/12/31/…
标签: asp.net .net iis-7 httpmodule