Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to know how add more permissions to Facebook external login and specially the email one. The external login works fine but I can't seem to replicate the same code that was working for MVC 5 into this one, so this is what I have for now:

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

        });

but it doesn't add the email permission.

This is the code which I used in MVC 5 along with 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));
                }
            }
        });
like image 248
user5049376 Avatar asked Feb 08 '23 22:02

user5049376


2 Answers

Ok thanks to @Mike Wasson's comment it led me to a working answer,

this SO post

so what i changed in startup class was this:

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

and added this new class

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

and no further adjustments are needed and it can retrieve the email now :D

like image 175
user5049376 Avatar answered Feb 12 '23 12:02

user5049376


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);
                }
            };
        });
like image 45
Jhonattan Avatar answered Feb 12 '23 12:02

Jhonattan