我正在使用 ASP.NET MVC 4 (NET Framework 4.0)
出于我不明白的原因,即使在浏览器或/和应用程序重新启动后,我的用户仍保持登录状态。更进一步,即使在整个计算机重新启动之后,这意味着身份验证 cookie/票证仍然存在。
我不想在我的 Web.config
中使用 cookieless
属性,因为我不想将 cookie 存储在 URL 中以解决安全和 SEO 问题。
我不明白为什么会这样,我已将身份验证 cookie 设置为不在 FormsAuthentication.SetAuthCookie()
方法中保留。
这是我的登录操作,isValid(username, password)
是一个自定义方法,用于检查模型中的用户名和密码是否匹配。
db
是我的数据库上下文。
[AllowAnonymous]
[HttpPost]
public ActionResult LogIn(Employe user)
{
if (ModelState.IsValid)
{
if (IsValid(user.username, user.password))
{
FormsAuthentication.SetAuthCookie(user.username, false);
Employe currentEmp = db.Employes.SingleOrDefault(emp => emp.username == user.username);
Session["currentUser"] = currentEmp;
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError("", "Login Data Incorrect!");
}
}
return View();
}
这是我的 Web.config
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" protection="All" timeout="2880" />
</authentication>
最佳答案
如果您使用的是表单例份验证模式,则存在同时控制表单例份验证过期和自定义 session 过期的情况。那可能会使您的应用程序发生问题。 希望您能在这里找到解决方案 How can I handle forms authentication timeout exceptions in ASP.NET?
关于c# - 即使设置为非持久性,ASP.NET MVC 4 身份验证 cookie 也会被保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52864204/