如何在使用Facebook OAuth提供商时获取位置声明

时间:2016-09-30 02:44:51

标签: c# oauth asp.net-core

我正在尝试使用Facebook OAuth获取用户的位置(或位置)(我没有尝试使用谷歌,但它也是我想要的东西)

我已经在我的启动文件中设置了我的范围,如下所示:

app.UseFacebookAuthentication(new FacebookOptions
            {
                AutomaticAuthenticate = true,
                AutomaticChallenge = true,
                ClientId = "",
                ClientSecret = "",
                Scope = {"email",
                        "user_friends",
                        "user_location" }
            });

问题出在我做的时候

_signInManager.GetExternalLoginInfoAsync()

我总是得到相同的声明类型,因此我想添加可用的地点或任何其他声明类型(例如用户名而不是电子邮件)。

当我这样做时:

var user = new ApplicationUser
{
    ProviderKey = info.ProviderKey,
    Email = info.Principal.FindFirstValue(ClaimTypes.Email),
    Name = info.Principal.FindFirstValue(ClaimTypes.GivenName),
    Surname = info.Principal.FindFirstValue(ClaimTypes.Surname),
    AuthenticationType = info.LoginProvider,
    NormalizedUserName = info.Principal.FindFirstValue(ClaimTypes.Email),
    UserName =  info.Principal.FindFirstValue(ClaimTypes.Email),
    Location = info.Principal.FindFirstValue(ClaimTypes.Locality), // I'm getting null here
    Created = DateTime.UtcNow
 };

我想我需要为我想收集的额外索赔添加配置,但我不知道从哪里开始,我们将不胜感激。

1 个答案:

答案 0 :(得分:1)

您需要添加location字段:

app.UseFacebookAuthentication(new FacebookOptions
{
      AutomaticAuthenticate = true,
      AutomaticChallenge = true,
      ClientId = "",
      ClientSecret = "",
      Scope = {"email", "user_friends", "user_location" },
      Fields = {"location"}
});