Javascript Facebook Connect-单点登录导致无限循环:(
我有一个FacebookConnect(FBML)web应用程序,它已经运行了一段时间了 所有这些都运行良好,但是用户报告了单点登录的问题(我在他们的计算机上看到了这个问题,但无法在本地复制) 有了FBC,你可以挂接一个“事件”来自动确定用户是否通过了Facebook的身份验证 这是通过以下方式实现的:Javascript Facebook Connect-单点登录导致无限循环:(,javascript,asp.net,facebook,forms-authentication,single-sign-on,Javascript,Asp.net,Facebook,Forms Authentication,Single Sign On,我有一个FacebookConnect(FBML)web应用程序,它已经运行了一段时间了 所有这些都运行良好,但是用户报告了单点登录的问题(我在他们的计算机上看到了这个问题,但无法在本地复制) 有了FBC,你可以挂接一个“事件”来自动确定用户是否通过了Facebook的身份验证 这是通过以下方式实现的: FB.Connect.ifUserConnected(onUserConnected, null); 然后,onUserConnected函数可以执行尝试单点登录所需的任何操作 对我来说,我所
FB.Connect.ifUserConnected(onUserConnected, null);
然后,onUserConnected函数可以执行尝试单点登录所需的任何操作
对我来说,我所做的是对服务器进行AJAX调用,以查看用户是否有活动的网站帐户(基于Facebook详细信息-用户id,当他们连接时,我将其存储在我的系统中)
如果有,我会显示一个jQuery模式对话框(“连接Facebook”),并执行window.location.reload()
然后服务器启动并进行单点登录。此代码在每个页面上执行:
public static void SingleSignOn(string redirectUrl)
{
if (!HttpContext.Current.Request.IsAuthenticated) // If User is not logged in
{
// Does this user have an Account?
if (ActiveFacebookUser != null)
{
// Get the user.
var saUser = tblUserInfo.GetUserByUserId(ActiveFacebookUser.IdUserInfo);
if (saUser != null)
{
// Get the Membership user.
MembershipUser membershipUser = Membership.GetUser(saUser.UserID);
if (membershipUser != null && membershipUser.IsApproved)
{
// Log Them Automically.
FormsAuthentication.SetAuthCookie(membershipUser.UserName, true);
// At this point, the Forms Authentication Cookie is set in the HTTP Response Stream.
// But the current HTTP Context (IsAuthenticated) will read HTTP Request Cookies (which wont have the new cookie set).
// Therefore we need to terminate the execution of this current HTTP Request and refresh the page to reload the cookies.
HttpContext.Current.Response.Redirect(
!string.IsNullOrEmpty(redirectUrl) ? redirectUrl : HttpContext.Current.Request.RawUrl,
true);
}
else
{
HandleUnregisteredFacebookUser();
}
}
else
{
HandleUnregisteredFacebookUser();
}
}
else
{
HandleUnregisteredFacebookUser();
}
}
}
我从来没有遇到过这样的问题,但是用户报告了一个“无限”循环,其中显示对话框、刷新窗口、显示对话框、刷新窗口等等
基本上,我的AJAX调用是说用户存在,但我的单点登录不存在
这很难相信,因为代码非常相似:
这是AJAX Web服务:
if (!HttpContext.Current.Request.IsAuthenticated) // If user is not yet authenticated
{
if (FacebookConnect.Authentication.IsConnected) // If user is authenticated to Facebook
{
long fbId;
Int64.TryParse(Authentication.UserId, out fbId);
if (fbId > 0)
{
tblFacebook activeUser = tblFacebook.Load(facebookUniqueId: fbId);
if (activeUser != null && activeUser.IsActive) // If user has an active account
{
return true;
}
}
}
}
因此,如果此WS的响应为“true”,我将执行window.location.reload()
所以我不知道问题是什么(因为我无法复制),听起来好像单点登录没有正确地将表单身份验证cookie添加到响应流,或者response.Redirect(Request.RawUrl)没有正确地重新加载cookie
其他人怎么处理
这就是应该发生的事情:
此外,我意识到我也可以使用“reloadIfSessionStateChanged”:true来执行重新加载(这会停止无限循环),但问题是我无法显示漂亮的对话框。因此我发现了几个问题 首先,我不应该设置持久cookie:
FormsAuthentication.SetAuthCookie(membershipUser.UserName, true);
我们应该只在用户勾选“记住我”之类的框时执行此操作
其次,我检查服务器上每个页面上的FB Auth状态,因此这可能在客户端不同步
这是我的新解决方案——它更好,并且为可怕的“无限循环”提供了故障保护
我不再在服务器上检查FB Auth状态,而是在客户端执行以下操作:
FB.Connect.ifUserConnected(onUserConnected, null); // runs on every page request, on client-side
在onUserConnection函数中,我执行以下操作:
希望这对其他人有所帮助。因此我发现了一些问题 首先,我不应该设置持久cookie:
FormsAuthentication.SetAuthCookie(membershipUser.UserName, true);
我们应该只在用户勾选“记住我”之类的框时执行此操作
其次,我检查服务器上每个页面上的FB Auth状态,因此这可能在客户端不同步
这是我的新解决方案——它更好,并且为可怕的“无限循环”提供了故障保护
我不再在服务器上检查FB Auth状态,而是在客户端执行以下操作:
FB.Connect.ifUserConnected(onUserConnected, null); // runs on every page request, on client-side
在onUserConnection函数中,我执行以下操作: