Asp.net 5 MVC 6,添加facebook电子邮件的权限

use*_*376 4 asp.net asp.net-mvc facebook asp.net-core-mvc asp.net-core

我想知道如何为Facebook外部登录添加更多权限,特别是电子邮件.外部登录工作正常,但我似乎无法将用于MVC 5的相同代码复制到这个中,所以这就是我现在所拥有的:

        services.Configure<FacebookAuthenticationOptions>(options =>
        {
            options.AppId = Configuration["Authentication:Facebook:AppId"];
            options.AppSecret = Configuration["Authentication:Facebook:AppSecret"];
            options.Scope.Add("email");

        });
Run Code Online (Sandbox Code Playgroud)

但它不会添加电子邮件权限.

这是我在MVC 5中使用的代码以及Facebook SDK nugget:

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));
                }
            }
        });
Run Code Online (Sandbox Code Playgroud)

use*_*376 5

好的,感谢@Mike Wasson的评论,它让我得到了一个有效的答案,

这个 SO帖子

所以我在启动课上改变了这个:

        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";
        }
Run Code Online (Sandbox Code Playgroud)

并添加了这个新课程

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

并且不需要进一步调整,它现在可以检索电子邮件:D

  • 生日怎么样?我尝试添加范围和生日但这不起作用? (2认同)