public static string GetConnectionStringFromUrl(string url, SslMode sslMode = SslMode.Require, bool trustServerCertificate = true)
        {
            if (url == null)
            {
                throw new ApiException("Url was null when trying to convert url to connection string", System.Net.HttpStatusCode.InternalServerError);
            }

            try
            {
                Uri      databaseUri = new Uri(url);
                string[] userInfo    = databaseUri.UserInfo.Split(':');


                ConnectionStringBuilder builder = new ConnectionStringBuilder
                {
                    Host     = databaseUri.Host,
                    Port     = databaseUri.Port,
                    Username = userInfo[0],
                    Password = userInfo[1],
                    Database = databaseUri.LocalPath.TrimStart('/'),
                    SslMode  = sslMode,
                    TrustServerCertificate = trustServerCertificate
                };

                return(builder.ToString());
            }
            catch
            {
                throw new ApiException(new { message = "Unknown error when url was being converted to connection string", urlLength = url.Length }, System.Net.HttpStatusCode.InternalServerError);
            }
        }
Пример #2
0
        /// <summary>
        /// Open connection to host on port.
        /// </summary>
        /// <param name="host">Host name to connect to.</param>
        /// <param name="port">Port to connect to.</param>
        /// <param name="isSsl">Enable SSL if it's an ssl</param>
        /// <param name="timeout">Determins the time that you allow your application to do transaction, default is 100000</param>
        /// <exception cref="ArgumentException"></exception>
        internal void Open(string host, int port = 465, SslMode isSsl = SslMode.None, int timeout = 100000)
        {
            if (string.IsNullOrWhiteSpace(host) || port <= 0)
            {
                throw new ArgumentException("Invalid Argument found.");
            }
            _host = host;
            _socket.Connect(host, port);
            _socket.SendTimeout    = timeout;
            _socket.ReceiveTimeout = timeout;
            if (isSsl == SslMode.Ssl)
            {
                var sslStream = new SslStream(_socket.GetStream(), true, _validationCallback, ClientCertificateSelectionCallback, EncryptionPolicy);

                if (clientcerts == null)
                {
                    sslStream.AuthenticateAsClient(host, null, _sslProtocol, CheckRevokation);
                }
                else
                {
                    sslStream.AuthenticateAsClient(host, clientcerts, _sslProtocol, CheckRevokation);
                }
                _writer    = new StreamWriter(sslStream, Encoding.ASCII);
                _reader    = new StreamReader(sslStream, Encoding.ASCII);
                _connected = true;
            }
            else
            {
                _reader    = new StreamReader(_socket.GetStream(), Encoding.ASCII);
                _writer    = new StreamWriter(_socket.GetStream(), Encoding.ASCII);
                _connected = true;
            }
        }
Пример #3
0
 /// <summary>
 /// Default constructor.
 /// </summary>
 /// <param name="target">Target host IP end point.</param>
 /// <param name="sslMode">SSL mode.</param>
 /// <param name="userName">Target host user name.</param>
 /// <param name="password">Target host password.</param>
 public Relay_Target(IPEndPoint target, SslMode sslMode, string userName, string password)
 {
     m_pTarget  = target;
     m_SslMode  = sslMode;
     m_UserName = userName;
     m_Password = password;
 }
Пример #4
0
        public static string StringValue(this SslMode mode)
        {
            string strValue;

            switch (mode)
            {
            case SslMode.Preferred:
                strValue = "1";
                break;

            case SslMode.Required:
                strValue = "2";
                break;

            case SslMode.VerifyCA:
                strValue = "3";
                break;

            case SslMode.VerifyFull:
                strValue = "4";
                break;

            default:
                strValue = "0";
                break;
            }

            return(strValue);
        }
Пример #5
0
 /// <summary>
 /// Default constructor.
 /// </summary>
 /// <param name="hostName">Target host name.</param>
 /// <param name="target">Target host IP end point.</param>
 /// <param name="sslMode">SSL mode.</param>
 /// <param name="userName">Target host user name.</param>
 /// <param name="password">Target host password.</param>
 public Relay_Target(string hostName,IPEndPoint target,SslMode sslMode,string userName,string password)
 {
     m_HostName = hostName;
     m_pTarget  = target;
     m_SslMode  = sslMode;
     m_UserName = userName;
     m_Password = password;
 }
Пример #6
0
 /// <summary>
 /// Default constructor.
 /// </summary>
 /// <param name="hostName">Host name.</param>
 /// <param name="ip">IP address to listen.</param>
 /// <param name="port">Port to listen.</param>
 /// <param name="sslMode">Specifies SSL mode.</param>
 /// <param name="sslCertificate">Certificate to use for SSL connections.</param>
 /// <exception cref="ArgumentNullException">Is raised when <b>ip</b> is null.</exception>
 public IPBindInfo(string hostName,
                   IPAddress ip,
                   int port,
                   SslMode sslMode,
                   X509Certificate2 sslCertificate)
     : this(hostName, BindInfoProtocol.TCP, ip, port, sslMode, sslCertificate)
 {
 }
