【问题标题】:ASP.Net MVC 5 Cookie loses expiration upon return from the browserASP.Net MVC 5 Cookie 在从浏览器返回时过期
【发布时间】:2017-05-26 01:06:00
【问题描述】:

这个简单的代码让我感到困惑。

通过这个控制器操作方法,我创建了一个 cookie,给它一个过期时间并将它设置为 HttpOnly。 cookie 被正确创建,添加到响应中,在浏览器调试器上看起来是正确的,但是在刷新后返回到相同的代码中时,会丢失过期和 HttpOnly 标志。 cookie 本身仍然存在,但值丢失了。如果我在访问浏览器后将 Request.Cookies["mycookie"] 看回到同一个控制器/操作方法中,那么值就消失了——尽管 cookie 本身并没有被删除。

如果有人理解这种行为,请解释一下,这里可能会发生什么-

public class HomeController : Controller
    {
        public ActionResult Index()
        {

           if (this.ControllerContext.HttpContext.Request.Cookies["mycookie"] == null)
            {
                HttpCookie cookie = Response.Cookies["mycookie"];
                cookie["mycookie"] = "test";
                cookie.Expires = DateTime.Now.AddDays(90);
                cookie.HttpOnly = true;   
               this.ControllerContext.HttpContext.Response.SetCookie(cookie);
            }

            return View();
        }

【问题讨论】:

    标签: c# asp.net asp.net-mvc cookies asp.net-mvc-5


    【解决方案1】:

    问题出在这一行:return View();

    无法设置 cookie,然后在与服务器的同一次往返中再次读取(服务器端)。因此,您需要创建第二个请求以使 cookie 可用。最简单的方法是通过调用 RedirectToAction 来强制执行第二个请求,尽管您可以使用一些巧妙的 AJAXy 方法来执行此操作,因此它看起来是同一个请求。

    有关工作示例,请参阅 this post - 这是写入和删除 cookie 的部分。

    public class CookieController : Controller
    {
    
        public ActionResult Create()
        {
            HttpCookie cookie = new HttpCookie("Cookie");
            cookie.Value = "Hello Cookie! CreatedOn: " + DateTime.Now.ToShortTimeString();
    
            this.ControllerContext.HttpContext.Response.Cookies.Add(cookie);
            return RedirectToAction("Index", "Home");
        }
    
        public ActionResult Remove()
        {
            if (this.ControllerContext.HttpContext.Request.Cookies.AllKeys.Contains("Cookie"))
            {
                HttpCookie cookie = this.ControllerContext.HttpContext.Request.Cookies["Cookie"];
                cookie.Expires = DateTime.Now.AddDays(-1);
                this.ControllerContext.HttpContext.Response.Cookies.Add(cookie);
            }
            return RedirectToAction("Index", "Home");
        }
    
    }
    

    Ashiquizzaman 也是正确的,因为您没有设置 cookie 的值,但这只是问题的一半。

    【讨论】:

      【解决方案2】:

      请看下面的代码。

      var request=this.ControllerContext.HttpContext.Request;
      var response =this.ControllerContext.HttpContext.Response;
      //OR
      // var request=System.Web.HttpContext.Current.Request;
      //var response =System.Web.HttpContext.Current.Response;
      if (request.Cookies["mycookie"] == null)
       {
          HttpCookie cookie= new HttpCookie("mycookie");
          cookie.Value = "test";//your problem hear.
          cookie.Expires = DateTime.Now.AddDays(90);
          cookie.HttpOnly = true;      
          response.Cookies.Add(cookie);        
        }
        else//need to update your cookies then use this block or not
       {
          HttpCookie cookie=Request.Cookies["mycookie"];
          cookie.Value = "test";//your problem hear.
          cookie.Expires = DateTime.Now.AddDays(90);
          cookie.HttpOnly = true;  
          //response.Cookies.SetCookie(cookie);
          response.Cookies.Set(cookie);//To update a cookie, you need only to set the cookie again using the new values.
       }
      

      希望对你有所帮助。

      【讨论】:

      • 感谢 Ashiquizzaman。由于我们已经在第一次将 cookie 添加到响应中,因此它应该在后续请求中可供我们使用,而无需再次设置它对吗?为什么服务器端每次收到请求都需要设置值?
      • 如果您需要更新 cookie,请使用 else 部分。否则你不需要它。@MadhuriMittal
      • @MadhuriMittal 对不起,你的 SetCookie 让我感到困惑。我认为您需要更新现有的 cookie。我用评论更新我的答案。
      • 我不得不大喊大叫——你应该永远在 MVC 中使用 System.Web.HttpContext.Current。 OP 做得对。
      • 感谢您的建议。我总是遵循这一点。
      猜你喜欢
      • 2012-08-20
      • 2017-07-22
      • 1970-01-01
      • 1970-01-01
      • 2017-05-08
      • 2015-07-30
      • 2012-05-31
      • 1970-01-01
      • 2021-12-22
      相关资源
      最近更新 更多