ASP.NET FormsAuthentication跨站点登录时绝对地址返回的问题

 

在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的代码,真是无语。。。

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值