Пример #7
0
        ///<summary>
        ///</summary>

        public SslMode ToSslMode(String Key, SslMode Default)
        {
            if (!connection_string_values.Contains(Key))
            {
                return(Default);
            }

            return((SslMode)Enum.Parse(typeof(SslMode), ToString(Key), true));
        }
Пример #8
0
 public DbSettings(
     string dbServer, string dbName, string dbUser,
     string dbPassword, SslMode dbUseSsl, int dbPort = 3306)
 {
     DbServer   = dbServer;
     DbName     = dbName;
     DbUser     = dbUser;
     DbPassword = dbPassword;
     DbPort     = dbPort;
 }
Пример #9
0
        public MySqlDb(string dbServer, string dbName, string dbUser,
                       string dbPassword, SslMode sslMode, int dbPort = DB_DEFAULT_PORT
                       )
        {
            var settings = new DbSettings(
                dbServer, dbName, dbUser, dbPassword, sslMode, dbPort
                );

            Initialize(settings);
        }
 public PostgreSqlConnectionStringBuilder(string db, string host, string password, string user, int port, bool pooling, bool trust, SslMode mode)
 {
     Database = db;
     Host     = host;
     Password = password;
     Username = user;
     Port     = port;
     Pooling  = pooling;
     TrustServerCertificate = trust;
     SslMode = mode;
 }
Пример #11
0
        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="hostName">Host name.</param>
        /// <param name="protocol">Bind protocol.</param>
        /// <param name="ip">IP address to listen.</param>
        /// <param name="port">Port to listen.</param>
        /// <param name="sslMode">Specifies SSL mode.</param>
        /// <param name="sslCertificate">Certificate to use for SSL connections.</param>
        /// <exception cref="ArgumentNullException">Is raised when <b>ip</b> is null.</exception>
        /// <exception cref="ArgumentException">Is raised when any of the arguments has invalid value.</exception>
        public IPBindInfo(string hostName,BindInfoProtocol protocol,IPAddress ip,int port,SslMode sslMode,X509Certificate2 sslCertificate)
        {
            if(ip == null){
                throw new ArgumentNullException("ip");
            }

            m_HostName     = hostName;
            m_Protocol     = protocol;
            m_pEndPoint    = new IPEndPoint(ip,port);
            m_SslMode      = sslMode;
            m_pCertificate = sslCertificate;
            if((sslMode == SslMode.SSL || sslMode == SslMode.TLS) && sslCertificate == null){
                throw new ArgumentException("SSL requested, but argument 'sslCertificate' is not provided.");
            }
        }
Пример #12
0
    public void Connect_with_Verify_and_callback_throws([Values(SslMode.VerifyCA, SslMode.VerifyFull)] SslMode sslMode)
    {
        var csb = new NpgsqlConnectionStringBuilder(ConnectionString)
        {
            SslMode = sslMode
        };

        var connection = CreateConnection(csb.ToString());

        connection.UserCertificateValidationCallback = (_, _, _, _) => true;

        var ex = Assert.ThrowsAsync <ArgumentException>(async() => await connection.OpenAsync()) !;

        Assert.That(ex.Message, Is.EqualTo(string.Format(NpgsqlStrings.CannotUseSslVerifyWithUserCallback, sslMode)));
    }
Пример #13
0
    public void Reject_self_signed_certificate([Values(SslMode.VerifyCA, SslMode.VerifyFull)] SslMode sslMode)
    {
        var csb = new NpgsqlConnectionStringBuilder(ConnectionString)
        {
            SslMode = sslMode,
            CheckCertificateRevocation = false,
        };

        using var _    = CreateTempPool(csb, out var connString);
        using var conn = new NpgsqlConnection(connString);

        var ex = Assert.Throws <NpgsqlException>(conn.Open) !;

        Assert.That(ex.InnerException, Is.TypeOf <AuthenticationException>());
    }
            public override string ToString()
            {
                StringBuilder stringBuilder = new StringBuilder();

                if (Host != null)
                {
                    stringBuilder.Append("Host=");
                    stringBuilder.Append(Host);
                    stringBuilder.Append(";");
                }

                if (Port != 0)
                {
                    stringBuilder.Append("Port=");
                    stringBuilder.Append(Port);
                    stringBuilder.Append(";");
                }

                if (Username != null)
                {
                    stringBuilder.Append("Username="******";");
                }

                if (Password != null)
                {
                    stringBuilder.Append("Password="******";");
                }

                if (Database != null)
                {
                    stringBuilder.Append("Database=");
                    stringBuilder.Append(Database);
                    stringBuilder.Append(";");
                }

                stringBuilder.Append("SSL Mode=");
                stringBuilder.Append(SslMode.ToString());
                stringBuilder.Append(";");

                stringBuilder.Append("Trust Server Certificate=");
                stringBuilder.Append(TrustServerCertificate);

                return(stringBuilder.ToString());
            }
