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

原文地址:https://www.cnblogs.com/zxktxj/p/3359890.html