解决cookie的Expires属性值总是0001-1-1 0:00:00的问题

给cookie的Expires赋值,发现读出值都是0001-1-1 0:00:00;跟踪调试发现不是转换问题,cookie读取后默认值就是0001-1-1 0:00:00。解决方法,设置过期项目:

 
HttpCookie cookie  =  System.Web.HttpContext.Current.Request.Cookies[ " User " ];
        
if  (cookie  !=   null )
        
{
            cookie[
"UserName"= _actors.ActorTitle;
            cookie[
"UserID"= _actors.ActorID.ToString();
            cookie[
"DateTime"= DateTime.Now.AddDays(30).ToString();
            System.Web.HttpContext.Current.Response.SetCookie(cookie);
        }

        
else
        
{
            cookie 
= new HttpCookie("User");
            cookie.Values.Add(
"UserName", _actors.ActorTitle);
            cookie.Values.Add(
"UserID", _actors.ActorID.ToString());
            cookie.Values.Add(
"DateTime", DateTime.Now.AddDays(30).ToString());
            System.Web.HttpContext.Current.Response.AppendCookie(cookie);
        }
       

你可能感兴趣的:(expires)