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
Java
Normal
0falsefalsefalse
EN-US
X-NONE
X-NONE
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Table Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0in 5.4pt 0in 5.4pt;
mso-para-margin-top:0in;
mso-para-margin-right:0in;
mso-para-margin-bottom:10.0pt;
mso-para-margin-left:0in;
line-height:115%;
mso-pagination:widow-orphan;
font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;}
FormsAuthenticationTicketticket=newFormsAuthenticationTicket(1, // Ticket version
userName, // Username associated with ticket
DateTime.Now, //Date/time issued
DateTime.Now.AddMinutes(20), // Date/time to expirefalse, // "true"for a persistent user cookie
userRole, // User-data, in this case the roles
FormsAuthentication.FormsCookiePath);
stringhash= FormsAuthentication.Encrypt(ticket);
HttpCookiecookie=newHttpCookie(FormsAuthentication.FormsCookieName,// Name of auth cookie
hash); //Hashed ticket
// Set the cookie's expiration time to
the tickets expiration time if (ticket.IsPersistent) cookie.Expires =
ticket.Expiration;
// Add the cookie to the list for
outgoing response
HttpContext.Current.Response.Cookies.Add(cookie);
So to update the roles in the FormAuthenticationTicket we need to first read the ticket from cookie and create new one from that and then add role values as below
Java
Normal
0falsefalsefalse
EN-US
X-NONE
X-NONE
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Table Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0in 5.4pt 0in 5.4pt;
mso-para-margin-top:0in;
mso-para-margin-right:0in;
mso-para-margin-bottom:10.0pt;
mso-para-margin-left:0in;
line-height:115%;
mso-pagination:widow-orphan;
font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;}
// Read the cookieHttpCookiecookie= FormsAuthentication.GetAuthCookie(Session[Constants.UserName].ToString(), true);
// Decrypt the cookie to get ticketFormsAuthenticationTicketticket= FormsAuthentication.Decrypt(cookie.Value);
// Create new ticket from old and update rolesFormsAuthenticationTicketnewticket=newFormsAuthenticationTicket
(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 cookie cookie.Value = FormsAuthentication.Encrypt(newticket);
// Set the cookie's expiration time to
the tickets expiration time if (ticket.IsPersistent) cookie.Expires = newticket.Expiration;
// Add the cookie to the list for outgoing response
Nipun Tomar is competent and experienced project leader & architect with 12 years of experience in managing multi-disciplinary teams of varying sizes and complex programs of work. Has the ability to build strong relationships with all stakeholders and to turn proposals into reality. "Especially successful in…
Join the conversation! Your thoughts help the community grow.