Exemplo n.º 1
0
        public async Task <IActionResult> ForgotPassword(ForgotPasswordViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var user = await _userManager.FindByEmailAsync(model.Email);

            if (user != null && await _userManager.IsEmailConfirmedAsync(user))
            {
                var token = await _userManager.GeneratePasswordResetTokenAsync(user);

                var passwordResetLink = Url.Action("ResetPassword", "Profile", new { email = model.Email, token = token }, Request.Scheme);
                var result            = await _manager.SendForgotPasswordEmailAsync(user, passwordResetLink);

                if (result)
                {
                    await _manager.LogUserActionToDatabaseAsync(user, UserActionType.ForgotPasswordEmailSent);
                }
            }
            else
            {
                _logger.LogWarning(EventIds.ForgotPasswordWrongEmail, string.Format(ProfileStrings.ForgotPasswordWrongEmail, model.Email));
            }

            ModelState.AddModelError(string.Empty, string.Format(ProfileStrings.ForgotPasswordSent, model.Email));
            return(View(model));
        }
        public async Task <IActionResult> Registration(RegistrationViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var user = new ApplicationUser
            {
                UserName = model.Username,
                Email    = model.Email
            };

            var result = await _userManager.CreateAsync(user, model.Password);

            if (!result.Succeeded)
            {
                foreach (var error in result.Errors)
                {
                    _logger.LogWarning(EventIds.RegistrationCreateUserError, string.Format(Strings.AccountError, error.Description, model.Username));
                    ModelState.AddModelError(string.Empty, error.Description);
                }

                return(View(model));
            }

            await _manager.LogUserActionToDatabaseAsync(user, UserActionType.Registration);

            var confirmationLink = await GenerateConfirmationLink(user);

            var isSent = await _manager.SendConfirmationEmailAsync(user, confirmationLink);

            if (!isSent)
            {
                ModelState.AddModelError(string.Empty, RegistrationStrings.NotifyUserConfirmationEmailSentError);
                return(View(model));
            }

            await _manager.LogUserActionToDatabaseAsync(user, UserActionType.ConfirmationEmailSent);

            ModelState.AddModelError(string.Empty, RegistrationStrings.NotifyUserConfirmationEmailSent);
            return(View(model));
        }
        public async Task <IActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var user = await GetUserFromModel(model);

            if (user == null)
            {
                _logger.LogWarning(EventIds.LoginError, LoginStrings.LoginUsernameError);
                ModelState.AddModelError(string.Empty, LoginStrings.InvalidLogin);
                return(View(model));
            }

            if (!user.EmailConfirmed && (await _userManager.CheckPasswordAsync(user, model.Password)))
            {
                ModelState.AddModelError(string.Empty, LoginStrings.EmailNotConfirmed);
                return(View(model));
            }

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

            if (!result.Succeeded)
            {
                ModelState.AddModelError(string.Empty, LoginStrings.InvalidLogin);
                _logger.LogWarning(EventIds.LoginError, LoginStrings.LoginPasswordError);
                return(View(model));
            }

            await _manager.LogUserActionToDatabaseAsync(user, UserActionType.Login, LoginStrings.LoginBuiltIn);

            if (string.IsNullOrWhiteSpace(returnUrl))
            {
                returnUrl = Url.Content("~/");
            }

            return(LocalRedirect(returnUrl));
        }