HttpCookie 过期和 IIS 设置 [英] HttpCookie Expires and IIS setting

查看:65
本文介绍了HttpCookie 过期和 IIS 设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Cookie 过期日期感到很疯狂,从概念上讲它很简单,但它在我的服务器上不起作用......我想这是一个让我发疯的 IIS 设置,你怎么看????

I am getting crazy with Cookie Expire date, conceptually it is pretty simple but it is not working on my server... I guess that's an IIS setting that's making me crazy, what do you think????

我从一个 MVC 3 示例项目开始,然后在主控制器中添加以下代码

I started with an MVC 3 sample project and I am adding the following code in the Home Controller

  public ActionResult Index()
    {
         var PersistentLogonCookieName = "test";
        var persistentLogon = DateTime.Now.ToLongTimeString(); 
         var persistentLogonCookie = new HttpCookie(PersistentLogonCookieName, persistentLogon);
           persistentLogonCookie.Expires = DateTime.UtcNow.AddDays(2);


        if (Request.Cookies[PersistentLogonCookieName] == null)
        {
            ViewBag.Message = "Cookie NOT Found!";

             persistentLogonCookie.Value = "added" + DateTime.Now.ToLongTimeString();
            Response.Cookies.Add(persistentLogonCookie);
        }
        else
        {
            ViewBag.Message = "Cookie Found!";

            Response.SetCookie(persistentLogonCookie);
        }
        return View();
    }

如果我在本地运行它,它在第二个请求后工作正常,我找到了 Cookie....如果我在服务器上运行,我总是找不到 Cookie....如果我评论 persistentLogonCookie.Expires = DateTime.UtcNow.AddDays(2); 它在服务器上也能正常工作.如果不是我的本地机器和服务器之间的不同 IIS 设置,我不知道该怎么想......在响应标头中,我有:

if I run it locally, it works fine after the second request I got Cookie Found.... if I run on the server I got always Cookie Not Found.... If I comment persistentLogonCookie.Expires = DateTime.UtcNow.AddDays(2); it works fine also on the server. I don't know what to think about if not that's a different IIS settings between my local machine and the server.... In the response header I have:

Set-Cookie  test=added4:23:39 PM; expires=Wed, 14-Aug-2013 15:23:39 GMT; path=/

但是当我在服务器上测试它时,cookie 没有添加到以下请求的 cookie 集合中,当我用本地 IIS 测试时它进入 cookie 集合......任何建议将不胜感激....

but the cookie is not added to the cookie collection of following requests when I test it on the server and it goes in the cookie collection when I test with my local IIS.... Any suggestion would be highly appreciated....

推荐答案

抱歉,我发现了问题所在.... 服务器在响应日期中返回了错误的 DATE....到期日期必须晚于响应日期才能将 cookie 添加到 cookie 集合中.

Sorry I found out what was the problem.... the server was returning the wrong DATE in the Response DATE.... The Expiry Date has to be AFTER the Response Date to add the cookie to the cookie collection.

这篇关于HttpCookie 过期和 IIS 设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