Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get user email from facebook in asp net

I'm trying to get user name and user email from facebook. I read a lot of information on this topic and this is my final code that works for some reason only on my facebook app admin account:

public partial class Startup
{
    public void ConfigureAuth(IAppBuilder app)
    {

        /*Other external login options*/

         var FacebookOptions = new FacebookAuthenticationOptions()
                {
                    AppId = "My App Id",
                    AppSecret = "My App Secret",
                    SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie,
                    BackchannelHttpHandler = new FacebookBackChannelHandler(),
                    UserInformationEndpoint = "https://graph.facebook.com/v2.7/me?fields=id,name,email"
                };


    }
}
public class FacebookBackChannelHandler : HttpClientHandler
{
    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
    {
        if (!request.RequestUri.AbsolutePath.Contains("/oauth"))
        {
            request.RequestUri = new Uri(request.RequestUri.AbsoluteUri.Replace("?access_token", "&access_token"));
        }

        return await base.SendAsync(request, cancellationToken);
    }
}
public class AccountController : Controller
{
    public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
    {
        var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
        /*my sign in code that works on my facebook app admin account*/
    }  
}    

On every over account I get loginInfo.Email is equal to null.

In developers.facebook.com I have:

Approved Items

When someone clicks on "Login with Facebook" he gets this message:

First facebook login page

If I click "Review the info you provide" i get:

Review the info you provide

What am I missing? Why doesn't it work?

like image 493
levkaster Avatar asked Aug 31 '16 18:08

levkaster


1 Answers

Finally found an answer! All that needs to be done is to add Scope = { "email" } to FacebookOptions and that's solves the problem!

My code now:

public partial class Startup
{
    public void ConfigureAuth(IAppBuilder app)
    {

        /*Other external login options*/

         var FacebookOptions = new FacebookAuthenticationOptions()
                {
                    AppId = "My App Id",
                    AppSecret = "My App Secret",
                    SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie,
                    BackchannelHttpHandler = new FacebookBackChannelHandler(),
                    Scope = { "email" },
                    UserInformationEndpoint = "https://graph.facebook.com/v2.7/me?fields=id,name,email"
                };
    }
}

The rest of the code stays the same. I only added error page for the case if this problem returns:

public class AccountController : Controller
{
    public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
    {
        var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
        if(loginInfo.Email == null)
        {
             return RedirectToAction("FacebookError", "Account");
        }
        /*my sign in code*/
    }  
}   
like image 57
levkaster Avatar answered Sep 20 '22 07:09

levkaster