FormsAuthentication.Encrypt returns null

I was writing some ASP.NET authentication code last night and was trying to encrypt an authentication ticket using FormsAuthentication.Encrypt(). However, it kept returning null. I banged my head against the wall for about an hour and then went home. As so often happens, I came in in the morning and spotted the problem immediately: the authentication ticket's user data was null. Doh! I had only put null in there for test purposes!

C#:
  1. FormsAuthenticationTicket authTicket =
  2.     new FormsAuthenticationTicket(
  3.     1, // version
  4.     "stu@me.com", // email
  5.     DateTime.Now, // creation
  6.     DateTime.Now.AddMinutes(60),// Expiration
  7.     false, // Persistent
  8.     null); // User data (TICKET WON'T BE ENCRYPTED IF THIS IS NULL)
  9.  
  10. string encryptedTicket = FormsAuthentication.Encrypt(authTicket);

One Response to “FormsAuthentication.Encrypt returns null”

  1. Giorgio Galante Says:

    Thanks Stuart - I was banging my head on this for about 30 minutes before finding this post…

Leave a Reply