Я установил зашифрованный httpcookie с помощью FormsAuthentication.Но позже, когда я читаю свойство Expired FormsAuthenticationTicket, оно всегда становится истинным даже до того, как истечет срок годности.
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);
}
}
}>