如何在 MVC 4 中检查登录 ASP.NET facebook 用户



我想检查用户是否已经在 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");
    }

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

在我看来,最简单的方法是,如果您愿意查看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,您可以在此处请求online_presence字段,但请注意,这仅在用户登录到 facebook 用户聊天时返回。

相关内容

  • 没有找到相关文章