public Task CreateUser(User user)
        {
            try
            {
                Logger.Info($"UserService.CreateUser(User user); user: {user}");

                var password      = _passwordGeneratorService.Random();
                var paswordHashed = _passwordHasher.HashPassword(password);

                user.PasswordHash = paswordHashed;

                UnitOfWork.UserRepository.Insert(user);
                UnitOfWork.SaveChanges();

                _emailService.SendSingleEmail(user.Email, "Create new user.",
                                              $"Your new account: {user.UserName}, password: {password}");

                return(Task.CompletedTask);
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message);
                return(Task.FromException(ex));
            }
        }
Beispiel #2
0
        public async Task <ActionResult> ResetPassword(Guid userId, string code)
        {
            var findUserResult = await UserManager.FindByIdAsync(userId);

            if (findUserResult == null)
            {
                var notificationErrorUserId = new UserNotification
                {
                    Subject             = Constants.NotificationSubjectError,
                    Message             = Constants.NotificationMessageResetPasswordFailUserIdInvalid,
                    NavigateButtonTitle = Constants.NotificationNavigateButtonTitleForgotPassword,
                    NavigateButtonLink  = Constants.NotificationNavigateButtonLinkForgotPassword
                };
                return(RedirectToAction("Notification", "Account", notificationErrorUserId));
            }

            var newPassword = _passwordGeneratorService.Random();

            var resetResult = await UserManager.ResetPasswordAsync(userId, code, newPassword);

            if (resetResult.Succeeded)
            {
                await UserManager.SendEmailAsync(userId, "New password", $"Your new password is {newPassword}");

                var notificationSuccess = new UserNotification
                {
                    Subject             = Constants.NotificationSubjectSuccess,
                    Message             = Constants.NotificationMessageResetPasswordSuccess,
                    NavigateButtonTitle = Constants.NotificationNavigateButtonTitleLogin,
                    NavigateButtonLink  = Constants.NotificationNavigateButtonLinkLogin
                };
                return(RedirectToAction("Notification", "Account", notificationSuccess));
            }

            var notificationErrorToken = new UserNotification
            {
                Subject             = Constants.NotificationSubjectError,
                Message             = Constants.NotificationMessageResetPasswordFailTokenInvalid,
                NavigateButtonTitle = Constants.NotificationNavigateButtonTitleResendConfirmEmail,
                NavigateButtonLink  = Constants.NotificationNavigateButtonLinkResendConfirmEmail
            };

            return(RedirectToAction("Notification", "Account", notificationErrorToken));
        }