2014-10-27 42 views
3

我正试图使用​​Microsoft OWIN OAuth库在现有的ASP.NET MVC 5应用程序中实现Facebook身份验证,并且无法找到如何获取用户的电子邮件地址的方法。注册如下所示:Facebook不会为用户返回电子邮件

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

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

将范围email添加到选项中。

在认证时,在控制器ExternalLoginCallback方法,所述email为null与此代码:

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

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

也试过此无济于事:从Facebook返回

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

JSON数据的确似乎没有包含电子邮件地址:

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

与Facebook不返回地址或代码中缺少某些东西是否存在问题?任何帮助提供将不胜感激。

谢谢。

+0

假设“电子邮件”是范围的正确名称,我会说你问电子邮件的范围,但用户是否也授予它? – 2014-10-29 09:00:34

回答

0

要回答我的问题,我刚才已经升级到使用最新的组件MVC 5.2.3和3.0.1 OWIN,应用程序,现在认证工作中。获取电子邮件地址的代码是:

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

    ... 
} 

这是在Visual Studio中创建的新ASP.NET MVC Web项目附带的默认代码。无论现在是什么问题,它都是固定的。

-1

我测试下面的代码和它的作品

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); 
} 
相关问题