Esempio n. 1
0
        public virtual IPrincipalConnection Parse(string connectionString)
        {
            if (connectionString == null)
            {
                throw new ArgumentNullException("connectionString");
            }

            var principalConnection = new PrincipalConnection();

            var dictionary = this.GetConnectionStringAsDictionary(connectionString);

            if (dictionary.Any())
            {
                try
                {
                    foreach (var keyValuePair in dictionary)
                    {
                        this.TrySetValue(principalConnection, keyValuePair);
                    }
                }
                catch (Exception exception)
                {
                    throw new FormatException(string.Format(CultureInfo.InvariantCulture, "The connection-string \"{0}\" could not be parsed.", connectionString), exception);
                }
            }

            return(principalConnection);
        }
Esempio n. 2
0
        protected internal virtual void TrySetValue(PrincipalConnection principalConnection, KeyValuePair <string, string> keyValuePair)
        {
            if (principalConnection == null)
            {
                throw new ArgumentNullException("principalConnection");
            }

            if (string.IsNullOrEmpty(keyValuePair.Key))
            {
                throw new FormatException("A key can not be empty.");
            }

            switch (keyValuePair.Key.ToLowerInvariant())
            {
            case "container":
            {
                principalConnection.Container = keyValuePair.Value;
                break;
            }

            case "contexttype":
            {
                principalConnection.ContextType = (ContextType)Enum.Parse(typeof(ContextType), keyValuePair.Value);
                break;
            }

            case "name":
            {
                principalConnection.Name = keyValuePair.Value;
                break;
            }

            case "options":
            {
                principalConnection.Options = (ContextOptions)Enum.Parse(typeof(ContextOptions), keyValuePair.Value);
                break;
            }

            case "password":
            {
                principalConnection.Password = keyValuePair.Value;
                break;
            }

            case "username":
            {
                principalConnection.UserName = keyValuePair.Value;
                break;
            }

            default:
            {
                throw new FormatException(string.Format(CultureInfo.InvariantCulture, "The key \"{0}\" is not valid.", keyValuePair.Key));
            }
            }
        }