Пример #15
0
        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="hostName">Host name.</param>
        /// <param name="protocol">Bind protocol.</param>
        /// <param name="ip">IP address to listen.</param>
        /// <param name="port">Port to listen.</param>
        /// <param name="sslMode">Specifies SSL mode.</param>
        /// <param name="sslCertificate">Certificate to use for SSL connections.</param>
        /// <exception cref="ArgumentNullException">Is raised when <b>ip</b> is null.</exception>
        /// <exception cref="ArgumentException">Is raised when any of the arguments has invalid value.</exception>
        public IPBindInfo(string hostName, BindInfoProtocol protocol, IPAddress ip, int port, SslMode sslMode, X509Certificate2 sslCertificate)
        {
            if (ip == null)
            {
                throw new ArgumentNullException("ip");
            }

            m_HostName     = hostName;
            m_Protocol     = protocol;
            m_pEndPoint    = new IPEndPoint(ip, port);
            m_SslMode      = sslMode;
            m_pCertificate = sslCertificate;
            if ((sslMode == SslMode.SSL || sslMode == SslMode.TLS) && sslCertificate == null)
            {
                throw new ArgumentException("SSL requested, but argument 'sslCertificate' is not provided.");
            }
        }
Пример #16
0
 public MailBuilder(string host, int port, string userName, string passWord, string senderEmailAddresss, string senderDisplayName, SslMode ssl = SslMode.None, bool useHtml = true, bool smime = false, MailPriority messagePriority = MailPriority.Normal, SendCompletedEventHandler onSendCallBack = null, bool encrypt = false, bool sign = false, AuthenticationType authenticationType = AuthenticationType.PlainText)
 {
     _passWord            = passWord;
     _ssl                 = ssl;
     _port                = port;
     _userName            = userName;
     _host                = host;
     _useHtml             = useHtml;
     _senderDisplayName   = senderDisplayName;
     _senderEmailAddresss = senderEmailAddresss;
     _implictSsl          = ssl;
     _smime               = smime;
     _onSendCallBack      = onSendCallBack;
     _encrypt             = encrypt;
     _sign                = sign;
     _messagePriority     = messagePriority;
     _authenticationType  = authenticationType;
 }
Пример #17
0
        /// <summary>
        /// Edit constructor.
        /// </summary>
        /// <param name="host">Smart host name or IP address.</param>
        /// <param name="port">Smart host port.</param>
        /// <param name="sslMode">SSL mode.</param>
        /// <param name="userName">Smart host user name.</param>
        /// <param name="password">Smart host password.</param>
        public wfrm_System_SmartHost(string host,int port,SslMode sslMode,string userName,string password)
        {
            InitUI();

            m_pServer.Text = host;
            m_pPort.Value = port;
            if(sslMode == SslMode.None){
                m_pSslMode.SelectedIndex = 0;
            }
            else if(sslMode == SslMode.SSL){
                m_pSslMode.SelectedIndex = 1;
            }
            else if(sslMode == SslMode.TLS){
                m_pSslMode.SelectedIndex = 2;
            }
            m_pUserName.Text = userName;
            m_pPassword.Text = password;
        }
Пример #18
0
        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="host">Smart host name or IP address.</param>
        /// <param name="port">Smart host port.</param>
        /// <param name="sslMode">Smart host SSL mode.</param>
        /// <param name="userName">Smart host user name.</param>
        /// <param name="password">Smart host password.</param>
        /// <exception cref="ArgumentNullException">Is raised when <b>host</b> is null.</exception>
        /// <exception cref="ArgumentException">Is raised when any of the arguments has invalid value.</exception>
        public Relay_SmartHost(string host,int port,SslMode sslMode,string userName,string password)
        {
            if(host == null){
                throw new ArgumentNullException("host");
            }
            if(host == ""){
                throw new ArgumentException("Argument 'host' value must be specified.");
            }
            if(port < 1){
                throw new ArgumentException("Argument 'port' value is invalid.");
            }

            m_Host     = host;
            m_Port     = port;
            m_SslMode  = sslMode;
            m_UserName = userName;
            m_Password = password;
        }
