I need more data on certain pages depending on what the user is doing. right now i'm using sessions, but now i want to move everything to cookies. this is not much data, so I decided that I could all auth ticket / cookie
@ jorsh1 based on your assessment of @Portman, Ticket.UserData is not a place to store changing data. You do not want to constantly recreate the authentication ticket when switching from one page to another.
Use session data using a session service or Sql server. If you do not need data in the session, and the data is small and not sensitive, use a cookie. (*)
The canonical example of MS UserData is to store things like a list of roles so you can say things like "I think this user is an administrator", but if it's something like an administrator role, you probably databases to check before you implicitly trust what's in the cookie.
string plainTextUserData = fid.Ticket.UserData;
This only works inside your application because Asp.Net has already decrypted the ticket for you. However, if you want to set IIRC data, you need to recreate and reconnect the forms authentication cookie.
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket( currentTicket.Version + 1, currentUser.UserName, now, now.Add(formsAuthentication.Timeout), false, "new user data string", FormsAuthentication.FormsCookiePath); string hash = FormsAuthentication.Encrypt(ticket); HttpCookie cookie = new HttpCookie( FormsAuthentication.FormsCookieName, hash); Response.Cookies.Add(cookie);
Another application cannot decode this ticket unless it knows the decryption key, or it was forced. If you download application balance or use web gardens, you even need to keep the keys in sync.
* I am not a supporter of storing things in a session. Usually there is another way to save this data.
[edit] What I use for a session for:
The only thing I often find is to store a lightweight version of critical user data in a session-based session. However, we make it transparently loaded and do not duplicate what is on the Ticket.
Thus, we do not detect anything sensitive in cookies, and we do not rely on the session. We also established aggressive session remediation. Combined with the small amount of data stored in the session, the sessions do not cause problems for us, and since only one piece of code knows what is happening in the session, we can easily reorganize it.
Something else, the sessions are evil. I prefer to maintain the viewstate on the page that it needs, or else just keep the temporary state in the database.