我知道提供了一个“名称”字段,但我更愿意明确地访问第一个和最后一个名字。有人可以帮忙吗?我仍然围着ASP.Net MVC包围我的头。
解决方法
在您的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: "*"); * */
var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
然后以下获得名字:
var firstNameClaim = loginInfo.ExternalIdentity.Claims.First(c => c.Type == "urn:facebook:first_name");