Пример #19
0
 /// <summary>
 /// Initiate and construct a smime mailer object
 /// </summary>
 /// <param name="host">URL address of mail server</param>
 /// <param name="port">Port address of mail server</param>
 /// <param name="userName">User name of user</param>
 /// <param name="passWord">User's password to login to server</param>
 /// <param name="senderEmailAddresss">Email address of sender</param>
 /// <param name="senderDisplayName">Name of sender</param>
 /// <param name="signingCertificate2">Certificate that is user for signing, Note: You can set this with the same cert you use for encryption</param>
 /// <param name="encryptionCertificate2">Certificate that is used for Encryption of mail</param>
 /// <param name="sslType">Defines type of Ssl that your server uses, if you want to send Implicit Ssl mail use ssl, if explicit it should be TLS</param>
 /// <param name="implictSsl">Use implicit ssl</param>
 /// <param name="sign">Do you need to sign mail?</param>
 /// <param name="toSigningCerts"></param>
 /// <param name="toEncryptionCerts"></param>
 /// <param name="authenticationType"></param>
 /// <param name="encrypt"></param>
 public SmimeMailer(string host, int port, string userName, string passWord, string senderEmailAddresss,
                    string senderDisplayName, X509Certificate2 signingCertificate2 = null,
                    X509Certificate2 encryptionCertificate2 = null, SslMode sslType = SslMode.None,
                    bool sign = false,
                    List <X509Certificate2> toSigningCerts = null, List <X509Certificate2> toEncryptionCerts = null,
                    AuthenticationType authenticationType  = AuthenticationType.Base64, bool encrypt         = true) : base(host, port, userName, passWord, sslType, authenticationType)
 {
     if (encryptionCertificate2 == null || toEncryptionCerts == null)
     {
         throw new ArgumentNullException("encryptionCertificate2");
     }
     if (sign && (signingCertificate2 == null || toSigningCerts == null))
     {
         throw new ArgumentNullException("signingCertificate2");
     }
     _encrypt = encrypt;
     _sign    = sign;
     From     = new SmimeMailAddress(senderEmailAddresss, senderDisplayName, encryptionCertificate2, signingCertificate2);
 }
 public AddEditSmartHostForm(string host, int port, SslMode sslMode, string userName, string password)
 {
     this.InitializeComponent();
     this.m_pServer.Text = host;
     this.m_pPort.Value  = port;
     if (sslMode == SslMode.None)
     {
         this.m_pSslMode.SelectedIndex = 0;
     }
     else if (sslMode == SslMode.SSL)
     {
         this.m_pSslMode.SelectedIndex = 1;
     }
     else if (sslMode == SslMode.TLS)
     {
         this.m_pSslMode.SelectedIndex = 2;
     }
     this.m_pUserName.Text = userName;
     this.m_pPassword.Text = password;
 }
Пример #21
0
        /// <summary>
        /// Edit constructor.
        /// </summary>
        /// <param name="host">Smart host name or IP address.</param>
        /// <param name="port">Smart host port.</param>
        /// <param name="sslMode">SSL mode.</param>
        /// <param name="userName">Smart host user name.</param>
        /// <param name="password">Smart host password.</param>
        public wfrm_System_SmartHost(string host, int port, SslMode sslMode, string userName, string password)
        {
            InitUI();

            m_pServer.Text = host;
            m_pPort.Value  = port;
            if (sslMode == SslMode.None)
            {
                m_pSslMode.SelectedIndex = 0;
            }
            else if (sslMode == SslMode.SSL)
            {
                m_pSslMode.SelectedIndex = 1;
            }
            else if (sslMode == SslMode.TLS)
            {
                m_pSslMode.SelectedIndex = 2;
            }
            m_pUserName.Text = userName;
            m_pPassword.Text = password;
        }
        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="host">Smart host name or IP address.</param>
        /// <param name="port">Smart host port.</param>
        /// <param name="sslMode">Smart host SSL mode.</param>
        /// <param name="userName">Smart host user name.</param>
        /// <param name="password">Smart host password.</param>
        /// <exception cref="ArgumentNullException">Is raised when <b>host</b> is null.</exception>
        /// <exception cref="ArgumentException">Is raised when any of the arguments has invalid value.</exception>
        public Relay_SmartHost(string host, int port, SslMode sslMode, string userName, string password)
        {
            if (host == null)
            {
                throw new ArgumentNullException("host");
            }
            if (host == "")
            {
                throw new ArgumentException("Argument 'host' value must be specified.");
            }
            if (port < 1)
            {
                throw new ArgumentException("Argument 'port' value is invalid.");
            }

            m_Host     = host;
            m_Port     = port;
            m_SslMode  = sslMode;
            m_UserName = userName;
            m_Password = password;
        }
