清除 cookie 后,使用 Okta 的 MVC 5 Kentor Auth 无法正常工作
MVC 5 Kentor Auth with Okta not working when cookies are cleared
我已经按照本页 https://github.com/Sustainsys/Saml2/blob/master/docs/OwinMiddleware.md 上提到的说明实现了对 Okta 的 SAML 身份验证支持。
有人第一次点击 Okta 中打开我的应用程序的磁贴时,身份验证不起作用。特别是来自我的 ExternalLoginCallback 函数的 AuthenticationManager.GetExternalLoginInfoAsync() 调用 returns null.
当用户第二次单击磁贴时,一切正常。
我可以通过从我的 Web 应用程序的浏览器中清除所有 cookie,然后尝试从 Okta 登录来始终重现该问题。第一次总是失败,第二次就成功了
到目前为止,我已将范围缩小到 1 个 cookie:ASP.NET_SessionId。如果我删除这个 cookie 并尝试登录它会失败。
我的 ExternalLoginCallback 方法看起来很标准:
// GET: /Account/ExternalLoginCallback
[AllowAnonymous]
public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
if (loginInfo == null)
{
return RedirectToAction("Login");
}
// Sign in the user with this external login provider if the user already has a login
var result = await SignInManager.ExternalSignInAsync(loginInfo, isPersistent: false);
switch (result)
{
case SignInStatus.Success:
return RedirectToLocal(returnUrl);
case SignInStatus.LockedOut:
return View("Lockout");
case SignInStatus.RequiresVerification:
return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = false });
case SignInStatus.Failure:
default:
// 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 { Email = loginInfo.Email });
}
}
我在这里错过了什么?
注:
我尝试了使用 Kentor Auth Services 附带的 SampleOwinApplication 应用程序清除 cookie 的相同步骤。这个应用程序在所有情况下都有效(即使在清除 cookie 之后)
回答我自己的问题:原来是 https://coding.abel.nu/2014/11/catching-the-system-webowin-cookie-monster/
中描述的 cookie monster 问题
刚刚将以下代码添加到 Startup.Auth.cs 并解决了问题。
app.UseKentorOwinCookieSaver();
我已经按照本页 https://github.com/Sustainsys/Saml2/blob/master/docs/OwinMiddleware.md 上提到的说明实现了对 Okta 的 SAML 身份验证支持。
有人第一次点击 Okta 中打开我的应用程序的磁贴时,身份验证不起作用。特别是来自我的 ExternalLoginCallback 函数的 AuthenticationManager.GetExternalLoginInfoAsync() 调用 returns null.
当用户第二次单击磁贴时,一切正常。
我可以通过从我的 Web 应用程序的浏览器中清除所有 cookie,然后尝试从 Okta 登录来始终重现该问题。第一次总是失败,第二次就成功了
到目前为止,我已将范围缩小到 1 个 cookie:ASP.NET_SessionId。如果我删除这个 cookie 并尝试登录它会失败。
我的 ExternalLoginCallback 方法看起来很标准:
// GET: /Account/ExternalLoginCallback
[AllowAnonymous]
public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
if (loginInfo == null)
{
return RedirectToAction("Login");
}
// Sign in the user with this external login provider if the user already has a login
var result = await SignInManager.ExternalSignInAsync(loginInfo, isPersistent: false);
switch (result)
{
case SignInStatus.Success:
return RedirectToLocal(returnUrl);
case SignInStatus.LockedOut:
return View("Lockout");
case SignInStatus.RequiresVerification:
return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = false });
case SignInStatus.Failure:
default:
// 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 { Email = loginInfo.Email });
}
}
我在这里错过了什么?
注: 我尝试了使用 Kentor Auth Services 附带的 SampleOwinApplication 应用程序清除 cookie 的相同步骤。这个应用程序在所有情况下都有效(即使在清除 cookie 之后)
回答我自己的问题:原来是 https://coding.abel.nu/2014/11/catching-the-system-webowin-cookie-monster/
中描述的 cookie monster 问题刚刚将以下代码添加到 Startup.Auth.cs 并解决了问题。
app.UseKentorOwinCookieSaver();