我正在使用 Facebook 和 Google 登录我的 MVC 应用程序。 对于 Facebook 来说,登录和获取有关用户工作良好的数据。 Google 登录也可以正常工作,它会触发我的方法“ExternalLoginCallback”,该方法位于我收到 AccessToken 的位置。
但是 - 我没有找到任何像“FacebookClient”这样简单的 nuget 包,我可以在其中传递 AccessToken,我可以获得有关登录用户的更多信息,我想将它们存储到数据库中。
我发现了 Google 的其他 nugets,例如:Google.Apis.Plus.v1,我需要再次通过 X509Certificate2 传递 clientId、secreatId nad 凭据,所以我的问题是是否有提供“FacebookClient”的任何简单方法。如果不能,您能否描述一下您将如何检索有关登录用户的信息?谢谢。
[AllowAnonymous]
public ActionResult Login(string provider)
{
HttpContext.GetOwinContext().Authentication.Challenge(new AuthenticationProperties
{
RedirectUri = Url.Action("ExternalLoginCallback")
}, provider);
return new HttpUnauthorizedResult();
}
[AllowAnonymous]
public ActionResult ExternalLoginCallback()
{
var externalLoginInfo = HttpContext.GetOwinContext().Authentication.GetExternalLoginInfo();
var accessToken = externalLoginInfo.ExternalIdentity.Claims.FirstOrDefault(x => x.Type == "FacebookAccessToken")?.Value;
// HERE I WOULD LIKE TO HAVE FUNCTIONALITY SIMILAR TO FacebookClient
var client = new FacebookClient(accessToken);
dynamic facebookData = client.Get("me", new { fields = "email,name,first_name,last_name,gender,locale" });
}
最佳答案
最后我找到了方法:
var userInfoUrl = "https://www.googleapis.com/oauth2/v2/userinfo?alt=json";
var hc = new HttpClient();
hc.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var response = hc.GetAsync(userInfoUrl).Result;
var userInfo = response.Content.ReadAsStringAsync().Result;
dynamic data = JObject.Parse(userInfo);
关于c# - ASP.NET MVC Identity Google 通过 GetExternalLoginInfo 提供的 AccessToken 访问用户配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41322260/