Пример #23
0
 /// <summary>
 /// Construct an emailer object to send mime mail
 /// </summary>
 /// <param name="host">Host address of server</param>
 /// <param name="port">Port number of server</param>
 /// <param name="userName">User name</param>
 /// <param name="passWord">Password</param>
 /// <param name="sslType">Defines the type of encryption that your mail server uses</param>
 /// <param name="implictSsl">Indicate if the ssl is an implict ssl</param>
 /// <param name="authenticationType">Defines type of authentication that your smtp server uses</param>
 public MimeMailer(string host, int port = 465, string userName = null, string passWord = "", SslMode sslType = SslMode.None, AuthenticationType authenticationType = AuthenticationType.PlainText) : base(host, port)
 {
     Host         = host;
     Port         = port;
     User         = userName;
     Password     = passWord;
     base.SslType = sslType;
     if (sslType == SslMode.Auto)
     {
         SslType = DetectSslMode();
     }
     AuthenticationMode = authenticationType;
 }
        /// <summary>
        /// The function will modify private member only, not base[key].
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="value"></param>
        /// <returns>value, coerced as needed to the stored type.</returns>
        private object SetValue(Keywords keyword, object value)
        {
            try
            {
                switch (keyword)
                {
                case Keywords.Host:
                    return(this._host = Convert.ToString(value));

                case Keywords.Port:
                    return(this._port = Convert.ToInt32(value));

                case Keywords.Database:
                    return(this._database = Convert.ToString(value));

                case Keywords.UserName:
                    return(this._username = Convert.ToString(value));

                case Keywords.Password:
                    return(this._password = value as string);

                case Keywords.SSL:
                    return(this._ssl = ToBoolean(value));

                case Keywords.SslMode:
                    return(this._sslmode = ToSslMode(value));

                case Keywords.Timeout:
                    return(this._timeout = ToInt32(value, 0, TIMEOUT_LIMIT, keyword));

                case Keywords.SearchPath:
                    return(this._searchpath = Convert.ToString(value));

                case Keywords.BufferSize:
                    return(this._bufferSize = Convert.ToInt32(value));

                case Keywords.Pooling:
                    return(this._pooling = ToBoolean(value));

                case Keywords.ConnectionLifeTime:
                    return(this._connection_life_time = Convert.ToInt32(value));

                case Keywords.MinPoolSize:
                    return(this._min_pool_size = ToInt32(value, 0, POOL_SIZE_LIMIT, keyword));

                case Keywords.MaxPoolSize:
                    return(this._max_pool_size = ToInt32(value, 0, POOL_SIZE_LIMIT, keyword));

                case Keywords.SyncNotification:
                    return(this._sync_notification = ToBoolean(value));

                case Keywords.CommandTimeout:
                    return(this._command_timeout = Convert.ToInt32(value));

                case Keywords.Enlist:
                    return(this._enlist = ToBoolean(value));

                case Keywords.PreloadReader:
                    if (ToBoolean(value))
                    {
                        throw new NotSupportedException("The PreloadReader parameter is no longer supported. Please see https://github.com/npgsql/Npgsql/wiki/PreloadReader-Removal");
                    }
                    return(false);

                case Keywords.UseExtendedTypes:
                    if (ToBoolean(value))
                    {
                        throw new NotSupportedException("The UseExtendedTypes parameter is no longer supported. Please see https://github.com/npgsql/Npgsql/wiki/UseExtendedTypes-Removal");
                    }
                    return(false);

                case Keywords.IntegratedSecurity:
                    bool iS = ToIntegratedSecurity(value);
                    if (iS == true)
                    {
                        CheckIntegratedSecuritySupport();
                    }

                    return(this._integrated_security = ToIntegratedSecurity(iS));

                case Keywords.IncludeRealm:
                    return(this._includeRealm = ToBoolean(value));

                case Keywords.Compatible:
                    Version ver = new Version(value.ToString());
                    if (ver > THIS_VERSION)
                    {
                        throw new ArgumentException("Attempt to set compatibility with version " + value +
                                                    " when using version " + THIS_VERSION);
                    }
                    return(_compatible = ver);

                case Keywords.ApplicationName:
                    return(this._application_name = Convert.ToString(value));
                }
            }
            catch (InvalidCastException exception)
            {
                string exception_template = string.Empty;

                switch (keyword)
                {
                case Keywords.Port:
                case Keywords.Timeout:
                case Keywords.ConnectionLifeTime:
                case Keywords.MinPoolSize:
                case Keywords.MaxPoolSize:
                case Keywords.CommandTimeout:
                    exception_template = "expecting {0}=[Numeric] value in ConnectionString";
                    break;

                case Keywords.SSL:
                case Keywords.Pooling:
                case Keywords.SyncNotification:
                    exception_template = "expecting {0}=[True/False] value in ConnectionString";
                    break;
                }

                if (!string.IsNullOrEmpty(exception_template))
                {
                    string key_name = GetKeyName(keyword);

                    throw new ArgumentException(string.Format(exception_template, key_name), key_name, exception);
                }

                throw;
            }

            return(null);
        }
