【问题标题】:Asp.net 5 MVC 6, add permission for facebook emailAsp.net 5 MVC 6,添加 facebook 电子邮件权限
【发布时间】:2015-09-29 09:03:43
【问题描述】:

我想知道如何为 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 块:

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));
                }
            }
        });

【问题讨论】:

标签: asp.net asp.net-mvc facebook asp.net-core asp.net-core-mvc


【解决方案1】:

好的,感谢@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

【讨论】:

  • 过生日怎么样?我尝试添加范围和生日,但这不起作用?
【解决方案2】:

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);
                }
            };
        });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-07
    • 2014-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-01
    • 1970-01-01
    相关资源
    最近更新 更多