In a recent project the role of the user was decided at a later point after login, based on some dropdown selection. But the FormsAuthenticationTicket was created at login as below
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, // Ticket version userName, // Username associated with ticket DateTime.Now, //Date/time issued DateTime.Now.AddMinutes(20), // Date/time to expire false, // "true"for a persistent user cookie userRole, // User-data, in this case the roles FormsAuthentication.FormsCookiePath);string hash = FormsAuthentication.Encrypt(ticket);HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName,// Name of auth cookie hash); //Hashed ticket
// Set the cookie's expiration time to the tickets expiration timeif (ticket.IsPersistent) cookie.Expires = ticket.Expiration;
// Add the cookie to the list for outgoing response
// Read the cookieHttpCookie cookie = FormsAuthentication.GetAuthCookie(Session[Constants.UserName].ToString(), true);// Decrypt the cookie to get ticketFormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value);// Create new ticket from old and update rolesFormsAuthenticationTicket newticket = new FormsAuthenticationTicket (ticket.Version, // Ticket version ticket.Name, // Username associated with ticket ticket.IssueDate, // Date/time issued ticket.Expiration, // Date/time to expire false, //"true" for a persistent user cookie DropDownListRole.SelectedItem.Text, //User-data, in this case the roles from a dropdown ticket.CookiePath);
// Encrypt the ticket and store it in the cookiecookie.Value = FormsAuthentication.Encrypt(newticket);
// Set the cookie's expiration time to the tickets expiration timeif (ticket.IsPersistent) cookie.Expires = newticket.Expiration;
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: