コード例 #1
0
        /// <summary>
        /// Send the user an email that their password has been changed.
        /// <param name="request"></param>
        /// <returns></returns>
        public SendPasswordChangedEmailResponse SendPasswordChangedEmail(SendPasswordChangedEmailRequest request)
        {
            //
            //Validate parameters
            //
            if (request == null || (request.ADUser == null && request.SecurityUser == null))
            {
                return(new SendPasswordChangedEmailResponse()
                {
                    IsSuccessful = false,
                    Message = "An invalid request was specified to the SendPasswordChangedEmail service."
                });
            }

            //
            //Send the email
            //
            var adUser       = request.ADUser;
            var securityUser = request.SecurityUser;

            try
            {
                _tokenResolver.GetTokenResolutionProvider().ClearTokens();
                var tokenValues = new Dictionary <String, String>();

                //
                //Setup the values for the Token Resolver so the email parses properly
                //Override the email address and other attributes since it may come from an AD User,
                //we cant bank on this always being security user.
                //
                var firstName = (adUser != null ? adUser.FirstName : securityUser.FirstName);
                tokenValues.Add(TokenNames.FirstName.ToString(), firstName);
                var lastName = (adUser != null ? adUser.LastName : securityUser.LastName);
                tokenValues.Add(TokenNames.LastName.ToString(), lastName);
                var emailAddress = (adUser != null ? adUser.EmailAddress : securityUser.EmailAddress);
                tokenValues.Add(TokenNames.EmailAddress.ToString(), emailAddress);

                //
                //Process the email template and send it
                //
                var emailTemplate = _authenticationContentProvider.GetPasswordChangedEmail();
                _sendMail.MessageHtml = emailTemplate.Html;
                _sendMail.Subject     = _tokenResolver.TokenizeString(emailTemplate.EmailSubject,
                                                                      TokenResolutionObjectTypes.SecurityUser.ToString(),
                                                                      (securityUser != null ? securityUser.SecurityUserId.ToString() : null),
                                                                      tokenValues);
                _sendMail.Body = _tokenResolver.TokenizeString(emailTemplate.EmailBody,
                                                               TokenResolutionObjectTypes.SecurityUser.ToString(),
                                                               (securityUser != null ? securityUser.SecurityUserId.ToString() : null),
                                                               tokenValues);
                _sendMail.AddMessageTo(_tokenResolver.TokenizeString(emailTemplate.EmailTo,
                                                                     TokenResolutionObjectTypes.SecurityUser.ToString(),
                                                                     (securityUser != null ? securityUser.SecurityUserId.ToString() : null),
                                                                     tokenValues));
                _sendMail.AddMessageCC(_tokenResolver.TokenizeString(emailTemplate.EmailCC,
                                                                     TokenResolutionObjectTypes.SecurityUser.ToString(),
                                                                     (securityUser != null ? securityUser.SecurityUserId.ToString() : null),
                                                                     tokenValues));
                _sendMail.AddMessageBCC(_tokenResolver.TokenizeString(emailTemplate.EmailBCC,
                                                                      TokenResolutionObjectTypes.SecurityUser.ToString(),
                                                                      (securityUser != null ? securityUser.SecurityUserId.ToString() : null),
                                                                      tokenValues));
                var fromAddress = _tokenResolver.TokenizeString(emailTemplate.EmailFrom,
                                                                TokenResolutionObjectTypes.SecurityUser.ToString(),
                                                                (securityUser != null ? securityUser.SecurityUserId.ToString() : null),
                                                                tokenValues);
                var fromDisplayName = _tokenResolver.TokenizeString(emailTemplate.EmailFromDisplayName,
                                                                    TokenResolutionObjectTypes.SecurityUser.ToString(),
                                                                    (securityUser != null ? securityUser.SecurityUserId.ToString() : null),
                                                                    tokenValues);
                var from = new MailAddress(fromAddress, fromDisplayName);
                _sendMail.SetMessageFrom(from);
                _sendMail.Send();
            }
            catch (Exception e)
            {
                String errorMessage = String.Format("An error occurred sending the password changed email to user {0}",
                                                    (adUser != null ? adUser.UserName : securityUser.UserName));
                LogService.Instance.Log.Error(errorMessage, e);
                return(new SendPasswordChangedEmailResponse()
                {
                    IsSuccessful = false,
                    Message = errorMessage
                });
            }

            //
            //Everything is good if we got hee
            //
            return(new SendPasswordChangedEmailResponse()
            {
                IsSuccessful = true,
                Message = null
            });
        }
