public async Task ResetPassword(string email, bool isForgotten, string password = null, string newPassword = null)
        {
            if (!isForgotten && string.IsNullOrEmpty(password) && string.IsNullOrEmpty(newPassword))
            {
                throw new InvalidParameterException("Empty password");
            }

            var dbUser = await FindByUsername(email);

            if (isForgotten)
            {
                var randomPassword = GetRandomPassword();
                dbUser.WasPasswordForgotten = true;
                dbUser.WasPasswordChanged   = true;
                dbUser.Password             = randomPassword;
                await _emailSenderService.SendResetPasswordEmail(dbUser, true);

                dbUser.Password = _hashingManager.GetHashedPassword(randomPassword);
            }
            else
            {
                if (!dbUser.Password.Equals(_hashingManager.GetHashedPassword(password)))
                {
                    throw new UnauthorisedException("Your actual password is incorrect");
                }

                dbUser.WasPasswordForgotten = false;
                dbUser.WasPasswordChanged   = false;
                dbUser.Password             = _hashingManager.GetHashedPassword(newPassword);
                await _emailSenderService.SendResetPasswordEmail(dbUser, false);
            }

            _context.Users.Update(dbUser);
            await _context.SaveChangesAsync();
        }
Example #2
0
        public async Task <Object> ForgotPassword(EmailForForgotPasswordDto emailDto)
        {
            var ip   = UsefulMethods.GetLocalIPAddress();
            var user = await _userManager.FindByEmailAsync(emailDto.Email);

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

                var callBackUrl = "http://" + ip + ":51044/api/resetPassword?userId=" + user.Id + "&token=" + token;

                string parent = Directory.GetParent(Directory.GetCurrentDirectory()).FullName;
                string path;

                path = Path.Combine(parent, "DeliveryApp\\wwwroot\\Templates\\EmailTemplates\\ResetPasswordEmail.html");


                var builder = new BodyBuilder();
                using (StreamReader SourceReader = System.IO.File.OpenText(path))
                {
                    builder.HtmlBody = SourceReader.ReadToEnd();
                }

                string messageBody = string.Format(
                    builder.HtmlBody,
                    callBackUrl
                    );

                await emailSenderService.SendResetPasswordEmail(emailDto.Email, messageBody);
            }
            return(Ok(new { message = "Email envoyé." }));
        }
Example #3
0
        public async Task ForgotPassword(string userName, string email)
        {
            var user = new User();

            if (!string.IsNullOrEmpty(email))
            {
                user = await _userManager.FindByEmailAsync(email);
            }
            else if (!string.IsNullOrEmpty(userName))
            {
                user = await _userManager.FindByNameAsync(userName);
            }

            if (user == null)
            {
                throw new Exception("User not found");
            }

            string code = await _userManager.GeneratePasswordResetTokenAsync(user);

            string newPassord  = StringUtils.GeneratePassword();
            var    resetResult = await _userManager.ResetPasswordAsync(user, code, newPassord);

            if (resetResult.Succeeded)
            {
                _emailSenderService.SendResetPasswordEmail(user.Email, newPassord);
            }
        }
Example #4
0
 public void Handle(SendResetPasswordEmailEvent sendInvitationEmailEvent)
 {
     Task.Run(() => _emailService.SendResetPasswordEmail(sendInvitationEmailEvent.Email, sendInvitationEmailEvent.ResetPasswordSecurity));
 }