views:

98

answers:

1

Hi, I am using the FormsAuthenticationTicket and place the data and passing the data across all the pages. and it will work if we are not changing any data.

So, now if i want to change the data and pass it for the cookie and encrypt then how to change the data progrmatically.

please give me the solution for changing the data in HttpCookie progrmatically.

Thanks and Regards, Vara Prasad.M

A: 

This is an example of how I modify an already-issued forms auth ticket:

HttpCookie cookie = FormsAuthentication.GetAuthCookie(Username, true);
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value);

// Store UserData inside the Forms Ticket with all the attributes in sync with the web.config
FormsAuthenticationTicket newticket = new FormsAuthenticationTicket(ticket.Version,
                                                                    ticket.Name,
                                                                    ticket.IssueDate,
                                                                    ticket.Expiration,
                                                                    true, // always persistent
                                                                    "User Data",
                                                                    ticket.CookiePath);

// Encrypt the ticket and store it in the cookie
cookie.Value = FormsAuthentication.Encrypt(newticket);
cookie.Expires = newticket.Expiration.AddHours(24);
this.Context.Response.Cookies.Set(cookie);
mdarnall
i want to change the user data only then how is it possible?
Vara Prasad.M
The same code, just modify the "User Data" value to your value. The process (as far as I understand it) is the same regardless. Get the cookie, decrypt the ticket, update it, encrypt it, set the cookie in the response.
mdarnall

related questions