/* * Located in Global.asax.cs */ protected void Application_AuthenticateRequest(Object sender, EventArgs e) { var userData if (HttpContext.Current.User == null) return; if (!HttpContext.Current.User.Identity.IsAuthenticated) return; var id = HttpContext.Current.User.Identity as FormsIdentity; if (id == null) return; FormsAuthenticationTicket ticket = id.Ticket; string userData = ticket.UserData; string[] roles = userData.Split(','); HttpContext.Current.User = new GenericPrincipal(id, roles); } /* * Located in an accessible extension class */ public static bool BuildCookie(this Userprofile userprofile, bool remember = true) { try { var roles = "A comma delimited string of roles"; FormsAuthentication.Initialize(); var ticket = new FormsAuthenticationTicket(1, userprofile.UserprofileID.ToString(), DateTime.Now, DateTime.Now.AddDays(90), remember, roles, FormsAuthentication.FormsCookiePath); var hashedTicket = FormsAuthentication.Encrypt(ticket); var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashedTicket); if (ticket.IsPersistent) cookie.Expires = ticket.Expiration; HttpContext.Current.Response.Cookies.Add(cookie); return true; } catch (Exception) { return false; } }