public override void ExecuteCommand()

in Source/NuGetGallery.Operations/Tasks/Database/CreateSqlUserTask.cs [35:85]


        public override void ExecuteCommand()
        {
            // Generate password
            var rng = new RNGCryptoServiceProvider();
            byte[] data = new byte[20];
            rng.GetBytes(data);
            string password = Convert.ToBase64String(data);

            WithMasterConnection((c, db) =>
            {
                if (!WhatIf)
                {
                    db.Execute(String.Format("CREATE LOGIN [{0}] WITH PASSWORD='{1}';", UserName, password));
                }
                Log.Info("Created Login: {0}", UserName);
            });

            WithConnection((c, db) =>
            {
                if (!WhatIf)
                {
                    db.Execute(String.Format("CREATE USER [{0}] FROM LOGIN [{0}];", UserName));
                }
                Log.Info("Created User: {0}", UserName);

                if (!WhatIf)
                {
                    db.Execute(String.Format("EXEC sp_addrolemember 'db_owner', '{0}';", UserName));
                }
                Log.Info("Added User to db_owner role: {0}", UserName);
            });

            // Generate the new connection string
            var newstr = new SqlConnectionStringBuilder(ConnectionString.ConnectionString);
            newstr.UserID = String.Format("{0}@{1}", UserName, Util.GetDatabaseServerName(ConnectionString));
            newstr.Password = password;

            if (Clip)
            {
                var t = new Thread(() => Clipboard.SetText(newstr.ConnectionString));
                t.SetApartmentState(ApartmentState.STA);
                t.Start();
                t.Join();
                Log.Info("Connection String for the new user is in the clipboard");
            }
            else
            {
                Log.Info("Connection String for the new user: ");
                Log.Info(newstr.ConnectionString);
            }
        }