我通过FormsAuthentication设置了一个加密的httpcookie。但是稍后,当我阅读FormsAuthenticationTicket的Expired属性时,即使在到期日期之前,它也总是变为true。
if (Request.Cookies.Count == 0)
{
FormsAuthenticationTicket _ticket = new FormsAuthenticationTicket(1, "gary", DateTime.Now, DateTime.Now.AddMinutes(10), false, "vip");
string _encrptStr = FormsAuthentication.Encrypt(_ticket);
HttpCookie _cookie = new HttpCookie("myname", _encrptStr);
Response.Cookies.Add(_cookie);
Response.Write("cookie not found and is set now" + DateTime.Now.ToShortDateString());
}
else
{
HttpCookie _cookie = Request.Cookies["myname"];
if (_cookie == null)
{
Response.Write("cookie mynamenot found");
}
else
{
FormsAuthenticationTicket _ticket = FormsAuthentication.Decrypt(_cookie.Value);
if (_ticket.Expired == true)
{
Request.Cookies.Clear();
FormsAuthenticationTicket _ticket1 = new FormsAuthenticationTicket(1, "gary", DateTime.Now, DateTime.Now.AddMinutes(10), false, "vip");
string _encrptStr = FormsAuthentication.Encrypt(_ticket);
Response.Write(_ticket1.IssueDate + ":" + _ticket1.Expiration);
HttpCookie _cookie1 = new HttpCookie("myname", _encrptStr);
Response.Cookies.Add(_cookie);
Response.Write("<br/>cookie is expired and a new one is set" + DateTime.Now.ToShortDateString());
}
else
{
Response.Write("vip value:" + _ticket.UserData);
}
}
}>