以编程方式更改FormsAuthenticationTicket中的用户数据

Var*_*d.M 23 forms-authentication

我正在使用FormsAuthenticationTicket并放置数据并将数据传递到所有页面.如果我们不更改任何数据,它将起作用.

所以,现在如果我想更改数据并将其传递给cookie并加密,那么如何以编程方式更改数据.

请给我一个以HttpCookie编程方式更改数据的解决方案.

mda*_*all 46

这是我如何修改已发布的表单身份验证票证的示例:

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);
Run Code Online (Sandbox Code Playgroud)

  • 为什么将 cookie 过期设置为“newticket.Expiration.AddHours(24)”而不是“newticket.Expiration”? (2认同)