Пример #1
0
        public async Task <RegisterOutput> VerificationUserResetPasswprd(ResetPasswordDto input)
        {
            var result = new RegisterOutput();

            try
            {
                if (!input.NewPassword.Equals(input.ConfirmPassword))
                {
                    result.CanLogin        = false;
                    result.RegisterMessage = "New password and confirm password not match.";
                    return(result);
                }

                long userId = 0;
                try
                {
                    userId = Convert.ToInt32(await _encryptionDecryptionService.DecryptString(input.UserKey));
                }
                catch (Exception ex)
                {
                    result.CanLogin        = false;
                    result.RegisterMessage = "Invalid request.";
                    return(result);
                }

                var user = await _userManager.GetUserByIdAsync(userId);

                if (user == null)
                {
                    // Don't reveal that the user does not exist
                    result.CanLogin        = false;
                    result.RegisterMessage = "Invalid request.";
                    return(result);
                }

                var applicationUser = await _applicationUserReposatory.GetAll().FirstOrDefaultAsync(x => x.UserId == user.Id);

                if (applicationUser == null)
                {
                    // Don't reveal that the user does not exist
                    result.CanLogin        = false;
                    result.RegisterMessage = "Invalid request.";
                    return(result);
                }

                //add logic to check password history table and manage user password history

                CheckErrors(await _userManager.ResetPasswordAsync(user, input.Token.Replace(' ', '+'), input.NewPassword));


                //update application and user state
                applicationUser.IsActive          = true;
                applicationUser.IsPasswordCreated = true;
                await _applicationUserReposatory.UpdateAsync(applicationUser);

                //add user role that can access the general user content
                //TDDO

                await CurrentUnitOfWork.SaveChangesAsync();

                var isEmailConfirmationRequiredForLogin = await SettingManager.GetSettingValueAsync <bool>(AbpZeroSettingNames.UserManagement.IsEmailConfirmationRequiredForLogin);

                result.CanLogin        = user.IsActive && (user.IsEmailConfirmed || !isEmailConfirmationRequiredForLogin);
                result.RegisterMessage = "Your password successfully changed. Now you can login with your new credentials.";
                return(result);
            }
            catch (Exception ex)
            {
                result.CanLogin        = false;
                result.RegisterMessage = ex.Message;
                return(result);
            }
        }
Пример #2
0
        private async Task SendEmail(SendMailDto mailInput)
        {
            try
            {
                var encryptedEmailAddress = _configuration["EmailConfig:EncryptedEmailAddress"];
                var encryptedPassword     = _configuration["EmailConfig:EncryptedPassword"];
                var host            = _configuration["EmailConfig:Host"];
                var port            = _configuration["EmailConfig:Port"];
                var enableSSL       = _configuration["EmailConfig:EnableSSL"];
                var defaultCCEmails = _configuration["EmailConfig:DefaultCCEmails"];
                var defaultEmails   = _configuration["EmailConfig:DefaultEmails"];
                var defaultSubject  = _configuration["EmailConfig:DefaultSubject"];

                using (var client = new SmtpClient())
                {
                    var credential = new NetworkCredential
                    {
                        UserName = await _encryptionDecryptionService.DecryptString(encryptedEmailAddress),
                        Password = await _encryptionDecryptionService.DecryptString(encryptedPassword)
                    };

                    client.Credentials = credential;
                    client.Host        = host;
                    client.EnableSsl   = Convert.ToBoolean(enableSSL);

                    //Try port 587 instead of 465. Port 465 is technically deprecated.
                    client.Port = Convert.ToInt32(port);

                    using (var emailMessage = new MailMessage())
                    {
                        foreach (var email in mailInput.EmailAddresses)
                        {
                            emailMessage.To.Add(email);
                        }

                        emailMessage.From = new MailAddress(await _encryptionDecryptionService.DecryptString(encryptedEmailAddress));

                        if (_environment.IsDevelopment())
                        {
                            foreach (var ccEmail in defaultCCEmails.Split(','))
                            {
                                emailMessage.CC.Add(ccEmail);
                            }
                        }

                        emailMessage.Subject    = string.IsNullOrEmpty(mailInput.Subject)? defaultSubject : mailInput.Subject;
                        emailMessage.Body       = mailInput.Body;
                        emailMessage.IsBodyHtml = true;

                        // Include "Message-Id" header or your message will be treated as spam by Google.
                        emailMessage.Headers.Add("Message-Id", String.Concat("<", DateTime.Now.ToString("yyMMdd"), ".", DateTime.Now.ToString("HHmmss"), "@amniltech.com"));

                        client.Send(emailMessage);
                    }
                }
                await Task.CompletedTask;
            }
            catch (Exception ex)
            {
                throw new UserFriendlyException(ex.Message);
            }
        }