如何使用ASP.NET MVC 5 OWIN获得Facebook的姓和名值? [英] How to get Facebook first and last name values using ASP.NET MVC 5 and OWIN?

查看:145
本文介绍了如何使用ASP.NET MVC 5 OWIN获得Facebook的姓和名值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道一个名称字段中提供,但我会preFER明确访问姓和名。有人能帮助呢?我还在包装我的头周围ASP.Net MVC。

I know that a 'Name' field is provided, but I would prefer to access the first and last names explicitly. Can someone help with this? I'm still wrapping my head around ASP.Net MVC.

推荐答案

在您Startup.Auth.cs ConfigureAuth(IAppBuilder应用)方法,设置Facebook的以下内容:

In your Startup.Auth.cs ConfigureAuth(IAppBuilder app) method, set the following for 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: "*");
         * */

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

Then use this to access the user's login info:

var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();

然后将以下拿到的第一个名字:

And then the following to get the first name:

var firstNameClaim = loginInfo.ExternalIdentity.Claims.First(c => c.Type == "urn:facebook:first_name");

这篇关于如何使用ASP.NET MVC 5 OWIN获得Facebook的姓和名值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