Exemple #1
0
        public void RequestPasswordReset(string email, string siteUrl, Func <string, string> resetUrlGenerator)
        {
            if (string.IsNullOrEmpty(email))
            {
                throw new ArgumentNullException("email");
            }

            if (string.IsNullOrEmpty(siteUrl))
            {
                throw new ArgumentNullException("siteurl");
            }

            if (resetUrlGenerator == null)
            {
                throw new ArgumentNullException("resetUrlGenerator");
            }

            User user = _userService.GetUserByEmail(email);

            if (user == null)
            {
                throw new UserNotExistsException(string.Format(CultureInfo.InvariantCulture, "User with email {0} not found", email));
            }

            DateTime expiration = DateTime.UtcNow.Add(_webConfiguration.ResetPasswordRequestExpiration);

            ResetPasswordRequest request = _userService.CreatePasswordResetRequestForUser(user, expiration);
            string       link            = resetUrlGenerator(request.Token);
            TimeZoneData timeZoneData    = _timeZonesProvider.GetTimeZoneByIdOrDefault(user.TimeZone);
            var          templateData    = new Dictionary <string, object>
            {
                { "link", link },
                { "siteurl", siteUrl },
                { "username", user.Name },
                { "expire", new DateTimeOffset(request.Expiration).ToOffset(timeZoneData.Offset).ToString("dd.MM.yyyy HH:mm zzz") }
            };
            string emailBody    = _templateEngine.TransformTemplate(templateData, MailResources.RestorePasswordMailBody);
            string emailSubject = _templateEngine.TransformTemplate(templateData, MailResources.RestorePasswordMailSubject);

            _mailService.SendMessage(emailSubject, emailBody, _webConfiguration.RestorePasswordMailFrom, user.Email);
        }