FormsAuthentication.Encrypt(ticket) returns null

December 5th, 2008 by ganton | Print

I’ve done a sample today and I need to create FormsAuthenticationTicket manually in order to make it inexpiable. I create specifying null for user data in the constructor.

   1: FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, FormsAuthentication.FormsCookieName,
   2:     DateTime.Now, expiredTime, persist, null, FormsAuthentication.FormsCookiePath);
   3:  
   4: HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket));

Firstly, I wasn’t able to continue to default page of the site and I was always returned to the login page. After checking that all of my code is ok I observed created cookie and what I’ve seen was that it doesn’t contain any data. Normally, it should contain encrypted ticket. Then I saw that the call of FormsAuthentication.Encrypt(ticket) returns null.

After replacing null with string.Empty in the constructor of FormsAuthenticationTicket all went fine.

   1: FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, FormsAuthentication.FormsCookieName,
   2:     DateTime.Now, expiredTime, persist, string.Empty, FormsAuthentication.FormsCookiePath);

I do not know what is the reason of such behavior but for me it is unnatural behavior.

Leave a Reply