public override void OnActionExecuting(ActionExecutingContext filterContext) { AuditHelpers.Log(AuditingLevel); base.OnActionExecuting(filterContext); }
public static bool CustomSignIn(string userName, string password, bool RememberMeCheckBox) { ApplicationUserManager manager = HttpContext.Current.GetOwinContext().GetUserManager <ApplicationUserManager>(); ApplicationSignInManager signinManager = HttpContext.Current.GetOwinContext().GetUserManager <ApplicationSignInManager>(); var user = manager.Find <ApplicationUser, string>(userName, password); if (user != null && user.Locked == false) { // Validate the user password IAuthenticationManager authenticationManager = HttpContext.Current.GetOwinContext().Authentication; //authenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie); authenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie); ClaimsIdentity identity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie); //Claims added to ClaimsIdentity getting lost in ASP.NET Core Identity System //identity.AddClaim(new Claim("FullName", user.FullName ?? "")); //identity.AddClaim(new Claim("Email", user.Email ?? "")); //identity.AddClaim(new Claim("ImagePath", user.ImagePath ?? "")); identity.AddClaims(new[] { new Claim("FullName", user.FullName ?? ""), new Claim("Email", user.Email ?? ""), new Claim("ImagePath", user.ImagePath ?? ""), }); AuthenticationProperties props = new AuthenticationProperties(); props.IsPersistent = RememberMeCheckBox; authenticationManager.SignIn(props, identity); UserInfoVM userItem = new UserInfoVM() { UserName = user.UserName ?? "", FullName = user.FullName ?? "", Email = user.Email ?? "", ImagePath = user.ImagePath ?? "", Roles = string.Join(";", identity.Claims.Where(c => c.Type == ClaimTypes.Role).Select(c => c.Value).ToList()), IsAuthenticated = identity.IsAuthenticated }; HttpContext.Current.Session["User"] = userItem; //HttpContext.Current.Session["User"] = user; // Mock user data // Nếu chọn lưu thông tin, đưa vào cookie if (RememberMeCheckBox == true) { HttpContext.Current.Response.Cookies["USERNAME"].Value = userName; HttpContext.Current.Response.Cookies["USERNAME"].Expires = DateTime.Now.AddMonths(1); HttpContext.Current.Response.Cookies["PASSWORD"].Value = password; HttpContext.Current.Response.Cookies["PASSWORD"].Expires = DateTime.Now.AddMonths(1); } // Nếu không chọn lưu thông tin, xóa cookie else { HttpContext.Current.Response.Cookies["USERNAME"].Expires = DateTime.Now.AddMonths(-1); HttpContext.Current.Response.Cookies["PASSWORD"].Expires = DateTime.Now.AddMonths(-1); } //chuyển đến trang chính của hệ thống //Context.User = mUserInfo; AuditHelpers.Log(0, identity.Name); return(true); } else { return(false); } }