Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        public static async Task <User> StartSession(LoginModel login)
        {
            var user = GetByEmail(login.Email) ?? GetByLogin(login.Email);

            if (user == null || user.Status == DBStatus.Archive || user.Status == DBStatus.Error)
            {
                throw new KeyNotFoundException("User not found!");
            }
            var password = SMTPSetting.Current == null ? login.Password : Helper.Decript(login.Password, SMTPSetting.Current.PassKey);

            if (user.AuthType == UserAuthType.SMTP)
            {
                using (var smtpClient = new SmtpClient {
                    Timeout = 20000
                })
                {
                    smtpClient.ServerCertificateValidationCallback = (s, c, h, e) => true;
                    smtpClient.Connect(SMTPSetting.Current.Host, SMTPSetting.Current.Port, SMTPSetting.Current.SSL);
                    smtpClient.Authenticate(user.EMail, password);
                }
            }
            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, password))
                {
                    throw new Exception("Authentication fail!");
                }
            }
            else
            {
                if (!user.Password.Equals(Helper.GetSha512(password), StringComparison.Ordinal))
                {
                    throw new Exception("Authentication fail!");
                }
            }
            await RegisterSession(user, login);

            return(user);
        }