zoukankan      html  css  js  c++  java
  • ASP.NET FormsAuthentication跨站点登录时绝对地址返回的问题

    关键字:FormsAuthentication, loginUrl, ReturnUrl, AbsoluteUri

    在ASP.NET应用程序中,FormsAuthentication几乎是标配,但FormsAuthentication在设计时却没有考虑登录程序与当前程序不在同一个站点的场景。这个场景最基本的需求就是去另一个站点登录成功后返回要原地。可是FormsAuthentication在传递ReturnUrl时只支持相对路径,不支持绝对地址,也没有提供相应的扩展。

    比如我们在admin.cnblogs.com站点的web.config中进行了如下的FormsAuthentication设置:

    <authentication mode="Forms">
    <forms loginUrl="http://passport.cnblogs.com/login.aspx" timeout="2880" enableCrossAppRedirects="true"/>
    </authentication>
    <authorization>
    <deny users="?" />
    </authorization>

    访问admin.cnblogs.cc/Home/Index,会被重定向至passport.cnblogs.com/login.aspx?ReturnUrl=%2fHome%2fIndex,这样登录后就回不来了。

    那如何解决这个问题呢?

    目前找到了三种方法:

    方法一:在当前应用程序添加一个登录跳板页,web.config中的loginUrl指向该跳板,在跳板中获取ReturnUrl的绝对地址,再重定向至实际登录页面。

    比如在ASP.NET MVC中,我们可以用一个Controller的Action作为跳板,代码如下:

    public class LoginController : Controller
    {
    public ActionResult Redirect()
    {
    var loginUrl = "http://passport.cnblogs.com/login.aspx";
    var returnUrl = "?ReturnUrl=http://" + Request.Url.Host +
    Request.QueryString["ReturnUrl"];
    return RedirectPermanent(loginUrl + returnUrl);
    }
    }

    然后将web.config中的loginUrl指向该Action。

    该方法的缺点是要进行两次重定向。

    方法二:在Global.asax的Application_PostAuthenticateRequest事件中将ReturnUrl设置为绝对地址,并重定向至登录页面。

    代码如下(代码来自http://forums.asp.net/t/1358796.aspx):

    protected void Application_PostAuthenticateRequest(object sender, EventArgs e)
    {
    if (!UrlAuthorizationModule.CheckUrlAccessForPrincipal(
    Request.AppRelativeCurrentExecutionFilePath,
    Context.User, Request.RequestType))
    {
    Response.Redirect(String.Format("{0}?ReturnUrl={1}",
    FormsAuthentication.LoginUrl,
    Request.Url.AbsoluteUri));
    }
    }

    方法三:在Global.asax的Application_EndRequest事件中修改Response.RedirectLocation,将ReturnUrl的替换为绝对地址。

    代码如下(代码来自David Findley's Blog):

    protected void Application_EndRequest(object sender, EventArgs e)
    {
    string redirectUrl = this.Response.RedirectLocation;
    if (!string.IsNullOrEmpty(redirectUrl))
    {
    this.Response.RedirectLocation = Regex.Replace(redirectUrl,
    "ReturnUrl=(?'url'.*)", delegate(Match m)
    {
    string url = HttpUtility.UrlDecode(m.Groups["url"].Value);
    Uri u = new Uri(this.Request.Url, url);
    return string.Format("ReturnUrl={0}", HttpUtility.UrlEncode(u.ToString()));
    }, RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
    }
    }

    该方法的缺点是要针对所有重定向的URL进行处理,不仅仅是登录。

    我们选用的是第二种方法。你是如何解决这个问题的?有没有更好的方法?

    这个问题完全归咎于FormsAuthentication的设计问题,当时遇到这个问题,都不敢相信微软没考虑到这个,然后看了一下FormsAuthentication的代码,真是无语。。。

    今天写这篇博文出来发泄一下,以解心头之恨。

    从中得到的启示:糟糕的设计会被很多人骂很多年,而优秀的设计会流芳百世。

  • 相关阅读:
    js---11闭包
    js---10作用域链
    js---12数据类型,数据类型转换,NaN,
    js---11运算符,流程控制,真假
    js---10时间类
    js--09定时器
    js---08函数 定时器
    js---07 js预解析,作用域---闭包
    js---06函数传参数
    [虎嗅]职场中年独白:我们是被嫌弃的少数派
  • 原文地址:https://www.cnblogs.com/dudu/p/FormsAuthentication_ReturnUrl_AbsoluteUri.html
Copyright © 2011-2022 走看看