【发布时间】:2019-02-01 15:08:48
【问题描述】:
我从网上了解到有两种类型的 Cookie:持久性 Cookie 和非持久性 Cookie。如果我不指定过期时间,则会创建非持久性 Cookie。我还了解到,一旦关闭浏览器,非持久性 Cookie 就会被删除,但在我的情况下,即使我关闭了浏览器窗口,我仍然可以在没有成功登录的情况下导航到我的应用程序。
public ActionResult Index(Login userLogin)
{
if (ModelState.IsValid)
{
Login Login = loginBusinessLayer.GetUserLogin(userLogin.UserId, userLogin.UserName);
if (Login.UserPassword == userLogin.UserPassword)
{
Session["UserLogin"] = Login;
User user = userBusinessLayer.GetUser(Login.UserId);
Role role = roleBusinessLayer.Roles.Single(rle => rle.RoleId == user.RoleId);
Session["Role"] = role;
HttpCookie cookie = new HttpCookie("UserLogin");
cookie["LoginId"] = Convert.ToString(Login.UserId);
cookie["RoleId"] = Convert.ToString(role.RoleId);
Response.Cookies.Add(cookie);
return RedirectToAction("Success", "Login");
}
else
{
return View();
}
}
return View();
}
【问题讨论】:
-
请在您的 Chrome 开发者工具的响应中向我们展示 set-cookie。
-
你能提供一份文件让我学习如何做吗?
-
mkyong.com/computer-tips/… 向您展示如何查看
Response headers。一旦你在那里,勾选Preserve log。然后点击Clear(红色按钮旁边)。然后加载您的页面。单击第一个 Web 请求(滚动到顶部,靠近名称)。在标题下将有Response headers。与我们分享任何set-cookie条目。
标签: c# asp.net asp.net-mvc logging cookies