public async Task <IActionResult> ForgotPassword(ForgotPasswordViewModel model)
        {
            if (ModelState.IsValid)
            {
                VesizleUser user = await _userManager.FindByEmailAsync(model.Email);

                if (user != null)
                {
                    string resetToken = await _userManager.GeneratePasswordResetTokenAsync(user);

                    EmailHelper emailHelper = new EmailHelper();
                    var         result      = emailHelper.SendEmailResetPassword(model.Email, user.Id, resetToken);
                    if (result.IsSuccessful)
                    {
                        ViewBag.EmailSent = Messages.EmailSent;
                        return(View(model));
                    }
                    ModelState.AddModelError(nameof(model.Email), result.Message);
                    return(View(model));
                }
                ModelState.AddModelError(nameof(model.Email), Messages.EmailNotFound);
                return(View(model));
            }
            return(View(model));
        }
        public async Task <IActionResult> ResetPassword(ResetPasswordViewModel model, string userId, string token)
        {
            if (ModelState.IsValid)
            {
                VesizleUser user = await _userManager.FindByIdAsync(userId);

                var            codeDecodedBytes = WebEncoders.Base64UrlDecode(token);
                var            codeDecoded      = Encoding.UTF8.GetString(codeDecodedBytes);
                IdentityResult result           = await _userManager.ResetPasswordAsync(user, codeDecoded, model.Password);

                if (result.Succeeded)
                {
                    var result2 = await _userManager.UpdateSecurityStampAsync(user);

                    if (result2.Succeeded)
                    {
                        return(RedirectToAction("Login"));
                    }
                    ModelState.AddIdentityError(result2.Errors);
                    return(View(model));
                }
                ModelState.AddIdentityError(nameof(model.RePassword), result.Errors);
                return(View(model));
            }
            return(View(model));
        }
        public async Task <IActionResult> Delete([Required] string id)
        {
            VesizleUser user = await _userManager.FindByIdAsync(id);

            if (user != null)
            {
                var result = await _userManager.DeleteAsync(user);

                if (result.Succeeded)
                {
                    await _signInManager.SignOutAsync();

                    return(Ok());
                }
                return(BadRequest(result.Errors));
            }
            return(View("Edit"));
        }
        public async Task <ActionResult> Login(LoginViewModel model)
        {
            if (ModelState.IsValid)
            {
                VesizleUser user = await _userManager.FindByEmailAsync(model.Email);

                if (user != null)
                {
                    await _signInManager.SignOutAsync();

                    var result = await _signInManager.PasswordSignInAsync(user, model.Password, model.RememberMe, false);

                    if (result.Succeeded)
                    {
                        return(RedirectToAction("Index", "Home"));
                    }
                }
                ModelState.AddModelError(nameof(model.Email), Messages.LoginFailed);
                return(View(model));
            }

            return(View(model));
        }
        public static async Task <IdentityResult> CustomValidateAsync(this IPasswordValidator <VesizleUser> validator,
                                                                      UserManager <VesizleUser> userManager, VesizleUser user, string password)
        {
            var identityResult = await validator.ValidateAsync(userManager, user, password);

            List <IdentityError> errors = identityResult.Succeeded ? new List <IdentityError>() : identityResult.Errors.ToList();

            if (password.ToLower().Contains(user.UserName.ToLower()))
            {
                errors.Add(new IdentityError
                {
                    Description = "Password cannot contain username"
                });
            }
            if (password.Contains("123"))
            {
                errors.Add(new IdentityError
                {
                    Description = "Password cannot contain 123 numeric sequence"
                });
            }
            return(errors.Count == 0 ? IdentityResult.Success : IdentityResult.Failed(errors.ToArray()));
        }