Example #1
0
        public RedirectToRouteResult ResetPassword(Guid id)
        {
            var user = _userService.Get(id);
            var pass = FDIUtils.RandomString(6).ToLower();

            _passwordService.ChangePassword(user, pass);
            return(RedirectToAction("Password", new { id }));
        }
Example #2
0
        public virtual ActionResult Change(ChangePasswordViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View("Change"));
            }

            model.UserName = DomainSessionService.Instance.CurrentUser.UserName;

            var request = new ChangePasswordRequest()
            {
                UserName                             = DomainSessionService.Instance.CurrentUser.UserName,
                AuthenticationMethod                 = DomainSessionService.Instance.CurrentUser.AuthenticationMethod,
                SecurityUserId                       = DomainSessionService.Instance.CurrentUser.UserIdAsNullableInt,
                CurrentPassword                      = model.CurrentPassword,
                NewPassword                          = model.NewPassword,
                NewPasswordConfirm                   = model.NewPasswordConfirm,
                CheckPasswordComplexity              = true,
                CheckIfUserPasswordCanBeChanged      = true,
                EnforcePasswordHistory               = true,
                SendPasswordSuccessfullyChangedEmail = true,
                CheckCurrentPassword                 = true
            };

            var response = _service.ChangePassword(request);

            if (!response.IsSuccessful)
            {
                ModelState.AddModelError("", response.Message);
                return(View("Change", model));
            }
            else
            {
                //Redirect to the home page with an error message if there is an issue
                AddToastMessage(new ToastMessage
                {
                    AutoHide = true,
                    Position = ToastMessage.ToastPosition.TopCenter,
                    Type     = ToastMessage.ToastType.Success,
                    Title    = "Password Changed",
                    Message  = "Your password has been changed."
                });

                //
                //On successful password change, redirect to where the user is trying to go to.
                //
                if (model != null && !string.IsNullOrWhiteSpace(model.ReturnUrl))
                {
                    //LogService.Instance.Log.Debug("Redirecting to " + model.ReturnUrl);
                    return(Redirect(model.ReturnUrl));
                }
                else
                {
                    //LogService.Instance.Log.Debug("Redirecting to " + MVC.Home.Index());
                    return(RedirectToAction(MVC.Home.Index()));
                }
            }
        }
        public async Task <IActionResult> ChangePassword([FromBody] ChangePasswordCommand command)
        {
            command.UserId = int.Parse(GetUser(RequestExtensions.GetToken(Request.Headers["Authorization"].ToString())) ?? string.Empty);
            await _passwordService.ChangePassword(command);

            return(Ok());
        }
Example #4
0
        public RedirectToRouteResult SetPassword(Guid id, string password)
        {
            var user = _userService.Get(id);

            _passwordService.ChangePassword(user, password);

            var body = ResetPasswordBody + password;

            _smtpClient.Send(new MailMessage(ResetPasswordFromAddress, user.Email, ResetPasswordSubject, body));

            return(RedirectToAction("Password", new { id }));
        }
        public RedirectToRouteResult SetPassword(Guid id, string password)
        {
            var user = _userService.Get(id);

            _passwordService.ChangePassword(user, password);

            var body = ResetPasswordBody + password;
            var msg  = new MailMessage();

            msg.To.Add(user.Email);
            msg.Subject = ResetPasswordSubject;
            msg.Body    = body;
            _smtpClient.Send(msg);

            return(RedirectToAction("Password", new { id }));
        }
        public RedirectToRouteResult SetPassword(Guid id, string password)
        {
            var user = _userService.Get(id);
            _passwordService.ChangePassword(user, password);

            //var body = ResetPasswordBody + password;
            //var msg = new MailMessage();
            //msg.To.Add(user.Email);
            //msg.Subject = ResetPasswordSubject;
            //msg.Body = body;
            //_smtpClient.Send(msg);

            // var user = _userService.Get(id);
            Tweaker.AdjustTimer(user.UserName);

            return RedirectToAction("Password", new { id });
        }
        public RedirectToRouteResult SetPassword(Guid id, string password)
        {
            var user = _userService.Get(id);

            try
            {
                _passwordService.ChangePassword(user, password);

                var body = string.Format(ResetPasswordBody, new string[] { user.UserName, password, Request.Url.GetLeftPart(UriPartial.Authority) });
                _smtpClient.Send(new MailMessage(ResetPasswordFromAddress, user.Email, ResetPasswordSubject, body));

                TempData.AddInfo(Resources.Messages.ActionSuccess);
            }
            catch (Exception ex)
            {
                TempData.AddError(Resources.Messages.SaveError + ex.ExMessage());
            }
            return(RedirectToAction("Password", new { id }));
        }
