protected void Application_PostAuthenticationRequest(Object sender, EventArgs e) { //Get httpCookie HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName]; if (authCookie != null) { FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authCookie.Value); if (!ticket.Expired) { UserViewModel model = JsonConvert.DeserializeObject <UserViewModel>(ticket.UserData); CustomPrinciple user = new CustomPrinciple(model.UserName); user.UserId = model.UserId; user.UserName = model.UserName; user.Name = model.Name; user.ContactNo = model.ContactNo; user.Roles = model.Roles; HttpContext.Current.User = user; } else { FormsAuthentication.SignOut(); Response.Redirect("~/Account/Login"); } } }
protected void Application_PostAuthenticateRequest(Object sender, EventArgs e) { try { HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName]; if (authCookie != null && !string.IsNullOrEmpty(authCookie.Value)) { FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value); if (authTicket != null) { var serializeModel = JsonConvert.DeserializeObject <UserMaster>(authTicket.UserData); var newUser = new CustomPrinciple(authTicket.Name) { ID = serializeModel.ID, FirstName = serializeModel.First_Name, LastName = serializeModel.Last_Name, Emailid = serializeModel.EmailId, Phone = serializeModel.Phone }; HttpContext.Current.User = newUser; } } } catch (CryptographicException cex) { FormsAuthentication.SignOut(); } }
public void testing_custom_role_type() { var identity = new ClaimsIdentity(); identity.AddClaim(new Claim("myRoleClaimType", "role1")); var principle = new CustomPrinciple(identity); Assert.IsTrue(principle.IsInRole("role1")); Assert.IsFalse(principle.IsInRole("role2")); }
protected void Application_PostAuthenticateRequest(Object sender, EventArgs e) { HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName]; if (authCookie != null) { FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value); CustomPrincipalSerializeModel serializeModel = JsonConvert.DeserializeObject <CustomPrincipalSerializeModel>(authTicket.UserData); CustomPrinciple newUser = new CustomPrinciple(authTicket.Name); newUser.UserId = serializeModel.UserId; newUser.roles = serializeModel.roles; newUser.UserName = serializeModel.UserName; HttpContext.Current.User = newUser; } }