本文介绍了如何使用 ASP.NET MVC 5 和 OWIN 获取 Facebook 的名字和姓氏值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道提供了姓名"字段,但我更愿意明确访问名字和姓氏.有人可以帮忙吗?我仍然在思考 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 app)
方法中,为 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 的名字和姓氏值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!