如何使用ASP.NET MVC 5和OWIN获取Facebook的名字和姓氏值?

Dav*_*xon 20 asp.net-mvc facebook owin asp.net-mvc-5

我知道提供了"名称"字段,但我更愿意明确地访问名字和姓氏.有人可以帮忙吗?我仍然围绕着ASP.Net MVC.

Dav*_*xon 46

在Startup.Auth.cs ConfigureAuth(IAppBuilder app)方法中,为Facebook设置以下内容:

var x = new FacebookAuthenticationOptions();
        x.Scope.Add("email");
        x.AppId = "*";
        x.AppSecret = "**";
        x.Provider = new FacebookAuthenticationProvider()
        {
            OnAuthenticated = async context =>
                {
                    context.Identity.AddClaim(new System.Security.Claims.Claim("FacebookAccessToken", context.AccessToken));
                    foreach (var claim in context.User)
                    {
                        var claimType = string.Format("urn:facebook:{0}", claim.Key);
                        string claimValue = claim.Value.ToString();
                        if (!context.Identity.HasClaim(claimType, claimValue))
                            context.Identity.AddClaim(new System.Security.Claims.Claim(claimType, claimValue, "XmlSchemaString", "Facebook"));

                    }

                }
        };

        x.SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie;
        app.UseFacebookAuthentication(x);
        /*
        app.UseFacebookAuthentication(
           appId: "*",
           appSecret: "*");
         * */
Run Code Online (Sandbox Code Playgroud)

然后使用它来访问用户的登录信息:

var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
Run Code Online (Sandbox Code Playgroud)

然后以下来获得名字:

var firstNameClaim = loginInfo.ExternalIdentity.Claims.First(c => c.Type == "urn:facebook:first_name");
Run Code Online (Sandbox Code Playgroud)


Raf*_*ski 7

Facebook改变了它的许可api.您可以在此处获取有关它的更多信息:https://developers.facebook.com/docs/facebook-login/permissions

名称需要public_profile权限

var facebookAuthenticationOptions = new FacebookAuthenticationOptions()
{
    AppId = "appId",
    AppSecret = "key"
};
facebookAuthenticationOptions.Scope.Add("email");
facebookAuthenticationOptions.Scope.Add("public_profile");
app.UseFacebookAuthentication(facebookAuthenticationOptions);
Run Code Online (Sandbox Code Playgroud)

你可以使用它:

var loginInfo = await authenticationManager.GetExternalLoginInfoAsync();
loginInfo.ExternalIdentity.Claims.First(c => c.Type == "urn:facebook:name")
Run Code Online (Sandbox Code Playgroud)

authenticationManager是一个实例,你可以使用:

HttpContext.GetOwinContext().Authentication;
Run Code Online (Sandbox Code Playgroud)

  • 这适合我; 我可以得到名字+姓氏.但电子邮件总是"无聊".另外,您是否知道单独获取名字和姓氏的方法? (2认同)

小智 6

不幸的是,由于Facebook使用API​​更新2.4更改了默认返回值,因此此方法不再起作用

看起来现在获取first_name等的唯一方法是使用Facebook Graph API(就像这篇帖子所示).

我还在Katana项目网站上发现了这个帖子,解决了这个问题,并且已经提交了一个拉取请求,但它还没有合并为jet.

希望这可以保护一些人的时间;)


Waq*_*hah 5

截至 2017 年,这是对我有用的代码(感谢上面 David Poxon 的代码)。确保您已升级到 3.1.0 版Microsoft.Owin.Security.Facebook

在 Startup.Auth.cs(或某些情况下的 Startup.cs)中,放置以下代码:

app.UseFacebookAuthentication(new FacebookAuthenticationOptions()
{
    AppId = "***",
    AppSecret = "****",
    BackchannelHttpHandler = new HttpClientHandler(),
    UserInformationEndpoint = "https://graph.facebook.com/v2.8/me?fields=id,name,email,first_name,last_name",
    Scope = { "email" },
    Provider = new FacebookAuthenticationProvider()
    {
        OnAuthenticated = async context =>
        {
            context.Identity.AddClaim(new System.Security.Claims.Claim("FacebookAccessToken", context.AccessToken));
            foreach (var claim in context.User)
            {
                var claimType = string.Format("urn:facebook:{0}", claim.Key);
                string claimValue = claim.Value.ToString();
                if (!context.Identity.HasClaim(claimType, claimValue))
                    context.Identity.AddClaim(new System.Security.Claims.Claim(claimType, claimValue, "XmlSchemaString", "Facebook"));
            }
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

然后在您的控制器的外部登录回调方法中,添加以下代码:

var firstName = loginInfo.ExternalIdentity.Claims.First(c => c.Type == "urn:facebook:first_name").Value;
Run Code Online (Sandbox Code Playgroud)

同样,对于获得姓氏,用上面的线和更换urn:facebook:first_nameurn:facebook:last_name