在Microsoft和Facebook oauth2,MVC C#中,ExternalLoginInfo电子邮件始终为空?

Moh*_*yan 14 c# asp.net-mvc oauth oauth-2.0

我正在使用以下代码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
    });
}
Run Code Online (Sandbox Code Playgroud)

我正在使用以下包:

<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" />
Run Code Online (Sandbox Code Playgroud)

Moh*_*yan 23

我找到了解决方案,我们必须使用Facebook和文件中Microsoft的以下内容Startup.Auth.cs:

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

// Microsoft
app.UseMicrosoftAccountAuthentication(new MicrosoftAccountAuthenticationOptions()
{
    ClientId = "---",
    ClientSecret = "---",
    Scope = { "wl.basic", "wl.emails" }
});
Run Code Online (Sandbox Code Playgroud)

  • 执行此操作后,仍然`info.Email`对我来说是空的. (8认同)

Bim*_*Das 8

app.UseFacebookAuthentication(new FacebookAuthenticationOptions
            {
                AppId = "",
                AppSecret = "",
                BackchannelHttpHandler = new FacebookBackChannelHandler(),
                UserInformationEndpoint = "https://graph.facebook.com/v2.4/me?fields=id,name,email,first_name,last_name,location",
                Scope = { "email" }
            });
Run Code Online (Sandbox Code Playgroud)

创建一个类:

 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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

适合我.