I want to check if a user is already logged in with facebook in ASP.NET MVC4. I'm using Facebook C# SDK. I have this code for getting the access_token from facebook:
public ActionResult FacebookCallback(string code)
{
var fb = new FacebookClient();
dynamic result = fb.Post("oauth/access_token", new
{
client_id = "XXXXXXXXXXXXXX",
client_secret = "XXXXXXXXXXXXXXXXXXX",
redirect_uri = RedirectUri.AbsoluteUri,
code = code
});
var accessToken = result.access_token;
Session["AccessToken"] = accessToken;
fb.AccessToken = accessToken;
dynamic me = fb.Get("me?fields=first_name,last_name,id,email");
string email = me.email;
FormsAuthentication.SetAuthCookie(email, false);
return RedirectToAction("Index", "Home");
}
I don't know how to check if user is already logged in. Please help.
The most easiest way in my opinion if your willing to checkout Facebooks javascript SDK, then there is a FB.getLoginStatus
method which will give you the info you need.
Taken from facebook's dev site:
FB.getLoginStatus(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;
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
} else {
// the user isn't logged in to Facebook.
}
});
There is also the user_online_presence
extended permission here . Probably not what you want, but using FQL, you can request the online_presence
field here , but note this only returns if the user is logged into facebook user chat.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.