ExternalLoginInfo 电子邮件在 Microsoft 和 Facebook oauth2、MVC C# 中始终为 null?
ExternalLoginInfo Email is always null in Microsoft and Facebook oauth2, MVC C#?
我正在为 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
});
}
我正在使用以下软件包:
<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" }
});
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" }
});
创建 class :
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);
}
}
适合我。
我试过穆罕默德的回答。没有骰子。
我尝试了 Bimal 的回答。没有骰子。
最后,我使用 Nuget 升级了所有 OWIN 引用。现在可以了。
如果其他答案不起作用,请尝试根据所有 OWIN 项目升级所有 Nuget 包。
我对 Microsoft 帐户也有同样的问题,但只是那些与个人帐户(即 hotmail)相关联的问题,而不是 business/school 帐户。在尝试了上面的几个答案后,尽管用户授予了我的应用程序权限,但我仍然没有收到电子邮件地址。
最后,为我解决这个问题的是将我项目中的 OWIN 包更新到 4.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
});
}
我正在使用以下软件包:
<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" }
});
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" }
});
创建 class :
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);
}
}
适合我。
我试过穆罕默德的回答。没有骰子。 我尝试了 Bimal 的回答。没有骰子。
最后,我使用 Nuget 升级了所有 OWIN 引用。现在可以了。
如果其他答案不起作用,请尝试根据所有 OWIN 项目升级所有 Nuget 包。
我对 Microsoft 帐户也有同样的问题,但只是那些与个人帐户(即 hotmail)相关联的问题,而不是 business/school 帐户。在尝试了上面的几个答案后,尽管用户授予了我的应用程序权限,但我仍然没有收到电子邮件地址。
最后,为我解决这个问题的是将我项目中的 OWIN 包更新到 4.0 版。之后,一切正常。