Пример #25
0
 private void Initialize(string dbServer, string dbName, string dbUser, string dbPassword, SslMode sslMode, string id)
 {
     if (this.db == null)
     {
         this.db = new MySqlDb(dbServer, dbName, dbUser, dbPassword, sslMode);
     }
 }
Пример #26
0
        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="hostName">Host name.</param>
        /// <param name="protocol">Bind protocol.</param>
        /// <param name="ip">IP address to listen.</param>
        /// <param name="port">Port to listen.</param>
        /// <param name="sslMode">Specifies SSL mode.</param>
        /// <param name="certificate">Certificate to use for SSL connections.</param>
        /// <exception cref="ArgumentNullException">Is raised when <b>ip</b> is null.</exception>
        /// <exception cref="ArgumentException">Is raised when any of the arguments has invalid value.</exception>
        public IPBindInfo(string hostName, BindProtocol protocol, IPAddress ip, int port, SslMode sslMode, X509Certificate2 certificate)
        {
            AssertUtil.ArgumentNotNull(ip, nameof(ip));
            AssertUtil.AssertNetworkPort(port, nameof(port));

            this.HostName = hostName;
            this.Protocol = protocol;
            this.EndPoint = new IPEndPoint(ip, port);

            this.SslMode     = sslMode;
            this.Certificate = certificate;
            if ((sslMode == SslMode.SSL || sslMode == SslMode.TLS) && certificate == null)
            {
                throw new ArgumentException($"SSL requested, but argument '{nameof(certificate)}' is not provided.");
            }
        }
        /// <summary>
        /// The function will modify private member only, not base[key].
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="value"></param>
        /// <returns>value, coerced as needed to the stored type.</returns>
        private object SetValue(Keywords keyword, object value)
        {
            try
            {
                switch (keyword)
                {
                    case Keywords.Host:
                        return this._host = Convert.ToString(value);
                    case Keywords.Port:
                        return this._port = Convert.ToInt32(value);
                    case Keywords.Protocol:
                        return this._protocol = ToProtocolVersion(value);
                    case Keywords.Database:
                        return this._database = Convert.ToString(value);
                    case Keywords.UserName:
                        return this._username = Convert.ToString(value);
                    case Keywords.Password:
                        this._password.Password = value as string;
                        return value as string;
                    case Keywords.SSL:
                        return this._ssl = ToBoolean(value);
                    case Keywords.SslMode:
                        return this._sslmode = ToSslMode(value);
            #pragma warning disable 618
                    case Keywords.Encoding:
                        return Encoding;
            #pragma warning restore 618
                    case Keywords.Timeout:
                        return this._timeout = ToInt32(value, 0, TIMEOUT_LIMIT, keyword);
                    case Keywords.SearchPath:
                        return this._searchpath = Convert.ToString(value);
                    case Keywords.Pooling:
                        return this._pooling = ToBoolean(value);
                    case Keywords.ConnectionLifeTime:
                        return this._connection_life_time = Convert.ToInt32(value);
                    case Keywords.MinPoolSize:
                        return this._min_pool_size = ToInt32(value, 0, POOL_SIZE_LIMIT, keyword);
                    case Keywords.MaxPoolSize:
                        return this._max_pool_size = ToInt32(value, 0, POOL_SIZE_LIMIT, keyword);
                    case Keywords.SyncNotification:
                        return this._sync_notification = ToBoolean(value);
                    case Keywords.CommandTimeout:
                        return this._command_timeout = Convert.ToInt32(value);
                    case Keywords.Enlist:
                        return this._enlist = ToBoolean(value);
                    case Keywords.PreloadReader:
                        return this._preloadReader = ToBoolean(value);
                    case Keywords.UseExtendedTypes:
                        return this._useExtendedTypes = ToBoolean(value);
                    case Keywords.IntegratedSecurity:
                        return this._integrated_security = ToIntegratedSecurity(value);
                    case Keywords.Compatible:
                        Version ver = new Version(value.ToString());
                        if (ver > THIS_VERSION)
                            throw new ArgumentException("Attempt to set compatibility with version " + value +
                                                        " when using version " + THIS_VERSION);
                        return _compatible = ver;
                    case Keywords.ApplicationName:
                        return this._application_name = Convert.ToString(value);
                    case Keywords.AlwaysPrepare:
                        return this._always_prepare = Convert.ToBoolean(value);
                }
            }
            catch (InvalidCastException exception)
            {
                string exception_template = string.Empty;

                switch (keyword)
                {
                    case Keywords.Port:
                    case Keywords.Timeout:
                    case Keywords.ConnectionLifeTime:
                    case Keywords.MinPoolSize:
                    case Keywords.MaxPoolSize:
                    case Keywords.CommandTimeout:
                        exception_template = resman.GetString("Exception_InvalidIntegerKeyVal");
                        break;
                    case Keywords.SSL:
                    case Keywords.Pooling:
                    case Keywords.SyncNotification:
                        exception_template = resman.GetString("Exception_InvalidBooleanKeyVal");
                        break;
                    case Keywords.Protocol:
                        exception_template = resman.GetString("Exception_InvalidProtocolVersionKeyVal");
                        break;
                }

                if (!string.IsNullOrEmpty(exception_template))
                {
                    string key_name = GetKeyName(keyword);

                    throw new ArgumentException(string.Format(exception_template, key_name), key_name, exception);
                }

                throw;
            }

            return null;
        }
