Esempio n. 1
0
        protected override bool Unbind(ServiceCredentials credentials)
        {
            if (credentials == null)
            {
                return(false);
            }

            Logger.Debug(Strings.SqlNodeUnbindServiceDebugMessage, credentials.SerializeToJson());

            string user         = credentials.User;
            string databaseName = credentials.Name;

            using (SqlConnection databaseConnection = new SqlConnection(this.ConnectionString))
            {
                databaseConnection.Open();
                databaseConnection.ChangeDatabase(databaseName);

                using (SqlCommand cmdUserExists = new SqlCommand(string.Format(CultureInfo.InvariantCulture, "select count(*) from sys.sysusers where name=N'{0}'", user), databaseConnection))
                {
                    int userCount = (int)cmdUserExists.ExecuteScalar();
                    if (userCount != 1)
                    {
                        throw new MSSqlErrorException(MSSqlErrorException.MSSqlCredentialsNotFound, user);
                    }
                }
            }

            this.DeleteDatabaseUser(user);
            return(true);
        }
Esempio n. 2
0
        /// <summary>
        /// Binds a SQL Server database to an app.
        /// </summary>
        /// <param name="name">The name of the service.</param>
        /// <param name="bindOptions">Binding options.</param>
        /// <param name="credentials">Already existing credentials.</param>
        /// <returns>
        /// A new set of credentials used for binding.
        /// </returns>
        protected override ServiceCredentials Bind(string name, Dictionary <string, object> bindOptions, ServiceCredentials credentials)
        {
            Logger.Debug(Strings.SqlNodeBindServiceDebugMessage, name, JsonConvertibleObject.SerializeToJson(bindOptions));
            Dictionary <string, object> binding = null;

            try
            {
                ProvisionedService service = ProvisionedService.GetService(name);
                if (service == null)
                {
                    throw new MSSqlErrorException(MSSqlErrorException.MSSqlConfigNotFound, name);
                }

                // create new credential for binding
                binding = new Dictionary <string, object>();

                if (credentials != null)
                {
                    binding["user"]     = credentials.User;
                    binding["password"] = credentials.Password;
                }
                else
                {
                    binding["user"]     = "******" + Credentials.GenerateCredential();
                    binding["password"] = "******" + Credentials.GenerateCredential();
                }

                binding["bind_opts"] = bindOptions;

                this.CreateDatabaseUser(name, binding["user"] as string, binding["password"] as string);
                ServiceCredentials response = this.GenerateCredential(name, binding["user"] as string, binding["password"] as string);

                Logger.Debug(Strings.SqlNodeBindResponseDebugMessage, response.SerializeToJson());
                this.bindingServed += 1;
                return(response);
            }
            catch (Exception)
            {
                if (binding != null)
                {
                    this.DeleteDatabaseUser(binding["user"] as string);
                }

                throw;
            }
        }