Cookie expiry date is always 1/1/0001

China☆狼群 提交于 2020-01-07 04:19:11

问题


Description:

I'm using cookie in mvc project to remember most recent download format selected by user. While creating cookie, I'm setting expiry date for that cookie. And when I try to get that cookie and expiry date of that cookie then I'm getting "1/1/0001" as expiry date always. I'm not getting the reason behind this. please help to reason behind this.

Code:

1) Setting cookie and it's expiry date:

 Response.Cookies.Add(new HttpCookie(base.LoggedOnUser.Email, exportFileFormat.ToString()));
                    var requestCookie = Request.Cookies[base.LoggedOnUser.Email];
                    if (requestCookie != null)
                    {
                        requestCookie.Expires = DateTime.UtcNow.AddDays(Convert.ToInt32(ConfigurationManager.AppSettings["FileFormatTypeCookieExpiry"]));
                    }

2) Getting cookie and it's expiry date:

 var fileFormatTypeCookie = HttpContext.Current.Request.Cookies[CurrentUser.Email];
                        if (fileFormatTypeCookie != null && fileFormatTypeCookie.Value != null)
                        {
                            var exportFileFormat = fileFormatTypeCookie.Value;
                            var expiry = fileFormatTypeCookie.Expires;

                        }

Above variable expiry is always "1/1/0001".


回答1:


I quote the answer from MikeSmithDev from a possible duplicate question:

Why is the cookie expiration date not surviving across sessions in ASP.NET?

The Short Answer - You cannot read the cookie's expiration date and time.

Slightly Longer Answer - This is not an issue of sessions in ASP.NET. It is an issue of what you can read from a cookie server-side in ASP.NET. Per the MSDN:

The browser is responsible for managing cookies, and the cookie's expiration time and date help the browser manage its store of cookies. Therefore, although you can read the name and value of a cookie, you cannot read the cookie's expiration date and time. When the browser sends cookie information to the server, the browser does not include the expiration information. (The cookie's Expires property always returns a date-time value of zero.)

You can read the Expires property of a cookie that you have set in the HttpResponse object, before the cookie has been sent to the browser. However, you cannot get the expiration back in the HttpRequest object. So basically, the cookie expiration date is set correctly. This can be verified by inspecting the cookie in the browser. Unfortunately, reading this cookie like in your Get function will return 1/1/0001.

If you really want to get the expiration, then you'd have to store it in the cookie itself:

Set

DateTime exp = DateTime.Now.AddDays(1); 
HttpCookie PreferredCookie = new HttpCookie("PreferredCookie"); 
PreferredCookie.Values.Add("cookieType", "Zref"); 
PreferredCookie.Values.Add("exp", exp.ToString());
PreferredCookie.Expires = exp; 
Response.Cookies.Set(PreferredCookie); 

Get

HttpCookie PreferredCookie = Request.Cookies["PreferredCookie"];
if (PreferredCookie != null)
{
  CookieLiteral.Text = "Value = " + PreferredCookie["cookieType"] + "<br>";
  CookieLiteral.Text += "Expires = " + PreferredCookie["exp"];
}
else
{
  CookieLiteral.Text = "No Cookie"; 
}


来源:https://stackoverflow.com/questions/44242283/cookie-expiry-date-is-always-1-1-0001

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!