C# 谷歌认证间歇性工作
我正在使用GoogleOAuth作为.NETWeb应用程序的身份验证模式。虽然它在我的机器上似乎工作得很好,但在现场环境中,它似乎是间歇性工作的 以下是在开发人员控制台中输入的详细信息: 并且Google+API设置为启用: 默认的C# 谷歌认证间歇性工作,c#,asp.net-mvc,oauth,google-authentication,C#,Asp.net Mvc,Oauth,Google Authentication,我正在使用GoogleOAuth作为.NETWeb应用程序的身份验证模式。虽然它在我的机器上似乎工作得很好,但在现场环境中,它似乎是间歇性工作的 以下是在开发人员控制台中输入的详细信息: 并且Google+API设置为启用: 默认的ExternalLogin方法如下: public ActionResult ExternalLogin(string provider, string returnUrl) { return new ChallengeResult(provider,
ExternalLogin
方法如下:
public ActionResult ExternalLogin(string provider, string returnUrl)
{
return new ChallengeResult(provider,
Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl }));
}
public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
if (loginInfo == null)
{
return RedirectToAction("Login");
}
var userEmail = loginInfo.Email;
var loggedInUser = VerifyAndAuthenticateUser(userEmail);
if (loggedInUser != null)
{
FormsAuthentication.SetAuthCookie(userEmail, false);
return RedirectToLocal(returnUrl);
}
return RedirectToAction("login", "account");
}
而ExternalLoginCallback
的定义如下:
public ActionResult ExternalLogin(string provider, string returnUrl)
{
return new ChallengeResult(provider,
Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl }));
}
public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
if (loginInfo == null)
{
return RedirectToAction("Login");
}
var userEmail = loginInfo.Email;
var loggedInUser = VerifyAndAuthenticateUser(userEmail);
if (loggedInUser != null)
{
FormsAuthentication.SetAuthCookie(userEmail, false);
return RedirectToLocal(returnUrl);
}
return RedirectToAction("login", "account");
}
Myweb.config
包含system.web元素中的表单身份验证:
<authentication mode="Forms">
<forms loginUrl="~/account/login" timeout="2880" />
</authentication>
我在
ExternalLogin
和ExternalLoginCallback
方法中添加了几行代码来记录失败的地方,似乎调用ExternalLoginCallback
失败了。同样,这是间歇性的,因为有时我能够完成登录。问题可能是什么?通过对参考SO答案进行以下两项更改,问题已得到解决:
Startup.Auth中的更改
会计控制器中的更改
()
public ActionResult ExternalLogin(string provider, string returnUrl)
{
ControllerContext.HttpContext.Session.RemoveAll();
var redirectUri = Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl });
return new ChallengeResult(provider, redirectUri);
}