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)
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)
截至 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_name用urn:facebook:last_name
| 归档时间: |
|
| 查看次数: |
18057 次 |
| 最近记录: |