Storing object as profile data in ASP.NET forms authentication cookie

In ASP.NET, you are normally given just a string to store additional user data using the FormsAuthenticationTicket, this ticket also takes a UserId, but most times we need to store some form of structured data in cookies, such as an object.

The solution to this is JSON, serialize the object to a JSON string, save it as the UserData, then de-serialize it back to the object when needed.

Setting up the object and IIdentity

To make retrieving the data easily, I would create a custom identity that has the properties of the object you are storing. For eg.

You should also have a cast, that will convert the User Identity to your custom Identity. So I made an Extension to do this:

Setting the Data

You should have a FormsAuthenticationService, and it would look like this:


Using the Data

Now its simple to retrieve this data from the cookie:

You can use this inside your controller or your views, such as the above was used in the layout to welcome the user by their FirstName and LastName.