private string PrepareDb(InstallModel model) { var csBuilder = model.ConnectionStringBuilder; SetupWorker.SendFriendlyMessage("Checking connection availability. Connection string: {0}", csBuilder.ConnectionString); var success = CheckDb(csBuilder.ConnectionString); if (success) { return(csBuilder.ConnectionString); } if (!string.IsNullOrEmpty(model.DbAdminUser)) { SetupWorker.SendFriendlyMessage("Trying to connect with administrator user {0}.", model.DbAdminUser); // let's try with admin user csBuilder.UserID = model.DbAdminUser; csBuilder.Password = model.DbAdminPassword; success = CheckDb(csBuilder.ConnectionString); } if (!success) { SetupWorker.SendFriendlyMessage("Trying to connect with integrated user."); // let's try with integrated user csBuilder.IntegratedSecurity = true; success = CheckDb(csBuilder.ConnectionString); } if (success) { AddUserToDatabase(csBuilder.ConnectionString, model.DbUserName, model.DbUserPassword); } else { model.DbAdminUser = "******"; model.DbAdminRequired = true; CustomValidateModel(model); throw new Exception(Resource.DbServerAdminRequiredException); } if (csBuilder.InitialCatalog.ToLower() == "master") { throw new Exception("'Master' is reserved for system database, please provide other database name."); } return(csBuilder.ConnectionString); }
private bool CheckDb(string connectionString) { bool result = false; try { var db = new DbContext(connectionString); db.Database.CreateIfNotExists(); result = true; } catch (Exception err) { SetupWorker.TraceMessageLine(err.Message); } return(result); }
public JsonResult Index(InstallModel model) { CustomValidateModel(model); if (!ModelState.IsValid) { var errorModel = from x in ModelState.Keys where ModelState[x].Errors.Count > 0 select new { key = x, errors = ModelState[x].Errors. Select(y => y.ErrorMessage). ToArray() }; Response.StatusCode = (int)HttpStatusCode.BadRequest; return(Json(errorModel)); } var traceListener = new SignalRTraceListener(); Trace.Listeners.Add(traceListener); try { var connectionString = PrepareDb(model); SetupWorker.SendFriendlyMessage("Saving database connection string to web.config."); //After saving connection string signalR needs some time to recycle iis ConnectionHelper.SqlConnectionString = connectionString; } catch (Exception ex) { SetupWorker.TraceMessageLine(ex.Message); return(Json(new { Success = false, ex.Message })); } finally { Trace.Listeners.Remove(traceListener); } return(Json(new { Success = true })); }
private void AddUserToDatabase(string connectionString, string userId, string password, string dbRole = "db_owner") { using (var dbConn = new SqlConnection(connectionString)) { SetupWorker.SendFriendlyMessage("Creating user and adding it to database."); dbConn.Open(); var databaseName = dbConn.Database; dbConn.ChangeDatabase("master"); try { ExecuteSQL(dbConn, "CREATE LOGIN [{0}] WITH PASSWORD = '******'", userId, password); } catch (Exception err) { SetupWorker.TraceMessageLine(err.Message); } dbConn.ChangeDatabase(databaseName); ExecuteSQL(dbConn, "CREATE USER [{0}] FOR LOGIN {0}", userId); ExecuteSQL(dbConn, "EXEC sp_addrolemember '{1}', '{0}'", userId, dbRole); dbConn.Close(); } }