public IActionResult EditUserPassword([FromBody] UpdatedPassword photographer)
        {
            try
            {
                string email = photographer.email;

                Photographer dbUser = _database.GetPhotographerByEmail(email);
                if (dbUser != null)
                {
                    // Compare old password
                    if (_pwHelper.VerifyHashedPassword(dbUser.hashedPassword, photographer.oldPassword) == PasswordVerificationResult.Success)
                    {
                        string hash = _pwHelper.HashPassword(photographer.newPassword);
                        _database.UpdatePhotographerPassword(email, hash);

                        return(Ok(new { message = "Password updated" }));
                    }
                    else
                    {
                        return(Unauthorized(new { message = "Password provided is wrong" }));
                    }
                }
                else
                {
                    return(NotFound(new { message = "User email not found" }));
                }
            }
            catch (Exception err)
            {
                Console.WriteLine(err.ToString());
                return(StatusCode(500));
            }
        }
        public async Task <ActionResult> UpdatePassword(UpdatedPassword updatedPassword)
        {
            // Get the customer from the database based on the customer id from the claims via the access token
            Customer customer = await userManager.FindByIdAsync(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            // If the customer is found...
            if (customer != null)
            {
                // Update the password in the database
                IdentityResult result = await userManager.ChangePasswordAsync(customer, updatedPassword.CurrentPassword, updatedPassword.NewPassword);


                // If the password was successfully updated, return ok
                if (result.Succeeded)
                {
                    return(Ok());
                }
            }

            return(Conflict());
        }
Example #3
0
        public async Task <ActionResult> UpdatePassword(UpdatedPassword updatedPassword)
        {
            // Get the customer from the database based on the customer id from the claims via the access token
            Customer customer = await userManager.FindByIdAsync(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            // If the customer is found...
            if (customer != null)
            {
                // Update the password in the database
                IdentityResult result = await userManager.ChangePasswordAsync(customer, updatedPassword.CurrentPassword, updatedPassword.NewPassword);


                // If the password was successfully updated, return ok
                if (result.Succeeded)
                {
                    // Send a confirmation email that the customer's password has been changed
                    if (customer.EmailPrefPasswordChange == true)
                    {
                        emailService.AddToQueue(EmailType.PasswordChange, "Password change confirmation", new Recipient
                        {
                            FirstName = customer.FirstName,
                            LastName  = customer.LastName,
                            Email     = customer.Email
                        }, new EmailProperties {
                            Host = GetHost()
                        });
                    }



                    return(Ok());
                }
                else
                {
                    return(Conflict());
                }
            }

            return(BadRequest());
        }
Example #4
0
        public async Task <ActionResult> UpdatePassword(UpdatedPassword updatedPassword)
        {
            if (ModelState.IsValid && PasswordValid(updatedPassword.newPassword))
            {
                Customer customer = await userManager.FindByIdAsync(User.FindFirst(ClaimTypes.NameIdentifier).Value);

                if (customer != null)
                {
                    IdentityResult result = await userManager.ChangePasswordAsync(customer, updatedPassword.currentPassword, updatedPassword.newPassword);

                    if (result.Succeeded)
                    {
                        return(Ok());
                    }
                    else
                    {
                        return(Conflict());
                    }
                }
            }

            return(BadRequest());
        }