Пример #28
0
 public ImapServer(int port, SslMode mode, X509Certificate certificate)
     : this(port)
 {
     SslMode     = mode;
     Certificate = certificate;
 }
        /// <summary>
        /// The function will modify private member only, not base[key].
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="value"></param>
        private void SetValue(Keywords keyword, object value)

        {
            string key_name = GetKeyName(keyword);


            try

            {
                switch (keyword)

                {
                case Keywords.Host:

                    this._host = Convert.ToString(value);

                    break;

                case Keywords.Port:

                    this._port = Convert.ToInt32(value);

                    break;

                case Keywords.Protocol:

                    this._protocol = ToProtocolVersion(value);

                    break;

                case Keywords.Database:

                    this._database = Convert.ToString(value);

                    break;

                case Keywords.UserName:

                    this._username = Convert.ToString(value);

                    break;

                case Keywords.Password:

                    this._password = Convert.ToString(value);

                    break;

                case Keywords.SSL:

                    this._ssl = ToBoolean(value);

                    break;

                case Keywords.SslMode:

                    this._sslmode = ToSslMode(value);

                    break;

#pragma warning disable 618
                case Keywords.Encoding:
                    break;
#pragma warning restore 618

                case Keywords.Timeout:

                    this._timeout = ToInt32(value, 0, TIMEOUT_LIMIT, key_name);

                    break;

                case Keywords.SearchPath:

                    this._searchpath = Convert.ToString(value);

                    break;

                case Keywords.Pooling:

                    this._pooling = ToBoolean(value);

                    break;

                case Keywords.ConnectionLifeTime:

                    this._connection_life_time = Convert.ToInt32(value);

                    break;

                case Keywords.MinPoolSize:

                    this._min_pool_size = (MaxPoolSize > 0) ? ToInt32(value, 0, MaxPoolSize, key_name) : Convert.ToInt32(value);

                    break;

                case Keywords.MaxPoolSize:

                    this._max_pool_size = ToInt32(value, 0, POOL_SIZE_LIMIT, key_name);

                    break;

                case Keywords.SyncNotification:

                    this._sync_notification = ToBoolean(value);

                    break;

                case Keywords.CommandTimeout:

                    this._command_timeout = Convert.ToInt32(value);

                    break;

                case Keywords.Enlist:

                    this._enlist = ToBoolean(value);

                    break;

                case Keywords.PreloadReader:

                    this._preloadReader = ToBoolean(value);

                    break;

                case Keywords.UseExtendedTypes:

                    this._useExtendedTypes = ToBoolean(value);

                    break;

                case Keywords.IntegratedSecurity:
                    this._integrated_security = ToIntegratedSecurity(value);
                    break;

                case Keywords.Compatible:
                    Version ver = new Version(value.ToString());
                    if (ver > THIS_VERSION)
                    {
                        throw new ArgumentException("Attempt to set compatibility with version " + value + " when using version " + THIS_VERSION);
                    }
                    _compatible = ver;
                    break;
                }
            }

            catch (InvalidCastException exception)

            {
                string exception_template = string.Empty;

                switch (keyword)

                {
                case Keywords.Port:

                case Keywords.Timeout:

                case Keywords.ConnectionLifeTime:

                case Keywords.MinPoolSize:

                case Keywords.MaxPoolSize:

                case Keywords.CommandTimeout:

                    exception_template = resman.GetString("Exception_InvalidIntegerKeyVal");

                    break;

                case Keywords.SSL:

                case Keywords.Pooling:

                case Keywords.SyncNotification:

                    exception_template = resman.GetString("Exception_InvalidBooleanKeyVal");

                    break;

                case Keywords.Protocol:

                    exception_template = resman.GetString("Exception_InvalidProtocolVersionKeyVal");

                    break;
                }

                if (!string.IsNullOrEmpty(exception_template))

                {
                    throw new ArgumentException(string.Format(exception_template, key_name), key_name, exception);
                }

                throw;
            }
        }
