예제 #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
            });
        }
        /// <summary>
        /// Send the user the email telling them instructions on how to reset their password
        /// </summary>
        /// <param name="request"></param>
        public SendForgotPasswordEmailResponse SendForgotPasswordEmail(SendForgotPasswordEmailRequest request)
        {
            //
            //Validate parameters
            //
            if (request == null || request.SecurityPasswordResetRequest == null || (request.ADUser == null && request.SecurityUser == null))
            {
                return(new SendForgotPasswordEmailResponse()
                {
                    IsSuccessful = false,
                    Message = "An invalid request was specified to the SendForgotPasswordEmail service."
                });
            }

            if (String.IsNullOrWhiteSpace(request.SecurityPasswordResetRequest.EmailAddress))
            {
                return(new SendForgotPasswordEmailResponse()
                {
                    IsSuccessful = false,
                    Message = "An invalid request was specified to the SendForgotPasswordEmail service. The email address to send to is empty."
                });
            }

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

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

                //
                //Create the URL for the user to reset their password with
                //
                String passwordResetId    = AESEncryption.Encrypt(passwordResetRequest.SecurityPasswordResetRequestId.ToString());
                String passwordResetToken = AESEncryption.Encrypt(passwordResetRequest.Token);

                //
                //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);
                tokenValues.Add(TokenNames.EmailAddress.ToString(), passwordResetRequest.EmailAddress);
                //We send both the encrypted ID and token to the user, making it harder for them to hack a reset entry
                String forgotPasswordUrl = String.Format("{0}Authentication/ForgotPassword/Reset?id={1}&t={2}",
                                                         DomainApplicationService.Instance.SiteBaseURL,
                                                         HttpUtility.UrlEncode(passwordResetId),
                                                         HttpUtility.UrlEncode(passwordResetToken));
                tokenValues.Add(TokenNames.ForgotPasswordURL.ToString(), forgotPasswordUrl);


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

            //
            //Everything is good if we got hee
            //
            return(new SendForgotPasswordEmailResponse()
            {
                IsSuccessful = true,
                Message = null
            });
        }