2014-10-10 65 views
1

我有一个'UI'设置来控制外观,用户可以更改设置,但是它涉及到更新cookie。c#更新Cookie,不强制用户登录

我似乎能够更新,但它强制用户再次进行身份验证,我如何更新cookie而无需让用户再次进行验证?

//We need to update the userToken as the menuOptionChanged 
      var usertoken2 = new UserToken(schedule.MinimisedMenuBool); 
      HttpCookie cookie = FormsAuthentication.GetAuthCookie(usertoken.UserName, false); 
      var ticket = FormsAuthentication.Decrypt(cookie.Value); 

      var newticket = new FormsAuthenticationTicket(ticket.Version,ticket.Name,ticket.IssueDate,ticket.Expiration,false,usertoken2.CalculateRawToken(),ticket.CookiePath); 

      // Encrypt the ticket and store it in the cookie 
      cookie.Value = FormsAuthentication.Encrypt(newticket); 
      System.Web.HttpContext.Current.Response.Cookies.Set(cookie); 

回答

1

为什么要在认证cookie中保存UI外观首选项?

把它作为一个单独的cookie保存有什么不妥吗?

HttpCookie menuCookie = new HttpCookie("menuCookie");  

menuCookie.Values.Add("menuAppearance", schedule.MinimisedMenuBool); 
menuCookie.Expires = DateTime.Now.AddYears(1); 

Response.Cookies.Add(menuCookie); 

然后,您可以选择仅为登录用户解析该cookie。

+0

好点,只是没有想到这一点。 – 2014-10-10 11:35:38