【问题标题】:IIS 7 with URL Rewrite Module 2.0 - setting 401 status codes and the ReturnUrl带有 URL 重写模块 2.0 的 IIS 7 - 设置 401 状态代码和 ReturnUrl
【发布时间】:2023-04-03 22:20:02
【问题描述】:

我有一个安装在 IIS 7 上的网站,并安装了 URL Rewrite module 2.0。它由查看 URL 的内容管理系统运行,如果当前用户无权查看页面,则返回 401 错误。这由 ASP.NET URL 授权模块获取,然后将页面跳转到 web.config 文件中指定的 loginUrl 页面(表单身份验证)。

这在我的本地机器上完美运行 - 这是 IIS 7 和 Windows 7。

如果 URL 是 /612/some-string,则用户将被定向到登录页面 /66/login?ReturnUrl=/612/some-string

URL 重写查看文档 ID 的 URL 的第一部分。真正的 URL 是这样的:index.aspx?documentId=612

不幸的是,当我将它部署到我们的登台服务器时,ReturnUrl 不是重写后的 URL,而是原始 URL。这会导致各种问题。

登台服务器也是安装了 URL 重写模块 2.0 的 IIS 7。它是 Windows 2008 服务器 SP2。两者都运行 ASP.NET 3.5。

我唯一的猜测是machine.config 文件对默认 httpModules 的排序不同,并且 .NET 表单身份验证模块在 URL 被重写之前就开始了。

我会尽快复习,但与此同时,这个问题的经验是什么,可以解决吗?

更新

我也尝试过改变

Response.StatusCode = 401; 

FormsAuthentication.RedirectToLoginPage();

这让我有点领先,但仍将用户引导回尚未重写的 URL。

我也可以这样做,而不是设置 401:

string currentPage = HttpUtility.UrlEncode(Request.RawUrl);
string loginUrl = FormsAuthentication.LoginUrl + "?ReturnUrl=" + currentPage;
Response.Redirect(loginUrl);

但这看起来很难看。

【问题讨论】:

  • 有什么解决办法吗?

标签: asp.net iis-7 url-rewriting


【解决方案1】:

在 Dominick Baier 的《开发更多=安全的 Microsoft ASP.NET 2.0 应用程序》一书的第 2 章中,有一个 ShowPipeline.ashx,它显示了使用 HttpHandler 在服务器上的完整管道排序:

    <%@ WebHandler Class='ShowPipeline' Language='c#' %>

    using System;
    using System.Web;
    using System.Reflection;
    using System.ComponentModel;

    // shows which modules have registered for which event
    // add a ?asm=true query string parameter to also show the assemblies
    public class ShowPipeline : IHttpHandler

    {
        static bool _showAssemblies = false;

        // names of the pipeline events
    static string[] _handlerNames = {
        "BeginRequest",
        "AuthenticateRequest",
        "DefaultAuthentication",
        "PostAuthenticateRequest",
        "AuthorizeRequest",
        "PostAuthorizeRequest",
        "ResolveRequestCache",
        "PostResolveRequestCache",
        "AcquireRequestState",
        "PostAcquireRequestState",
        "PreRequestHandlerExecute",
        "PostRequestHandlerExecute",
        "ReleaseRequestState",
        "UpdateRequestCache",
        "PostUpdateRequestCache",
        "EndRequest"
    };

    public void ProcessRequest(HttpContext ctx)
    {
        if (ctx.Request.QueryString["asm"] == "true")
            _showAssemblies = true;

        ctx.Response.Write("<hr>");

        foreach (string s in _handlerNames)
        {
            _showHandlers(s);
        }

        ctx.Response.Write("<hr>");
    }

    public void _showHandlers(string handlerName)
    {
        HttpResponse r = HttpContext.Current.Response;
        object key = _getPrivateAppField("Event" + handlerName);
        EventHandlerList ehl = (EventHandlerList)_getPrivateAppField("_events");
        MulticastDelegate md = (MulticastDelegate)ehl[key];
        if (null != md)
        {
            r.Output.WriteLine("<h2>{0}</h2>", handlerName);
            foreach (Delegate d in md.GetInvocationList())
            {
                Type tt = d.Target.GetType();
                string asm = "";
                if (_showAssemblies)
                {
                    asm = string.Format("<font color='red'>[{0}]</font>", tt.Assembly.GetName());
                }
                r.Output.WriteLine("{0}{1}.<font color='blue'>{2}</font><br>", asm, tt, d.Method.Name);
            }
        }
    }
    object _getPrivateAppField(string fieldName)
    {
        return _getPrivateField(typeof(HttpApplication), fieldName, HttpContext.Current.ApplicationInstance);
    }

    object _getPrivateField(Type t, string fieldName, object o)
    {
        return t.GetField(fieldName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic).GetValue(o);
    }

    object _getPrivateField(string fieldName, object o)
    {
        return o.GetType().GetField(fieldName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic).GetValue(o);
    }

    public bool IsReusable { get { return true; } }
}

【讨论】:

  • 我喜欢。对解决问题不太有用,但我喜欢这个。
猜你喜欢
  • 1970-01-01
  • 2012-06-01
  • 1970-01-01
  • 2013-04-23
  • 2020-03-30
  • 2011-10-29
  • 2012-11-05
  • 1970-01-01
  • 2013-01-01
相关资源
最近更新 更多