外部登录信息电子邮件在Microsoft和Facebook oauth2、MVC#中始终为空?

外部登录信息电子邮件在Microsoft和Facebook oauth2、MVC#中始终为空?,c#,asp.net-mvc,oauth,oauth-2.0,C#,Asp.net Mvc,Oauth,Oauth 2.0,我将以下代码用于ExternalLoginCallback 在google中,一切正常。但是在Facebook和MicrosoftloginInfo.Email中总是空的。 以下代码有什么问题 [AllowAnonymous] public async Task<ActionResult> ExternalLoginCallback(string returnUrl) { ExternalLoginInfo loginInfo = await Authenticati

我将以下代码用于
ExternalLoginCallback

google
中,一切正常。但是在
Facebook
Microsoft
loginInfo.Email
中总是空的。 以下代码有什么问题

    [AllowAnonymous]
public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
    ExternalLoginInfo loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
    if (loginInfo == null)
    {
        return RedirectToAction("Login");
    }

    // loginInfo.Email is always null, so FindByEmailAsync throws an exception
    UserIdentityModel user = await UserManager.FindByEmailAsync(loginInfo.Email); 

    if (user != null)
    {
        await SignInAsync(user, false);
        return RedirectToLocal(returnUrl);
    }

    // If the user does not have an account, then prompt the user to create an account
    ViewBag.ReturnUrl = returnUrl;
    ViewBag.LoginProvider = loginInfo.Login.LoginProvider;
    return View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel
    {
        UserName = loginInfo.DefaultUserName,
        Email = loginInfo.Email
    });
}
[AllowAnonymous]
公共异步任务ExternalLoginCallback(字符串返回URL)
{
ExternalLoginInfo loginInfo=等待AuthenticationManager.GetExternalLoginInfoAsync();
if(loginInfo==null)
{
返回重定向操作(“登录”);
}
//电子邮件始终为空,因此FindByEmailAsync引发异常
UserIdentityModel user=wait UserManager.findbyemailsync(loginInfo.Email);
如果(用户!=null)
{
等待信号同步(用户,false);
返回重定向到本地(returnUrl);
}
//如果用户没有帐户,则提示用户创建帐户
ViewBag.ReturnUrl=返回URL;
ViewBag.LoginProvider=loginInfo.Login.LoginProvider;
返回视图(“ExternalLoginConfirmation”,新的ExternalLoginConfirmationViewModel
{
UserName=loginInfo.DefaultUserName,
Email=loginInfo.Email
});
}
我正在使用以下软件包:

<package id="Microsoft.Owin" version="3.0.1" targetFramework="net45" />
  <package id="Microsoft.Owin.Host.SystemWeb" version="3.0.1" targetFramework="net45" />
  <package id="Microsoft.Owin.Security" version="3.0.1" targetFramework="net45" />
  <package id="Microsoft.Owin.Security.Cookies" version="3.0.1" targetFramework="net45" />
  <package id="Microsoft.Owin.Security.Facebook" version="3.0.1" targetFramework="net45" />
  <package id="Microsoft.Owin.Security.Google" version="3.0.1" targetFramework="net45" />
  <package id="Microsoft.Owin.Security.MicrosoftAccount" version="3.0.1" targetFramework="net45" />
  <package id="Microsoft.Owin.Security.OAuth" version="3.0.1" targetFramework="net45" />
  <package id="Microsoft.Owin.Security.Twitter" version="3.0.1" targetFramework="net45" />

我找到了解决方案,我们必须在
Startup.Auth.cs
文件中使用
Facebook
Microsoft
,如下所示:

// Facebook
app.UseFacebookAuthentication(new FacebookAuthenticationOptions
{
    AppId = "---",
    AppSecret = "---",
    Scope = { "email" }
});

// Microsoft
app.UseMicrosoftAccountAuthentication(new MicrosoftAccountAuthenticationOptions()
{
    ClientId = "---",
    ClientSecret = "---",
    Scope = { "wl.basic", "wl.emails" }
});
创建一个类:

 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);
        }
    }
公共类FacebookBackChannelHandler:HttpClientHandler
{
受保护的重写异步System.Threading.Tasks.Task SendAsync(HttpRequestMessage请求,System.Threading.CancellationToken CancellationToken)
{
//替换RequestUri,使其不会出现格式错误
如果(!request.RequestUri.AbsolutePath.Contains(“/oauth”))
{
request.RequestUri=新Uri(request.RequestUri.AbsoluteUri.Replace(“?访问令牌“,”和访问令牌”);
}
返回wait base.sendaync(请求、取消令牌);
}
}

对我有用。

我试过穆罕默德的答案。没有骰子。 我试过比马尔的答案。没有骰子

最后,我使用Nuget升级了所有OWIN引用。现在它起作用了


如果其他答案不起作用,请尝试升级所有Nuget包,并考虑所有OWIN项

我对Microsoft帐户也有同样的问题,但只有那些与个人帐户(即hotmail)关联的帐户,而不是商业/学校帐户。在尝试了上面的几个答案后,尽管用户允许我使用我的应用程序,我仍然没有收到电子邮件地址


最后,解决这个问题的方法是将我项目中的OWIN包更新为4.0版。之后,一切正常。

尚未使用Microsoft oAuth,但使用Facebook时,您需要在Facebook应用程序的配置中特别请求电子邮件权限,否则您将无法获得电子邮件地址。我已经这样做了,并且检查了电子邮件。我在Yahoo OAuth2身份验证方面也面临同样的问题。成功验证后,电子邮件为空。你有Yahoo身份验证的解决方案吗?我有同样的问题:这样做之后,仍然
info。电子邮件对我来说是空的。这样做之后,仍然是空的。电子邮件对meinfo来说是空的。电子邮件仍然是空的。
 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);
        }
    }