Storing more information using FormsAuthentication.SetAuthCookie

You can add user data to the FormsAuthenticationTicket, then generate the cookie yourself.

There’s an example in the the MSDN documentation for FormsAuthenticationTicket.

EDIT

Note that when creating the ticket, you need to set the timeout, which in general you will want to be the same as the value configured in web.config. Unfortunately, in the Framework 3.5 or earlier, the FormsAuthentication class does not expose this timeout publicly. For a workaround, use one of the techniques described in the response to this connect feedback item.

UPDATE

That Connect feedback item is no longer there, sadly. Wish you had briefly described what the techniques were.

Yes, it’s a pity Microsoft has discarded historical Connect items. IIRC, the two techniques they suggested were:

  1. Use WebConfigurationManager to read the relevant configuration section and get the timeout value.

  2. Create a cookie using FormsAuthentication.GetAuthCookie, decrypt it using FormsAuthentication.Decrypt and inspect the generated FormsAuthenticationTicket.

Or upgrade to .NET 4.x where there is a FormsAuthentication.Timeout property.

See this question for more info

Leave a Comment