自分のサイトでFacebookを使用してログインしようとすると、更新のループが
無限にあります。ボディタグの後にスクリプトを宣言しました。
<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>
この関数を見ると、FacebookLogin.ashxのファイルにあるC#関数を呼び出しています。この関数は、ユーザーのデータを含むSession変数を作成する責任があります。
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");
}
その後、ページロードでこのセッション変数の存在を確認します
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;
}
}
Facebookのログインボタンをクリックした後、更新のループが無限に続くという問題!