0

我想检查用户是否已经在 ASP.NET MVC4 中使用 facebook 登录。我正在使用 Facebook C# SDK。我有从 facebook 获取 access_token 的代码:

  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");
    }

我不知道如何检查用户是否已经登录。请帮忙。

4

1 回答 1

0

我认为最简单的方法是如果您愿意检查 Facebook 的 javascript SDK,那么有一种FB.getLoginStatus方法可以为您提供所需的信息。

取自 facebook 的开发网站:

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.
  }
});

这里还有user_online_presence扩展权限。可能不是您想要的,但使用 FQL,您可以在此处请求该字段,但请注意,这仅在用户登录 facebook 用户聊天时返回。online_presence

于 2013-10-02T12:40:13.897 回答