private static NameValueCollection CreateNameValueCollectionFromConnectionString(string connectionString)
        {
            var settings = new NameValueCollection();

            if (!string.IsNullOrWhiteSpace(connectionString))
            {
                var connection = KeyValueConfigurationManager.KeyDelimiter + connectionString;
                var keyValues  = Regex.Split(connection, KeyValueConfigurationManager.KeyDelimiterRegexString, RegexOptions.IgnoreCase);
                if (keyValues.Length > 0)
                {
                    // Regex.Split returns the array that include part of the delimiters, so it will look
                    // something like this:
                    // { "", "Endpoint", "sb://a.b.c", "OperationTimeout", "01:20:30", ...}
                    // We should always get empty string for first element (except if we found no match at all).
                    if (!string.IsNullOrWhiteSpace(keyValues[0]))
                    {
                        throw new ConfigurationException(SRClient.AppSettingsConfigSettingInvalidKey(connectionString));
                    }

                    if (keyValues.Length % 2 != 1)
                    {
                        throw new ConfigurationException(SRClient.AppSettingsConfigSettingInvalidKey(connectionString));
                    }

                    for (var i = 1; i < keyValues.Length; i++)
                    {
                        var key = keyValues[i];
                        if (string.IsNullOrWhiteSpace(key) || !KeyRegex.IsMatch(key))
                        {
                            throw new ConfigurationException(SRClient.AppSettingsConfigSettingInvalidKey(key));
                        }

                        var value = keyValues[i + 1];
                        if (string.IsNullOrWhiteSpace(value) || !ValueRegex.IsMatch(value))
                        {
                            throw new ConfigurationException(SRClient.AppSettingsConfigSettingInvalidValue(key, value));
                        }

                        if (settings[key] != null)
                        {
                            throw new ConfigurationException(SRClient.AppSettingsConfigDuplicateSetting(key));
                        }

                        settings[key] = value;
                        i++;
                    }
                }
            }

            return(settings);
        }
Example #2
0
        private static NameValueCollection CreateNameValueCollectionFromConnectionString(string connectionString)
        {
            NameValueCollection nameValueCollection = new NameValueCollection();

            if (!string.IsNullOrWhiteSpace(connectionString))
            {
                string   str       = string.Concat(";", connectionString);
                string[] strArrays = Regex.Split(str, ";(OperationTimeout|Endpoint|RuntimePort|ManagementPort|StsEndpoint|WindowsDomain|WindowsUsername|WindowsPassword|OAuthDomain|OAuthUsername|OAuthPassword|SharedSecretIssuer|SharedSecretValue|SharedAccessKeyName|SharedAccessKey|TransportType)=", RegexOptions.IgnoreCase);
                if ((int)strArrays.Length > 0)
                {
                    if (!string.IsNullOrWhiteSpace(strArrays[0]))
                    {
                        throw new ConfigurationErrorsException(SRClient.AppSettingsConfigSettingInvalidKey(connectionString));
                    }
                    if ((int)strArrays.Length % 2 != 1)
                    {
                        throw new ConfigurationErrorsException(SRClient.AppSettingsConfigSettingInvalidKey(connectionString));
                    }
                    for (int i = 1; i < (int)strArrays.Length; i++)
                    {
                        string str1 = strArrays[i];
                        if (string.IsNullOrWhiteSpace(str1) || !KeyValueConfigurationManager.KeyRegex.IsMatch(str1))
                        {
                            throw new ConfigurationErrorsException(SRClient.AppSettingsConfigSettingInvalidKey(str1));
                        }
                        string str2 = strArrays[i + 1];
                        if (string.IsNullOrWhiteSpace(str2) || !KeyValueConfigurationManager.ValueRegex.IsMatch(str2))
                        {
                            throw new ConfigurationErrorsException(SRClient.AppSettingsConfigSettingInvalidValue(str1, str2));
                        }
                        if (nameValueCollection[str1] != null)
                        {
                            throw new ConfigurationErrorsException(SRClient.AppSettingsConfigDuplicateSetting(str1));
                        }
                        nameValueCollection[str1] = str2;
                        i++;
                    }
                }
            }
            return(nameValueCollection);
        }