/// <summary> /// Creates a new login on the server. /// </summary> /// <param name="loginProperties">Values for login initialization</param> /// <param name="server">The server to create the login on</param> public Login(LoginProperties loginProperties, Server server) { _server = server; _login = _server.ConstructLogin(loginProperties.Name); _login.LoginType = loginProperties.LoginType; _login.Sid = loginProperties.Sid; _login.DefaultDatabase = loginProperties.DefaultDatabase; if (loginProperties.LoginType == Smo.LoginType.SqlLogin) { if (loginProperties.PasswordHash != null) { _login.Create(loginProperties.PasswordHash, Smo.LoginCreateOptions.IsHashed); } else if (loginProperties.Password != null) { _login.Create(loginProperties.Password); } else { throw new ArgumentException("Password or hash was not supplied for sql login."); } } else { _login.Create(); } }
private static Microsoft.SqlServer.Management.Smo.Login ConstructLogin(LoginProperties loginProperties, Server server) { var login = new Microsoft.SqlServer.Management.Smo.Login(server._server, loginProperties.Name) { LoginType = loginProperties.LoginType, Sid = loginProperties.Sid, DefaultDatabase = loginProperties.DefaultDatabase }; if (loginProperties.LoginType == LoginType.SqlLogin) { if (loginProperties.PasswordHash != null) { login.Create(loginProperties.PasswordHash, LoginCreateOptions.IsHashed); } else if (loginProperties.Password != null) { login.Create(loginProperties.Password); } else { throw new ArgumentException("Password or hash was not supplied for sql login."); } } else { login.Create(); } return(login); }
//public static async Task<bool> CreateSQLMonitorLogin(string instanceName, out string message) //{ // bool result = false; // try // { // MSMO.Server server = SMO.Server.GetFromSMO(instanceName); // MSMO.Login newLogin = new MSMO.Login(server, Data.Config.SQLInformationAgent_NTLoginName); // newLogin.LoginType = MSMO.LoginType.WindowsUser; // //newLogin.DefaultDatabase = "master"; // newLogin.Create(); // newLogin.AddToRole(Data.Config.SQLInformationAgent_ServerRole); // message = "Success"; // result = true; // //server.Logins.Add(newLogin); // } // catch (Microsoft.SqlServer.Management.Common.ConnectionFailureException ex) // { // VNC.AppLog.Warning(ex, LOG_APPNAME, CLASS_BASE_ERRORNUMBER + 140); // message = "Connection Failure"; // } // catch (Exception ex) // { // VNC.AppLog.Error(ex, LOG_APPNAME, CLASS_BASE_ERRORNUMBER + 141); // message = ex.Message; // } // return result; //} public static bool CreateSQLMonitorLogin(string instanceName, out string message) { bool result = false; try { MSMO.Server server = SMO.Server.GetFromSMO(instanceName); MSMO.Login newLogin = new MSMO.Login(server, Data.Config.SQLInformationAgent_NTLoginName); newLogin.LoginType = MSMO.LoginType.WindowsUser; //newLogin.DefaultDatabase = "master"; newLogin.Create(); newLogin.AddToRole(Data.Config.SQLInformationAgent_ServerRole); message = "Success"; result = true; //server.Logins.Add(newLogin); } catch (Microsoft.SqlServer.Management.Common.ConnectionFailureException ex) { VNC.AppLog.Warning(ex, LOG_APPNAME, CLASS_BASE_ERRORNUMBER + 140); message = "Connection Failure"; } catch (Exception ex) { VNC.AppLog.Error(ex, LOG_APPNAME, CLASS_BASE_ERRORNUMBER + 141); message = ex.Message; } return(result); }