ExternalLoginInfo电子邮件总是空的微软和Facebook的oauth2,MVC C#? [英] ExternalLoginInfo Email is always null in Microsoft and Facebook oauth2, MVC C#?
本文介绍了ExternalLoginInfo电子邮件总是空的微软和Facebook的oauth2,MVC C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用下面的code为 ExternalLoginCallback
结果
在谷歌
一切正常。但在的Facebook
和微软
loginInfo.Email
总是空。
这有什么错如下code?
[使用AllowAnonymous]
公共异步任务<&的ActionResult GT; ExternalLoginCallback(字符串RETURNURL)
{
ExternalLoginInfo LOGININFO =等待AuthenticationManager.GetExternalLoginInfoAsync();
如果(LOGININFO == NULL)
{
返回RedirectToAction(登录);
} // loginInfo.Email总是空,所以FindByEmailAsync抛出一个异常
UserIdentityModel用户=等待UserManager.FindByEmailAsync(loginInfo.Email); 如果(用户!= NULL)
{
等待SignInAsync(用户,FALSE);
返回RedirectToLocal(RETURNURL);
} //如果用户没有一个帐户,然后提示用户创建一个帐户
ViewBag.ReturnUrl = RETURNURL;
ViewBag.LoginProvider = loginInfo.Login.LoginProvider;
返回视图(ExternalLoginConfirmation,新ExternalLoginConfirmationViewModel
{
用户名= loginInfo.DefaultUserName,
电子邮件= loginInfo.Email
});
}
我用下面的软件包:
<包ID =Microsoft.Owin版本=3.0.1targetFramework =net45/>
<包ID =Microsoft.Owin.Host.SystemWeb版本=3.0.1targetFramework =net45/>
<包ID =Microsoft.Owin.Security版本=3.0.1targetFramework =net45/>
<包ID =Microsoft.Owin.Security.Cookies版本=3.0.1targetFramework =net45/>
<包ID =Microsoft.Owin.Security.Facebook版本=3.0.1targetFramework =net45/>
<包ID =Microsoft.Owin.Security.Google版本=3.0.1targetFramework =net45/>
<包ID =Microsoft.Owin.Security.MicrosoftAccount版本=3.0.1targetFramework =net45/>
<包ID =Microsoft.Owin.Security.OAuth版本=3.0.1targetFramework =net45/>
<包ID =Microsoft.Owin.Security.Twitter版本=3.0.1targetFramework =net45/>
解决方案
我找到了解决办法,我们必须使用的Facebook
和微软
在 Startup.Auth.cs
文件:
//的Facebook
app.UseFacebookAuthentication(新FacebookAuthenticationOptions
{
的AppId =---,
AppSecret =---,
SCOPE = {电子邮件}
});//微软
app.UseMicrosoftAccountAuthentication(新MicrosoftAccountAuthenticationOptions()
{
客户端Id =---,
ClientSecret =---,
SCOPE = {wl.basic,wl.emails}
});
I'm using the following code for ExternalLoginCallback
In google
everything is OK. but in Facebook
and Microsoft
loginInfo.Email
is always null.
What's wrong with the following code?
[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
});
}
I'm using the following packages:
<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" />
解决方案
I found the solution, We have to use Facebook
and Microsoft
as the following in Startup.Auth.cs
file:
// Facebook
app.UseFacebookAuthentication(new FacebookAuthenticationOptions
{
AppId = "---",
AppSecret = "---",
Scope = { "email" }
});
// Microsoft
app.UseMicrosoftAccountAuthentication(new MicrosoftAccountAuthenticationOptions()
{
ClientId = "---",
ClientSecret = "---",
Scope = { "wl.basic", "wl.emails" }
});
这篇关于ExternalLoginInfo电子邮件总是空的微软和Facebook的oauth2,MVC C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文