由于某种未知原因,我无法再在 Chrome 上登录我自己的应用程序。
这是我登录的方式:
[AllowAnonymous]
[HttpPost]
public async Task<ActionResult> Login(LoginModel model)
{
if (User.Identity.IsAuthenticated)
return RedirectToAction("Index", "Home");
if (!ModelState.IsValid)
return View();
var status = await signInManager.PasswordSignInAsync(model.Login, model.Password, true, false);
switch (status)
{
case Microsoft.AspNet.Identity.Owin.SignInStatus.Success:
{
int userId = User.Identity.GetUserId<int>();
if (userManager.IsFirstLogin(userId))
{
return RedirectToAction("Declaration", "Home", new { returnUrl = model.ReturnUrl });
}
return RedirectToLocal(model.ReturnUrl);
}
default:
return View();
}
}
我调试了代码并且 PasswordSignInAsync 成功,但是 Chrome 显示没有为页面设置 cookie。实际上重定向到操作,标记为 [Authorize] 属性再次重定向到登录页面。
我在 IE 上测试了站点,一切正常,我可以登录和注销,并且 cookie 设置正确。
如果我能提供更详细的信息,请告诉我。
编辑:更多细节
我一直在测试一个对日期时间敏感的应用程序(在特定日期后停止显示)。我将系统时间更改为 2016 年 8 月 1 日,登录然后切换回现在。那是一切都停止工作的时候。
奇怪的是,当我切换回 future 的时间时,我可以再次登录并且 session cookie 设置正确。但是当我回到“现在”时,我无法再次登录。
最佳答案
仍然需要调查发生了什么,但是对我来说,“修复”是更改 Auth cookie 的名称。
在 Startup.Auth.cs 中:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
CookieName = $"SomeCustomNameForAuthCookie",
Provider = ...
});
我怀疑这样一个事实,即我的计算机上有很多用于开发的网站,并且所有网站都具有相同的默认 cookie 名称。
关于Asp.NET Identity 停止在 Chrome 上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35232797/