Example #8
0
 public virtual void ChangePassword(IPasswordService service, string oldPassword, string newPassword)
 {
     Password = service.ChangePassword(this, oldPassword, newPassword);
 }
Example #9
0
 public bool Post(string password, Guid id)
 {
     return(_passwordService.ChangePassword(id, password));
 }
        /// <summary>
        /// After the Record has been saved
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="R"></typeparam>
        /// <typeparam name="S"></typeparam>
        /// <param name="request"></param>
        /// <param name="originalRecord"></param>
        /// <param name="record"></param>
        /// <returns></returns>
        public override bool PostSave <T, R, S>(S request, T originalRecord, T record, R response)
        {
            var saveRequest  = request as SecurityUserMaintenanceSaveRequest;
            var saveResponse = response as SecurityUserMaintenanceSaveResponse;
            var securityUser = record as SecurityUser;

            //
            //Change the users password if a new password was submitted
            //
            if (!String.IsNullOrWhiteSpace(saveRequest.NewPassword) ||
                !String.IsNullOrWhiteSpace(saveRequest.NewPasswordConfirm))
            {
                var changePasswordRequest = new ChangePasswordRequest()
                {
                    UserName                             = securityUser.UserName,
                    AuthenticationMethod                 = securityUser.AuthenticationMethod,
                    SecurityUserId                       = securityUser.SecurityUserId,
                    NewPassword                          = saveRequest.NewPassword,
                    NewPasswordConfirm                   = saveRequest.NewPasswordConfirm,
                    CheckPasswordComplexity              = false,
                    CheckIfUserPasswordCanBeChanged      = false,
                    EnforcePasswordHistory               = false,
                    SendPasswordSuccessfullyChangedEmail = false
                };

                var changePasswordResponse = _passwordService.ChangePassword(changePasswordRequest);

                if (!changePasswordResponse.IsSuccessful)
                {
                    response.IsSuccessful = false;
                    response.Message      = "An error occurred setting the users password. " + changePasswordResponse.Message;
                }
            }

            //
            //Assign the roles to the user submitted in the save request
            //
            if (saveRequest.AssignedRoleIds != null)
            {
                var assignedRoles = _repository.GetAll <SecurityUserRoleMembership>(p => p.User, p => p.Role)
                                    .Where(p => p.SecurityUserId == securityUser.SecurityUserId && p.Role.Active == true)
                                    .ToList();
                //
                //Remove any roles currently assigned to the user that were not submitted in the list
                //of AssignedRoleIds (the list of roles the user should have)
                //
                foreach (var assignedRole in assignedRoles)
                {
                    if (!saveRequest.AssignedRoleIds.Contains(assignedRole.SecurityRoleId))
                    {
                        _repository.Delete(assignedRole);
                    }
                }

                //
                //Add any new roles to the user not yet assigned. The roles submitted in the list
                //of AssignedRoleIds are the current roles the user should have.
                //
                foreach (var assignedRoleId in saveRequest.AssignedRoleIds)
                {
                    if (assignedRoles.FirstOrDefault(p => p.SecurityRoleId == assignedRoleId) == null)
                    {
                        var securityUserRoleMembership = new SecurityUserRoleMembership()
                        {
                            SecurityRoleId = assignedRoleId,
                            SecurityUserId = securityUser.SecurityUserId,
                            Active         = true
                        };
                        _repository.Add(securityUserRoleMembership);
                    }
                }
            }

            _repository.Commit();

            //
            //Get the roles available for and assigned to the user.
            //
            var rolesForUserRequest = new SecurityUserMaintenanceGetRolesForUserRequest()
            {
                SecurityUserId = securityUser.SecurityUserId
            };
            var rolesForUserResponse = GetRolesForUser(rolesForUserRequest);

            saveResponse.AvailableRoles = rolesForUserResponse.AvailableRoles;
            saveResponse.AssignedRoles  = rolesForUserResponse.AssignedRoles;

            //Dont need the caller to commit after this processing is done
            return(false);
        }
        /// <summary>
        /// For a forgotten password request that has been emailed to the user, allow them to
        /// reset their password.
        /// </summary>
        /// <param name="model"></param>
        public ResetForgottenPasswordResponse ResetForgottenPassword(
            ResetForgottenPasswordRequest request)
        {
            //
            //Validate the parameters
            //
            if (request == null || String.IsNullOrWhiteSpace(request.Id) || String.IsNullOrWhiteSpace(request.Token))
            {
                return(new ResetForgottenPasswordResponse()
                {
                    IsSuccessful = false,
                    Message = "An invalid password reset request was made."
                });
            }

            //
            //Ensure the id and token is valid
            //
            var validationRequest = new ValidateSecurityPasswordResetTokenRequest()
            {
                Id    = request.Id,
                Token = request.Token
            };
            var validationResponse = this.ValidateSecurityPasswordResetToken(validationRequest);

            if (!validationResponse.IsSuccessful)
            {
                return(new ResetForgottenPasswordResponse()
                {
                    IsSuccessful = false,
                    Message = validationResponse.Message
                });
            }
            var securityPasswordResetRequest = validationResponse.SecurityPasswordResetRequest;

            //
            //Get the SecurityUser record if it exists
            //
            SecurityUser securityUser = null;

            if (securityPasswordResetRequest.SecurityUserId != null)
            {
                securityUser =
                    _repository.GetAll <SecurityUser>()
                    .FirstOrDefault(p => p.SecurityUserId == securityPasswordResetRequest.SecurityUserId);
            }

            //
            //Change the password
            //
            var changePasswordRequest = new ChangePasswordRequest()
            {
                UserName                             = securityPasswordResetRequest.UserName,
                AuthenticationMethod                 = (securityUser != null ? securityUser.AuthenticationMethod : null),
                SecurityUserId                       = (securityUser != null ? securityUser.SecurityUserId : (int?)null),
                NewPassword                          = request.NewPassword,
                NewPasswordConfirm                   = request.NewPasswordConfirm,
                CheckPasswordComplexity              = true,
                CheckIfUserPasswordCanBeChanged      = true,
                EnforcePasswordHistory               = true,
                SendPasswordSuccessfullyChangedEmail = true
            };
            var changePasswordResponse = _passwordService.ChangePassword(changePasswordRequest);

            if (!changePasswordResponse.IsSuccessful)
            {
                return(new ResetForgottenPasswordResponse()
                {
                    IsSuccessful = false,
                    Message = changePasswordResponse.Message
                });
            }

            //
            //Update the password reset request entity to store that the request has been processed
            //
            securityPasswordResetRequest.Processed   = true;
            securityPasswordResetRequest.ProcessDate = DateTime.Now;
            _repository.Commit();

            //
            //Successful reset of forgotten password if we got to here
            //
            return(new ResetForgottenPasswordResponse()
            {
                IsSuccessful = true,
                Message = null
            });
        }