コード例 #2
0
        /// <summary>
        /// Change a users password
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ChangePasswordResponse ChangePassword(ChangePasswordRequest request)
        {
            try
            {
                //
                //Validate the parameters
                //
                if (request == null || String.IsNullOrWhiteSpace(request.UserName))
                {
                    return(new ChangePasswordResponse()
                    {
                        IsSuccessful = false,
                        Message = "An invalid password change request was made."
                    });
                }

                //
                //Validate the new passwords are equal
                //
                if (!request.NewPassword.Equals(request.NewPasswordConfirm))
                {
                    return(new ChangePasswordResponse()
                    {
                        IsSuccessful = false,
                        Message = "The passwords you entered must match."
                    });
                }

                //
                //Validate the new passwords meets complexity requirements
                //
                if (request.CheckPasswordComplexity)
                {
                    var passwordComplexityRequest = new CheckPasswordComplexityRequest()
                    {
                        Password = request.NewPassword,
                        UserName = request.UserName
                    };
                    var passwordComplexityResponse = CheckPasswordComplexity(passwordComplexityRequest);
                    if (!passwordComplexityResponse.IsSuccessful)
                    {
                        return(new ChangePasswordResponse()
                        {
                            IsSuccessful = false,
                            Message = passwordComplexityResponse.Message
                        });
                    }
                }

                //
                //Determine if this is for an AD user or a security user record
                //Get the SecurityUser Record if we have it
                //
                SecurityUser securityUser = null;
                ADUser       adUser       = null;
                if (request.SecurityUserId != null)
                {
                    securityUser =
                        _repository.GetAll <SecurityUser>()
                        .FirstOrDefault(p => p.SecurityUserId == request.SecurityUserId);
                }

                //
                //Get the AD user if AD authentication is enabled, and we dont have a security user
                //or we dont have a security user active directory guid
                //
                if (DomainApplicationService.Instance.FormsAuthenticationADEnabled &&
                    (securityUser == null || securityUser.ActiveDirectoryGuid == null))
                {
                    adUser = new ADUser(request.UserName);
                    if (adUser == null || !adUser.ValidUser)
                    {
                        adUser = null;
                    }
                }
                else if (DomainApplicationService.Instance.FormsAuthenticationADEnabled &&
                         securityUser.ActiveDirectoryGuid != null)
                {
                    adUser = new ADUser(securityUser.ActiveDirectoryGuid.Value);
                    if (adUser == null || !adUser.ValidUser)
                    {
                        adUser = null;
                    }
                }

                //
                //Ensure we found a user account to reset a password for
                //
                if (securityUser == null && adUser == null)
                {
                    return(new ChangePasswordResponse()
                    {
                        IsSuccessful = false,
                        Message = "A valid user could not be found to reset the password for."
                    });
                }

                //
                //Ensure the user account is active and not locked and its password can be reset
                //
                //
                if (request.CheckIfUserPasswordCanBeChanged)
                {
                    var canUserPasswordBeChangedRequest = new CanUserPasswordBeChangedRequest()
                    {
                        SecurityUser         = securityUser,
                        ADUser               = adUser,
                        AuthenticationMethod = request.AuthenticationMethod
                    };
                    var canUserPasswordBeChangedResponse = CanUserPasswordBeChanged(canUserPasswordBeChangedRequest);
                    if (!canUserPasswordBeChangedResponse.IsSuccessful)
                    {
                        return(new ChangePasswordResponse()
                        {
                            IsSuccessful = false,
                            Message =
                                String.Format(
                                    "{0} Please contact support to have the password changed.",
                                    canUserPasswordBeChangedResponse.Message)
                        });
                    }
                }

                DateTime?newPasswordExpirationDate = null;
                var      passwordChanged           = false;

                //
                //If AD user, connect to AD and reset the users password
                //Ensure the user is allowed to reset their password
                //
                if (adUser != null
                    &&
                    (securityUser == null ||
                     request.AuthenticationMethod == AuthenticationMethods.ActiveDirectory.ToString()))
                {
                    adUser.ChangePassword(request.NewPassword);
                    newPasswordExpirationDate = adUser.PasswordExpiryDate;
                    passwordChanged           = true;
                }

                //
                //If Security User, then reset the users password there
                //Generate a new passwordhash, salt, and update the password last changed date as well as password expiration date
                //Store the users current password in the password history table if its a SecurityUser record.
                //
                else if (securityUser != null
                         &&
                         (request.AuthenticationMethod == null ||
                          request.AuthenticationMethod == AuthenticationMethods.SecurityUser.ToString()))
                {
                    //
                    //Ensure the users current password matches what they typed into the screen
                    //
                    if (request.CheckCurrentPassword)
                    {
                        if (securityUser.PasswordHash !=
                            SHA256Hash.HashValue(request.CurrentPassword, securityUser.PasswordSalt))
                        {
                            return(new ChangePasswordResponse()
                            {
                                IsSuccessful = false,
                                Message = "An invalid current password was entered."
                            });
                        }
                    }

                    //
                    //Enforce password history, where a password cannot be one of the previously used passwords.
                    //
                    if (request.EnforcePasswordHistory)
                    {
                        //
                        //Ensure the new password is not the current password
                        //
                        if (securityUser.PasswordHash ==
                            SHA256Hash.HashValue(request.NewPassword, securityUser.PasswordSalt))
                        {
                            return(new ChangePasswordResponse()
                            {
                                IsSuccessful = false,
                                Message = "The new password cannot be the same as the current password."
                            });
                        }

                        //
                        //Validate that the new password is not in the password history
                        //as a previously used password
                        //
                        var passwordHistoryRecordsToCheck =
                            ApplicationService.Instance.PasswordHistoryRecordsToCheckOnPasswordChange;
                        if (passwordHistoryRecordsToCheck > 0)
                        {
                            var passwordHistory =
                                _repository.GetAll <SecurityUserPasswordHistory>()
                                .Where(p => p.SecurityUserId == securityUser.SecurityUserId)
                                .OrderBy(p => p.CreateDate)
                                .Take(passwordHistoryRecordsToCheck)
                                .ToList();
                            foreach (var p in passwordHistory)
                            {
                                if (p.PasswordHash == SHA256Hash.HashValue(request.NewPassword, p.PasswordSalt))
                                {
                                    return(new ChangePasswordResponse()
                                    {
                                        IsSuccessful = false,
                                        Message = "The new password cannot be one of the previously used passwords."
                                    });
                                }
                            }
                        }
                    }

                    //
                    //Create the password history entry for the users current password
                    //
                    var securityUserPasswordHistory = new SecurityUserPasswordHistory()
                    {
                        SecurityUserId = securityUser.SecurityUserId,
                        PasswordHash   = securityUser.PasswordHash,
                        PasswordSalt   = securityUser.PasswordSalt
                    };
                    _repository.Add(securityUserPasswordHistory);

                    //
                    //Update the security user record
                    //
                    securityUser.PasswordLastChangedDate = DateTime.Now;
                    securityUser.PasswordSalt            = SHA256Hash.CreateSalt(6);
                    securityUser.PasswordHash            = SHA256Hash.HashValue(request.NewPassword, securityUser.PasswordSalt);

                    if (securityUser.PasswordNeverExpires)
                    {
                        securityUser.PasswordExpirationDate = null;
                    }
                    else
                    {
                        var passwordExpirationDateResponse =
                            CalculatePasswordExpirationDate(new CalculatePasswordExpirationDateRequest());
                        securityUser.PasswordExpirationDate = passwordExpirationDateResponse.PasswordExpirationDate;
                    }
                    newPasswordExpirationDate = securityUser.PasswordExpirationDate;
                    passwordChanged           = true;
                }

                //
                //If for some reason the password couldnt be changed
                //
                if (!passwordChanged)
                {
                    return(new ChangePasswordResponse()
                    {
                        IsSuccessful = false,
                        Message =
                            "The password could not be changed. Please report this error. No AD User or SecurityUser record could be obtained to change the password for."
                    });
                }

                _repository.Commit();

                //
                //Repopulate the current users session values if needed
                //
                if (DomainSessionService.Instance != null && DomainSessionService.Instance.CurrentUser != null &&
                    request.UserName.Equals(DomainSessionService.Instance.CurrentUser.UserName))
                {
                    DomainSessionService.Instance.CurrentUser.PasswordExpirationDate  = newPasswordExpirationDate;
                    DomainSessionService.Instance.CurrentUser.PasswordLastChangedDate = DateTime.Now;
                }

                //
                //Send the user an email to confirm that their password has been reset
                //
                if (request.SendPasswordSuccessfullyChangedEmail)
                {
                    var sendPasswordChangedEmailRequest = new SendPasswordChangedEmailRequest()
                    {
                        SecurityUser = securityUser,
                        ADUser       = adUser
                    };
                    var sendPasswordChangedEmailResponse = SendPasswordChangedEmail(sendPasswordChangedEmailRequest);
                }

                //
                //Password change was successful if we got to here
                //
                return(new ChangePasswordResponse()
                {
                    IsSuccessful = true,
                    Message = null,
                    PasswordExpirationDate = newPasswordExpirationDate
                });
            }
            catch (Exception e)
            {
                LogService.Instance.Log.Error("An unhandled exception occurred changing the password. ", e);
                return(new ChangePasswordResponse()
                {
                    IsSuccessful = false,
                    Message = String.Format("An unhandled exception occurred changing the password. {0}", e.Message)
                });
            }
        }