【问题标题】:Why is the non-persistent cookie not expired after closing the browser window?为什么关闭浏览器窗口后非持久cookie没有过期?
【发布时间】: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


【解决方案1】:

现代浏览器继续在后台运行,即使所有可见的窗口都已关闭。例如,Windows 上的 Google Chrome 需要从任务栏中的小通知图标关闭才能真正关闭。如果这样做了,会话 cookie 将被删除。

郑重声明,您的登录方法包含许多安全漏洞。我希望这只是一个例子。使用 asp.net 中的内置库:Asp.NET Identity 和 Owin 提供程序。

【讨论】:

  • 我从任务管理器中删除了浏览器,cookie仍然存在。
猜你喜欢
  • 2016-12-13
  • 2019-12-03
  • 2016-09-23
  • 1970-01-01
  • 2012-02-12
  • 2015-07-30
  • 2013-05-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多