Asp.net 5 MVC 6, add permission for facebook email

I want to know how to add additional permissions for external login on Facebook and specifically for email. The external input works fine, but I cannot reproduce the same code that worked for MVC 5 in this, so this is what I have now:

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

        });

but it does not add email permissions.

This is the code I used in MVC 5 along with the 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));
                }
            }
        });
+4
source share
2 answers

Well thanks to @Mike Wasson's comment, this led me to a working answer,

this 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 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

+5

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

Source: https://habr.com/ru/post/1609443/


All Articles