none
FormsdAuthentication.GetSuthCookie(string str, bool boo)方法的使用 RRS feed

  • 问题

  • FormsdAuthentication.GetSuthCookie(Name.false);
    在不同的地方调用
    我给传相同的值为什么返回值的长度不一致
    2012年9月7日 3:41

答案

全部回复

  • FormsAuthenticationTicket 的创建会和 DateTime.UtcNow 有关联。

    2012年9月7日 8:32
  • private static HttpCookie GetAuthCookie(string userName, bool createPersistentCookie, string strCookiePath, bool hexEncodedTicket)
    {
           FormsAuthentication.Initialize();
          if (userName == null)
          {
                 userName = string.Empty;
           }
          if ((strCookiePath == null) || (strCookiePath.Length < 1))
          {
                 strCookiePath = FormsAuthentication.FormsCookiePath;
           }
           FormsAuthenticationTicket ticket1 = new FormsAuthenticationTicket(2, userName, DateTime.Now, DateTime.Now.AddMinutes((double) FormsAuthentication._Timeout), createPersistentCookie, string.Empty, strCookiePath);
          string text1 = FormsAuthentication.Encrypt(ticket1, hexEncodedTicket);
          if ((text1 == null) || (text1.Length < 1))
          {
                throw new HttpException(SR.GetString("Unable_to_encrypt_cookie_ticket"));
           }
           HttpCookie cookie1 = new HttpCookie(FormsAuthentication.FormsCookieName, text1);
           cookie1.HttpOnly = true;
           cookie1.Path = strCookiePath;
           cookie1.Secure = FormsAuthentication._RequireSSL;
          if (FormsAuthentication._CookieDomain != null)
          {
                 cookie1.Domain = FormsAuthentication._CookieDomain;
           }
          if (ticket1.IsPersistent)
          {
                 cookie1.Expires = ticket1.Expiration;
           }
          return cookie1;
    }

    -------------------------------------------------------------------------------------------------------------------------------------------------

     FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(2, "username",
                 DateTime.Now, DateTime.Now.AddHours(1), true, "", FormsAuthentication.FormsCookiePath);
            string ticketEncrypted = FormsAuthentication.Encrypt(ticket);
             HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, ticketEncrypted);
             authCookie.HttpOnly = true;
             authCookie.Path = FormsAuthentication.FormsCookiePath;
             authCookie.Secure = FormsAuthentication.RequireSSL;
             authCookie.Expires = ticket.Expiration;
             HttpContext.Current.Response.Cookies.Add(authCookie);


    • 已编辑 sen.wolf 2012年10月20日 3:22
    2012年10月17日 9:52