为什么在FormsAuthenticationTicket到期之前,asp.net FormsAuthenticationTicket的expired属性为true?

时间:2018-09-28 14:27:30

标签: asp.net cookies formsauthenticationticket

我通过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);
        }
    }
}>

0 个答案:

没有答案