这里有一段代码从登录页的表单返回永久性cookie,并限制它的时效为7天: 
                  string url = FormsAuthentication.GetRedirectUrl ("Elmo", true); 
                  FormsAuthentication.SetAuthCookie ("Elmo", true); 
                  HttpCookie cookie = Response.Cookies[FormsAuthentication.FormsCookieName]; 
                  cookie.Expires = DateTime.Now + new TimeSpan (7, 0, 0, 0); 
                  Response.Redirect (url); 
可以通过改变TiemSpan的值来改变cookie的时效。 

解决方案 »

  1.   

    http://www.csharphelp.com/archives/archive179.html
      

  2.   

    设置:
    HttpCookie tmpCooki = new HttpCookie("NYJ");
    tmpCooki.Value="HI";tmpCooki.Expires=DateTime.Now.AddDays(1);
    Response.Cookies.Add(tmpCooki);HttpCookie t = new HttpCookie("N");
    t.Values.Add("UserName","Knight");
    t.Values.Add("Age","23");t.Expires=DateTime.Now.AddHours(1);
    Response.Cookies.Add(t);读取
    Label1.Text="";
    Label1.Text+=Request.Cookies["NYJ"].Value+"<br/>";
    Label1.Text+=Request.Cookies["N"]["UserName"]+"<br/>";
    Label1.Text+=Request.Cookies["N"]["Age"]+"<br/>";
      

  3.   

    这个问题已经解决了,看这个帖子,里面有读取Cookie和写入Cookie的代码:
    http://expert.csdn.net/Expert/topic/1082/1082426.xml?temp=.6507837
      

  4.   

    Response.Cookies("my")("UserName") = UserName
    Response.Cookies("my")("UserPassword") = UserPassword
    Response.Cookies("my").Expires = DateTime.Now.AddDays(30)
    '增加COOKIES的时间