我想知道如何为 Facebook 外部登录添加更多权限,特别是电子邮件。外部登录工作正常,但我似乎无法将用于 MVC 5 的相同代码复制到这个代码中,所以这就是我现在所拥有的:
services.Configure<FacebookAuthenticationOptions>(options =>
{
options.AppId = Configuration["Authentication:Facebook:AppId"];
options.AppSecret = Configuration["Authentication:Facebook:AppSecret"];
options.Scope.Add("email");
});
但它没有添加电子邮件权限。
这是我在 MVC 5 中与 Facebook SDK block 一起使用的代码:
app.UseFacebookAuthentication(new FacebookAuthenticationOptions
{
AppId = "XXXXXX",
AppSecret = "XXXXXXX",
Scope = { "email" },
Provider = new FacebookAuthenticationProvider
{
OnAuthenticated = async context =>
{
context.Identity.AddClaim(new System.Security.Claims.Claim("FacebookAccessToken", context.AccessToken));
}
}
});
最佳答案
好的,感谢@Mike Wasson 的评论,它让我找到了一个可行的答案,
这个SO post
所以我在启动类中更改的是:
services.Configure<FacebookAuthenticationOptions>(options =>
{
options.AppId = Configuration["Authentication:Facebook:AppId"];
options.AppSecret = Configuration["Authentication:Facebook:AppSecret"];
options.Scope.Add("email");
options.BackchannelHttpHandler = new FacebookBackChannelHandler();
options.UserInformationEndpoint = "https://graph.facebook.com/v2.4/me?fields=id,name,email,first_name,last_name,location";
}
并添加了这个新类
public class FacebookBackChannelHandler : HttpClientHandler
{
protected override async System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
// Replace the RequestUri so it's not malformed
if (!request.RequestUri.AbsolutePath.Contains("/oauth"))
{
request.RequestUri = new Uri(request.RequestUri.AbsoluteUri.Replace("?access_token", "&access_token"));
}
return await base.SendAsync(request, cancellationToken);
}
}
无需进一步调整,它现在可以检索电子邮件 :D
关于Asp.net 5 MVC 6, 为 facebook 电子邮件添加权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32839979/