Change the user data in FormsAuthenticationTicket programmatically

落花浮王杯 提交于 2019-12-20 08:44:30

问题


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 programmatically.

Please give me the solution for changing the data in HttpCookie programmatically.


回答1:


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

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

// Store UserData inside the Forms Ticket with all the attributes
// in sync with the web.config
var 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);


来源:https://stackoverflow.com/questions/3402909/change-the-user-data-in-formsauthenticationticket-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!