是否可以更新 FormsAuthentication cookie 值?

Is it possible to update FormsAuthentication cookie value?

我必须在登录时设置 FormsAuthentication cookie 值 (FormsAuthentication.SetAuthCookie(UserDesignation, false))。 现在我需要提供名称更改选项。因此,当用户更改他们的指定时,我需要将 FormsAuthentication cookie 值从旧指定更新为新指定。

可以吗?

如果是,我该怎么做?

您可以如下所示修改 cookie 数据,但我认为最好将角色保存在单独的 cookie 中并使用 FormsAuthentication cookie 对其进行身份验证

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

var newticket = new FormsAuthenticationTicket(ticket.Version,
                                              ticket.Name,
                                              ticket.IssueDate,
                                              ticket.Expiration,
                                              true, //persistent 
                                              "user data,designation",
                                              ticket.CookiePath);

cookie.Value = FormsAuthentication.Encrypt(newticket);
cookie.Expires = newticket.Expiration.AddHours(2);
HttpContext.Current.Response.Cookies.Set(cookie);