Change the user data in FormsAuthenticationTicket programmatically

前端 未结 1 989
悲哀的现实
悲哀的现实 2021-01-31 04:24

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,

相关标签:
1条回答
  • 2021-01-31 04:58

    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);
    
    0 讨论(0)
提交回复
热议问题