Пример #30
0
 /// <summary>
 /// Default constructor.
 /// </summary>
 /// <param name="hostName">Host name.</param>
 /// <param name="ip">IP address to listen.</param>
 /// <param name="port">Port to listen.</param>
 /// <param name="sslMode">Specifies SSL mode.</param>
 /// <param name="sslCertificate">Certificate to use for SSL connections.</param>
 /// <exception cref="ArgumentNullException">Is raised when <b>ip</b> is null.</exception>
 public IPBindInfo(string hostName,IPAddress ip,int port,SslMode sslMode,X509Certificate2 sslCertificate)
     : this(hostName,BindInfoProtocol.TCP,ip,port,sslMode,sslCertificate)
 {
 }
Пример #31
0
 ///<summary>
 ///</summary>
 
 public SslMode ToSslMode(String Key, SslMode Default)
 {
     if (! connection_string_values.Contains(Key))
     {
         return Default;
     }
     
     return (SslMode)Enum.Parse(typeof(SslMode), ToString(Key), true);
 }
Пример #32
0
		/// <summary>
		/// The function will modify private member only, not base[key].
		/// </summary>
		/// <param name="keyword"></param>
		/// <param name="value"></param>
		private void SetValue(Keywords keyword, object value)

		{
			string key_name = GetKeyName(keyword);

			try

			{
				switch (keyword)

				{
					case Keywords.Host:

						this._host = Convert.ToString(value);

						break;

					case Keywords.Port:

						this._port = Convert.ToInt32(value);

						break;

					case Keywords.Protocol:

						this._protocol = ToProtocolVersion(value);

						break;

					case Keywords.Database:

						this._database = Convert.ToString(value);

						break;

					case Keywords.UserName:

						this._username = Convert.ToString(value);

						break;

					case Keywords.Password:

						this._password = Convert.ToString(value);

						break;

					case Keywords.SSL:

						this._ssl = ToBoolean(value);

						break;

					case Keywords.SslMode:

						this._sslmode = ToSslMode(value);

						break;

#pragma warning disable 618
					case Keywords.Encoding:
						break;
#pragma warning restore 618

					case Keywords.Timeout:

						this._timeout = ToInt32(value, 0, TIMEOUT_LIMIT, key_name);

						break;

					case Keywords.SearchPath:

						this._searchpath = Convert.ToString(value);

						break;

					case Keywords.Pooling:
					    
						this._pooling = ToBoolean(value);
						
						break;

					case Keywords.ConnectionLifeTime:

						this._connection_life_time = Convert.ToInt32(value);

						break;

					case Keywords.MinPoolSize:

						this._min_pool_size = (MaxPoolSize > 0) ? ToInt32(value, 0, MaxPoolSize, key_name) : Convert.ToInt32(value);

						break;

					case Keywords.MaxPoolSize:

						this._max_pool_size = ToInt32(value, 0, POOL_SIZE_LIMIT, key_name);

						break;

					case Keywords.SyncNotification:

						this._sync_notification = ToBoolean(value);

						break;

					case Keywords.CommandTimeout:

						this._command_timeout = Convert.ToInt32(value);

						break;

					case Keywords.Enlist:

						this._enlist = ToBoolean(value);

						break;

					case Keywords.PreloadReader:

						this._preloadReader = ToBoolean(value);

						break;

					case Keywords.UseExtendedTypes:

						this._useExtendedTypes = ToBoolean(value);

                        break;
                    case Keywords.IntegratedSecurity:
                        this._integrated_security = ToIntegratedSecurity(value);
                        break;
				}
			}

			catch (InvalidCastException exception)

			{
				string exception_template = string.Empty;

				switch (keyword)

				{
					case Keywords.Port:

					case Keywords.Timeout:

					case Keywords.ConnectionLifeTime:

					case Keywords.MinPoolSize:

					case Keywords.MaxPoolSize:

					case Keywords.CommandTimeout:

						exception_template = resman.GetString("Exception_InvalidIntegerKeyVal");

						break;

					case Keywords.SSL:

					case Keywords.Pooling:

					case Keywords.SyncNotification:

						exception_template = resman.GetString("Exception_InvalidBooleanKeyVal");

						break;

					case Keywords.Protocol:

						exception_template = resman.GetString("Exception_InvalidProtocolVersionKeyVal");

						break;
				}

				if (!string.IsNullOrEmpty(exception_template))

				{
					throw new ArgumentException(string.Format(exception_template, key_name), key_name, exception);
				}

				throw;
			}
		}