Facebook doesn't return email to user

I'm trying to implement Facebook Authentication in an existing ASP.NET MVC 5 application using Microsoft's OWIN OAuth Libraries and can't find a way to get the user's email address. Registration looks like this:

var options = new FacebookAuthenticationOptions
{
    AppId = "YYY",
    AppSecret = "ZZZ"
};

options.Scope.Add("public_profile");
options.Scope.Add("email");
app.UseFacebookAuthentication(options);

      

An area is added to the parameters email

.

When authenticated in a ExternalLoginCallback

method in a controller, email

it is null with this code:

AuthenticateResult authResult = await authenticationManager.AuthenticateAsync(DefaultAuthenticationTypes.ExternalCookie);

ClaimsIdentity externalIdentity = authResult.Identity;
string email = externalIdentity.FindFirstValue(ClaimTypes.Email);

      

Also tried this to no avail:

var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
var email = loginInfo.Email;

      

The Json data returned from Facebook doesn't seem to contain an email address:

{"id":"989197284440871","first_name":"Martin","gender":"male","last_name":"Stauf\u010d\u00edk","link":"https:\/\/www.facebook.com\/app_scoped_user_id\/989197284440871\/","locale":"cs_CZ","name":"Martin Stauf\u010d\u00edk","timezone":2,"updated_time":"2014-10-18T16:14:08+0000","verified":true}

      

Is it a problem with Facebook not returning an address or is there something missing in the code? Any help offered would be appreciated.

Thank.

+3


source to share


2 answers


To answer my own question, I just updated my application to use the latest MVC 5.2.3 and OWIN 3.0.1 components, and authentication is now working. Code to get email address:

[AllowAnonymous]
public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
    var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
    var email = loginInfo.Email;

    ...
}

      



This is the default code that ships with the new ASP.NET MVC Web Project created in Visual Studio. Whatever the problem, it has been fixed.

0


source


I have tested the below code and it works



var loginInformation = await AuthenticationManager.AuthenticateAsync(DefaultAuthenticationTypes.ExternalCookie);
if (loginInformation != null && loginInformation.Identity != null &&
        loginInformation.Identity.IsAuthenticated)
{
    var claimsIdentity = loginInformation.Identity;
    var providerKeyClaim = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier);
    string providerKey = providerKeyClaim.Value;
    string issuer = providerKeyClaim.Issuer;
    string name = claimsIdentity.FindFirstValue(ClaimTypes.Name);
    string emailAddress = claimsIdentity.FindFirstValue(ClaimTypes.Email);
}

      

-1


source







All Articles