code_junkie

asp.net cookies, authentication and session timeouts

Nov 13th, 2011
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. void AddUserIDToAuthCookie(string userID)
  2. {
  3. //There is no way to directly set the userdata portion of a FormAuthenticationTicket
  4. //without re-writing the login portion of the Login control
  5. //
  6. //I find it easier to pull the cookie that the Login control inserted out
  7. //and create a new cookie with the userdata set
  8.  
  9. HttpCookie authCookie = Response.Cookies[AUTH_COOKIE];
  10. if(authCookie == null)
  11. {
  12. return;
  13. }
  14.  
  15. Response.Cookies.Remove(AUTH_COOKIE);
  16.  
  17. FormsAuthenticationTicket oldTicket = FormsAuthentication.Decrypt(authCookie.Value);
  18. var newTicket =
  19. new FormsAuthenticationTicket(oldTicket.Version, oldTicket.Name, oldTicket.IssueDate, oldTicket.Expiration,
  20. oldTicket.IsPersistent, userID, oldTicket.CookiePath);
  21.  
  22. authCookie.Value = FormsAuthentication.Encrypt(newTicket);
  23.  
  24. Response.Cookies.Add(authCookie);
  25. }
  26.  
  27. FormsAuthenticationTicket ticket = ((FormsIdentity) Page.User.Identity).Ticket;
  28. string id = ticket.UserData;
Add Comment
Please, Sign In to add comment