Asp.net 5 MVC 6,为Facebook电子邮件添加权限

4

我想知道如何向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 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));
                }
            }
        });

可能与此相关:https://github.com/aspnet/Security/issues/435 - Mike Wasson
2个回答

5

感谢 @Mike Wasson 的评论,它让我找到了一个可行的答案。

这个 Stack Overflow 帖子

所以我在启动类中所做的更改是:

        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

2
获取生日怎么样?我尝试添加范围和生日,但这没有起作用? - DotnetShadow

3

Aspnet Core RC2

 app.UseFacebookAuthentication(options =>
        {                
            options.AppId = Configuration["Authentication:Facebook:AppId"];
            options.AppSecret = Configuration["Authentication:Facebook:AppSecret"];                
            options.Fields.Add("name");
            options.Fields.Add("email");                
            options.Events = new OAuthEvents
            {
                OnRemoteFailure = context =>
                {
                    context.Response.Redirect($"/Account/ExternalLoginCallback?remoteError={ UrlEncoder.Default.Encode(context.Failure.Message) }");
                    context.HandleResponse();
                    return Task.FromResult(0);
                }
            };
        });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接