Пример #1
0
        public IActionResult ChangePassword(string password = "", string repassword = "******")
        {
            if (password != repassword)
            {
                TempData["Notice"] = "Lỗi: Mật khẩu nhập không khớp";
                return(RedirectToAction("ChangePassword"));
            }

            string username = HttpContext.Session.GetString("AdminSession");

            if (username == null)
            {
                return(LocalRedirect("~/Identity/Account/Login"));
            }

            RES.Data.DBModels.Admin admin = _context.Admin.Where(n => n.UserName == username).SingleOrDefault();

            if (admin == null)
            {
                return(NotFound());
            }

            admin.PasswordHash = HashPwdTool.GeneratePassword(password);

            _context.SaveChangesAsync();

            TempData["Notice"] = "Đổi mật khẩu thành công";
            return(RedirectToAction("ChangePassword"));
        }
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");

            if (ModelState.IsValid)
            {
                // Start check Is Admin
                RealEstateSystemContext _context = new RealEstateSystemContext();
                RES.Data.DBModels.Admin ad       = _context.Admin.Where(n => n.UserName == Input.Email && HashPwdTool.CheckPassword(Input.Password, n.PasswordHash)).SingleOrDefault();

                if (ad != null)
                {
                    HttpContext.Session.SetString("AdminSession", ad.UserName);
                    return(RedirectToAction("Index", "Home", new { Area = "Admin" }));
                }
                // End check Is Admin

                var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure : true);

                if (result.Succeeded)
                {
                    try
                    {
                        using (RealEstateSystemContext db = new RealEstateSystemContext())
                        {
                            Customer customer = db.Customer.Where(n => n.Account.UserName == Input.Email).Single();
                            HttpContext.Session.SetString("User_Name_Session", customer.LastName + " " + customer.Firstname);
                        }
                    }
                    catch (Exception) { }

                    _logger.LogInformation("User logged in.");
                    return(LocalRedirect(returnUrl));
                }
                if (result.RequiresTwoFactor)
                {
                    return(RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, RememberMe = Input.RememberMe }));
                }
                if (result.IsLockedOut)
                {
                    _logger.LogWarning("User account locked out.");
                    return(RedirectToPage("./Lockout"));
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                    return(Page());
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }
Пример #3
0
        public async Task <IActionResult> Create([Bind("AdminId,UserName,PasswordHash")] RES.Data.DBModels.Admin admin)
        {
            if (ModelState.IsValid)
            {
                var adminDB = _context.Admin.Where(n => n.UserName == admin.UserName).SingleOrDefault();
                if (adminDB != null)
                {
                    TempData["Notice"] = "Lỗi: Tên tài khoản " + admin.UserName + " đã tồn tại.";
                    return(RedirectToAction("Create"));
                }
                admin.PasswordHash = HashPwdTool.GeneratePassword(admin.PasswordHash);
                _context.Add(admin);
                await _context.SaveChangesAsync();

                TempData["Notice"] = "Tạo admin " + admin.UserName + " thành công.";
                return(RedirectToAction(nameof(Index)));
            }
            return(View(admin));
        }