Beispiel #1
0
 public static void ChangePassword(User user, string password)
 {
     if (config == null)
     {
         config = SmtpSetting.Load();
     }
     user.Password = Helper.Decript(password, config.PassKey);
 }
Beispiel #2
0
 public static SmtpSetting Load()
 {
     if (current == null)
     {
         current = new SmtpSetting();
         var builder = new ConfigurationBuilder().AddJsonFile("smtpconfig.json");
         var config  = builder.Build();
         config.GetSection("Smtp").Bind(current);
     }
     return(current);
 }
Beispiel #3
0
        public static async Task <User> StartSession(NetworkCredential credentials)
        {
            if (config == null)
            {
                config = SmtpSetting.Load();
            }
            credentials.Password = Helper.Decript(credentials.Password, config.PassKey);
            var user = GetByEmail(credentials.UserName);

            if (user == null)
            {
                user = GetByLogin(credentials.UserName);
            }

            if (user == null || user.Status == DBStatus.Archive || user.Status == DBStatus.Error)
            {
                throw new KeyNotFoundException("User not found!");
            }

            if (user.AuthType == UserAuthType.SMTP)
            {
                using (var smtpClient = new SmtpClient {
                    Timeout = 20000
                })
                {
                    smtpClient.ServerCertificateValidationCallback = (s, c, h, e) => true;
                    smtpClient.Connect(config.Host, config.Port, config.SSL);
                    smtpClient.Authenticate(credentials);
                }
            }
            else if (user.AuthType == UserAuthType.LDAP)
            {
                var address = new System.Net.Mail.MailAddress(user.EMail);
                var domain  = address.Host.Substring(0, address.Host.IndexOf('.'));
                if (!LdapHelper.ValidateUser(domain, address.User, credentials.Password))
                {
                    throw new Exception("Authentication fail!");
                }
            }
            else
            {
                if (!user.Password.Equals(Helper.GetSha512(credentials.Password), StringComparison.Ordinal))
                {
                    throw new Exception("Authentication fail!");
                }
            }
            await StartSession(user);

            return(user);
        }