问题描述
我有刷新的infinty循环当我尝试与Facebook登录我的网站上,
我宣布AFER的body标签,这是一个脚本
I have an infinty loop of refresh when I try to log in with facebook on my site ,
I have declared a script afer the the body tag that was
<script>
window.fbAsyncInit = function () {
FB.init({
appId: '337323336385***', // App ID
status: true, // check login status
cookie: true, // enable cookies to allow the server to access the session
xfbml: true // parse XFBML
});
// Additional initialization code here
FB.Event.subscribe('auth.authResponseChange', function (response) {
if (response.status === 'connected') {
// the user is logged in and has authenticated your
// app, and response.authResponse supplies
// the user's ID, a valid access token, a signed
// request, and the time the access token
// and signed request each expire
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
// TODO: Handle the access token
//alert("check");
// Do a post to the server to finish the logon
// This is a form post since we don't want to use AJAX
var form = document.createElement("form");
form.setAttribute("method", 'post');
form.setAttribute("action", '/FacebookLogin.ashx');
var field = document.createElement("input");
field.setAttribute("type", "hidden");
field.setAttribute("name", 'accessToken');
field.setAttribute("value", accessToken);
form.appendChild(field);
document.body.appendChild(form);
form.submit();
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
//alert("Please ");
} else {
// the user isn't logged in to Facebook.
//alert("jj");
alert("Please Sign into your account to access the site");
}
});
};
// Load the SDK Asynchronously
(function (d) {
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) { return; }
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
</script>
当你看到这个功能,它调用C#函数是FacebookLogin.ashx的文件,这个功能是RESPONSABLE创建包含数据的用户的会话变量
as you see this function it's calling a C# function that's in the file of FacebookLogin.ashx this function is responsable for creating a Session variable containing the data for the user
public void ProcessRequest(HttpContext context)
{
var accessToken = context.Request["accessToken"];
context.Session["AccessToken"] = accessToken;
HttpCookie cookie = new HttpCookie("FB");
string accessToken2 = context.Session["AccessToken"].ToString();
Facebook.FacebookClient client = new Facebook.FacebookClient(accessToken2);
dynamic result = client.Get("me", new { fields = "name,id,link,gender" });
cookie["FBID"] = result.id;
context.Response.Cookies.Add(cookie);
context.Response.Redirect("/login.aspx");
}
之后,在我检查这个会话变量是否存在等的页面加载
after that in the pageload I check for the existance of this session variable
string accessToken;
FacebookClient client;
dynamic result;
if (Session["AccessToken"] != null)
{
accessToken = Session["AccessToken"].ToString();
client = new FacebookClient(accessToken);
result = client.Get("me", new { fields = "name,id,link,gender" });
if (gb.CheckExistanceByFBID(result.id))
{
string FBID = result.id;
var userDetails = context.Users.Where(x => x.FBID == FBID).Select(x => x).First();
HttpCookie cookie = new HttpCookie("userData", userDetails.UserName);
cookie.Expires = DateTime.Now.AddMonths(2);
cookie["UserName"] = userDetails.UserName;
cookie["UserID"] = userDetails.UserID.ToString();
cookie["Password"] = userDetails.Password;
cookie["isAdmin"] = userDetails.Admin.ToString();
cookie["Name"] = userDetails.DisplayName;
cookie["FBID"] = userDetails.FBID;
Response.Cookies.Add(cookie);
System.Web.Security.FormsAuthentication.SetAuthCookie(userDetails.UserName, true);
System.Web.Security.FormsAuthentication.Timeout.Add(new TimeSpan(40, 0, 0, 0));
Response.Redirect("/Default.aspx");
}
else
{
//var accessToken = Session["AccessToken"].ToString();
//var client = new FacebookClient(accessToken);
//dynamic result = client.Get("me", new { fields = "name,id,link,gender" });
accessToken = Session["AccessToken"].ToString();
client = new FacebookClient(accessToken);
result = client.Get("me", new { fields = "name,id,link,gender" });
FBRegisterPanel.Visible = false;
MainRegisterPanel.Visible = true;
txtUserName.Text = result.name;
}
}
这是我有刷新的infinty环后,我的问题,点击Facebook登录按钮!
the problem that I have an infinty loop of refresh after I click the facebook login button !
推荐答案
我有同样的问题。
在login.aspx的负载时,JS执行,哪些职位的形式FacebookLogin.ashx,它重定向到login.aspx的,这意味着JS再次运行并继续循环。
When login.aspx loads, the JS is executed, which posts the form to FacebookLogin.ashx, which redirects to login.aspx, which means the JS is run again and the cycle continues.
(希望)通过执行以下,如果一个OAuth标记存储,if语句作出虚假因此脚本提交表单,以永远不会执行FacebookLogin.ashx。
(Hopefully) by doing the following, if an OAuth token is stored, the if-statement is made false so the script to submit the form to FacebookLogin.ashx is never executed.
FB.Event.subscribe('auth.authResponseChange', function (response) {
if (response.status === 'connected' && "<%= Session["AccessToken"].ToString() %>" == "") { ... }
请注意:确保会议[的accessToken]包含一个空字符串,即使没有存储访问令牌,这是因为:
Note: make sure that Session["AccessToken"] contains an empty string even if there is no access token to store, because:
- 的JS是寻找会话[的accessToken]一个空字符串,使if语句真。
- 如果保留为空,我想和LT;%=会议[的accessToken]的ToString()%>将抛出一个空引用异常 。
您可以通过此execuing在Page_Init做到这一点:
You can do this by execuing this on Page_Init:
if (Session["AccessToken"] == null)
{
Session["AccessToken"] = string.Empty;
}
这篇关于与Facebook C#登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!