Receive user email from facebook in asp net

I am trying to get the username and email address of a user from facebook. I read a lot of information on this topic, and this is my last 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 additional account I get loginInfo.Email is null .

At developers.facebook.com I have:

Approved Items

When someone clicks "Sign in to Facebook", he receives this message:

First facebook login page

If I click "Browse the information you provide", I get:

View the information you provide

What am I missing? Why is this not working?

+6
source share
2 answers

Finally found the answer! All you have to do is add Scope = { "email" } to FacebookOptions and solve this problem!

My code is:

 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 remains the same. I just added an 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*/ } } 
+5
source

Facebook offers people full control over the permissions that they give the app. This control goes beyond the point at which they see the login dialog. They may choose not to grant specific permissions during the login process. They can also revoke permissions in their Facebook privacy settings at any time. Applications must verify that permissions are correct before attempting to make an API call where they are needed. For example, verifying that an email action is still provided before attempting to publish an Open Graph story. facebook provides the graph API endpoint for a list of permissions granted:

 GET /{user-id}/permissions 

The call must be made either with an access token or with an access token to your application. The call returns a JSON string containing the names of rights granted to the application and their status:

 { "data": [ { "permission": "public_profile", "status": "granted" }, { "permission": "publish_actions", "status": "granted" }, { "permission": "user_friends", "status": "declined" } ] } 

using this, you can determine whether or not it is possible to obtain the required information. hope this helps.

link: https://developers.facebook.com/docs/facebook-login/permissions/requesting-and-revoking

+1
source

All Articles