SHARE
TWEET

Untitled

a guest Nov 14th, 2012 26 Never
  1. private void createCookie(string username, int customerID, bool persist)
  2. {
  3.     HttpCookie cookie = FormsAuthentication.GetAuthCookie(username, persist);
  4.     cookie.Expires = DateTime.Now.AddHours(12);
  5.     var ticket = FormsAuthentication.Decrypt(cookie.Value);
  6.     var userData = customerID.ToString();
  7.     var newTicket = new FormsAuthenticationTicket(ticket.Version, ticket.Name, ticket.IssueDate, ticket.Expiration, ticket.IsPersistent, userData);
  8.     cookie.Value = FormsAuthentication.Encrypt(newTicket);
  9.     Response.Cookies.Add(cookie);
  10. }
  11.        
  12. protected void Login1_LoggedIn(object sender, EventArgs e)
  13.     {
  14.         //... unimportant code left out
  15.  
  16.         //Update the users ticket with custom userInfo object
  17.         string userData = userInfo.Id.ToString("N");
  18.         HttpCookie cookie = Response.Cookies.Get(FormsAuthentication.FormsCookieName);
  19.         FormsAuthenticationTicket oldTicket = FormsAuthentication.Decrypt(cookie.Value);
  20.         FormsAuthenticationTicket newTicket =
  21.             new FormsAuthenticationTicket(
  22.                 oldTicket.Version,
  23.                 oldTicket.Name,
  24.                 oldTicket.IssueDate,
  25.                 oldTicket.Expiration,
  26.                 oldTicket.IsPersistent,
  27.                 userData,
  28.                 oldTicket.CookiePath);
  29.         cookie.Value = FormsAuthentication.Encrypt(newTicket);
  30.  }
RAW Paste Data
Top