///<summary>Returns true if the connection settings were successfully saved to the FreeDentalConfig file. Otherwise, false.</summary> public static bool TrySaveConnectionSettings(CentralConnection centralConnection, DatabaseType dbType, string connectionString = "" , bool noShowOnStartup = false, List <string> listAdminCompNames = null) { try { XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.IndentChars = (" "); using (XmlWriter writer = XmlWriter.Create(ODFileUtils.CombinePaths(Application.StartupPath, "FreeDentalConfig.xml"), settings)) { writer.WriteStartElement("ConnectionSettings"); if (connectionString != "") { writer.WriteStartElement("ConnectionString"); writer.WriteString(connectionString); writer.WriteEndElement(); } else if (RemotingClient.RemotingRole == RemotingRole.ClientDirect) { writer.WriteStartElement("DatabaseConnection"); writer.WriteStartElement("ComputerName"); writer.WriteString(centralConnection.ServerName); writer.WriteEndElement(); writer.WriteStartElement("Database"); writer.WriteString(centralConnection.DatabaseName); writer.WriteEndElement(); writer.WriteStartElement("User"); writer.WriteString(centralConnection.MySqlUser); writer.WriteEndElement(); string encryptedPwd; CDT.Class1.Encrypt(centralConnection.MySqlPassword, out encryptedPwd); //sets encryptedPwd ot value or null writer.WriteStartElement("Password"); //If encryption fails, write plain text password to xml file; maintains old behavior. writer.WriteString(string.IsNullOrEmpty(encryptedPwd) ? centralConnection.MySqlPassword : ""); writer.WriteEndElement(); writer.WriteStartElement("MySQLPassHash"); writer.WriteString(encryptedPwd ?? ""); writer.WriteEndElement(); writer.WriteStartElement("NoShowOnStartup"); if (noShowOnStartup) { writer.WriteString("True"); } else { writer.WriteString("False"); } writer.WriteEndElement(); writer.WriteEndElement(); } else if (RemotingClient.RemotingRole == RemotingRole.ClientWeb) { writer.WriteStartElement("ServerConnection"); writer.WriteStartElement("URI"); writer.WriteString(centralConnection.ServiceURI); writer.WriteEndElement(); //end URI if (centralConnection.IsAutomaticLogin) { writer.WriteStartElement("User"); writer.WriteString(centralConnection.OdUser); writer.WriteEndElement(); //end Username writer.WriteStartElement("UsingAutoLogin"); writer.WriteString("True"); writer.WriteEndElement(); //end UsingAutoLogin } writer.WriteStartElement("UsingEcw"); if (centralConnection.WebServiceIsEcw) { writer.WriteString("True"); } else { writer.WriteString("False"); } writer.WriteEndElement(); //end UsingEcw writer.WriteEndElement(); //end ServerConnection } writer.WriteStartElement("DatabaseType"); if (dbType == DatabaseType.MySql) { writer.WriteString("MySql"); } else { writer.WriteString("Oracle"); } writer.WriteEndElement(); if (OpenDentBusiness.WebTypes.Shared.XWeb.XWebs.UseXWebTestGateway) { writer.WriteStartElement("UseXWebTestGateway"); writer.WriteString("True"); writer.WriteEndElement(); } if (listAdminCompNames != null && listAdminCompNames.Count > 0) { writer.WriteStartElement("AdminCompNames"); foreach (string compName in listAdminCompNames) { writer.WriteStartElement("CompName"); writer.WriteString(compName); writer.WriteEndElement(); } writer.WriteEndElement(); } writer.WriteEndElement(); writer.Flush(); } //using writer if (centralConnection.IsAutomaticLogin) //Input the user's credentials to WCM (Windows Credential Manager) { PasswordVaultWrapper.WritePassword(centralConnection.ServiceURI, centralConnection.OdUser, centralConnection.OdPassword); } } catch (Exception) { return(false); } return(true); }
///<summary>Returns true if the connection settings were successfully saved to the FreeDentalConfig file. Otherwise, false. ///Set isCommandLineArgs to true in order to preserve settings within FreeDentalConfig.xml that are not command line args. ///E.g. the current value within the FreeDentalConfig.xml for NoShowOnStartup will be preserved instead of the value passed in.</summary> public static bool TrySaveConnectionSettings(CentralConnection centralConnection, DatabaseType dbType, string connectionString = "" , bool noShowOnStartup = false, List <string> listAdminCompNames = null, bool isCommandLineArgs = false) { try { //The parameters passed in might have misleading information (like noShowOnStartup) if they were comprised from command line arguments. //Non-command line settings within the FreeDentalConfig.xml need to be preserved when command line arguments are used. if (isCommandLineArgs) { //Override all variables that are NOT valid command line arguments with their current values within the FreeDentalConfig.xml //This will preserve the values that should stay the same (e.g. noShowOnStartup is NOT a command line arg and will always be true). CentralConnection centralConnectionFile; string connectionStringFile; YN noShowFile; DatabaseType dbTypeFile; List <string> listAdminCompNamesFile; GetChooseDatabaseConnectionSettings(out centralConnectionFile, out connectionStringFile, out noShowFile, out dbTypeFile , out listAdminCompNamesFile); //Since command line args are being used, override any variables that are NOT allowed to be passed in via the command line. #region FreeDentalConfig Nodes That Do Not Have a Corresponding Command Line Arg switch (noShowFile) //config node: <NoShowOnStartup> { case YN.Yes: noShowOnStartup = true; break; case YN.No: case YN.Unknown: //Either NoShowOnStartup was not found or was explicitly set to no. noShowOnStartup = false; break; } listAdminCompNames = listAdminCompNamesFile; //config node: <AdminCompNames> connectionString = connectionStringFile; //config node: <ConnectionString> centralConnection.IsAutomaticLogin = centralConnectionFile.IsAutomaticLogin; //config node: <UsingAutoLogin> #endregion } XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.IndentChars = (" "); using (XmlWriter writer = XmlWriter.Create(ODFileUtils.CombinePaths(Application.StartupPath, "FreeDentalConfig.xml"), settings)) { writer.WriteStartElement("ConnectionSettings"); if (connectionString != "") { writer.WriteStartElement("ConnectionString"); writer.WriteString(connectionString); writer.WriteEndElement(); } else if (RemotingClient.RemotingRole == RemotingRole.ClientDirect) { writer.WriteStartElement("DatabaseConnection"); writer.WriteStartElement("ComputerName"); writer.WriteString(centralConnection.ServerName); writer.WriteEndElement(); writer.WriteStartElement("Database"); writer.WriteString(centralConnection.DatabaseName); writer.WriteEndElement(); writer.WriteStartElement("User"); writer.WriteString(centralConnection.MySqlUser); writer.WriteEndElement(); string encryptedPwd; CDT.Class1.Encrypt(centralConnection.MySqlPassword, out encryptedPwd); //sets encryptedPwd ot value or null writer.WriteStartElement("Password"); //If encryption fails, write plain text password to xml file; maintains old behavior. writer.WriteString(string.IsNullOrEmpty(encryptedPwd) ? centralConnection.MySqlPassword : ""); writer.WriteEndElement(); writer.WriteStartElement("MySQLPassHash"); writer.WriteString(encryptedPwd ?? ""); writer.WriteEndElement(); writer.WriteStartElement("NoShowOnStartup"); if (noShowOnStartup) { writer.WriteString("True"); } else { writer.WriteString("False"); } writer.WriteEndElement(); writer.WriteEndElement(); } else if (RemotingClient.RemotingRole == RemotingRole.ClientWeb) { writer.WriteStartElement("ServerConnection"); writer.WriteStartElement("URI"); writer.WriteString(centralConnection.ServiceURI); writer.WriteEndElement(); //end URI if (centralConnection.IsAutomaticLogin) { writer.WriteStartElement("User"); writer.WriteString(centralConnection.OdUser); writer.WriteEndElement(); //end Username writer.WriteStartElement("UsingAutoLogin"); writer.WriteString("True"); writer.WriteEndElement(); //end UsingAutoLogin } writer.WriteStartElement("UsingEcw"); if (centralConnection.WebServiceIsEcw) { writer.WriteString("True"); } else { writer.WriteString("False"); } writer.WriteEndElement(); //end UsingEcw writer.WriteEndElement(); //end ServerConnection } writer.WriteStartElement("DatabaseType"); if (dbType == DatabaseType.MySql) { writer.WriteString("MySql"); } else { writer.WriteString("Oracle"); } writer.WriteEndElement(); if (OpenDentBusiness.WebTypes.Shared.XWeb.XWebs.UseXWebTestGateway) { writer.WriteStartElement("UseXWebTestGateway"); writer.WriteString("True"); writer.WriteEndElement(); } if (listAdminCompNames != null && listAdminCompNames.Count > 0) { writer.WriteStartElement("AdminCompNames"); foreach (string compName in listAdminCompNames) { writer.WriteStartElement("CompName"); writer.WriteString(compName); writer.WriteEndElement(); } writer.WriteEndElement(); } writer.WriteEndElement(); writer.Flush(); } //using writer if (centralConnection.IsAutomaticLogin) //Input the user's credentials to WCM (Windows Credential Manager) { PasswordVaultWrapper.WritePassword(centralConnection.ServiceURI, centralConnection.OdUser, centralConnection.OdPassword); } } catch (Exception) { return(false); } return(true); }
///<summary>Returns true if the connection settings were successfully saved to the FreeDentalConfig file and/or Windows PasswordVault. ///Otherwise, false. ///Set isCommandLineArgs to true in order to preserve settings within FreeDentalConfig.xml that are not command line args. ///E.g. the current value within the FreeDentalConfig.xml for NoShowOnStartup will be preserved instead of the value passed in.</summary> public static bool TrySaveConnectionSettings(CentralConnection centralConnection, DatabaseType dbType, string connectionString = "" , bool noShowOnStartup = false, List <string> listAdminCompNames = null, bool isCommandLineArgs = false, bool useDynamicMode = false, bool allowAutoLogin = true) { try { //The parameters passed in might have misleading information (like noShowOnStartup) if they were comprised from command line arguments. //Non-command line settings within the FreeDentalConfig.xml need to be preserved when command line arguments are used. if (isCommandLineArgs) { //Updating the freedentalconfig.xml file when connecting via command line arguments causes issues for users //who prefer to have a desktop icon pointing to their main database and additional icons for other databases (popular amongst CEMT users). return(false); /**** The following code will be reintroduced in the near future. Leaving as a comment on purpose. **** * //Override all variables that are NOT valid command line arguments with their current values within the FreeDentalConfig.xml * //This will preserve the values that should stay the same (e.g. noShowOnStartup is NOT a command line arg and will always be true). * CentralConnection centralConnectionFile; * string connectionStringFile; * YN noShowFile; * DatabaseType dbTypeFile; * List<string> listAdminCompNamesFile; * GetChooseDatabaseConnectionSettings(out centralConnectionFile,out connectionStringFile,out noShowFile,out dbTypeFile * ,out listAdminCompNamesFile,out useDynamicMode); * //Since command line args are being used, override any variables that are NOT allowed to be passed in via the command line. #region FreeDentalConfig Nodes That Do Not Have a Corresponding Command Line Arg * switch(noShowFile) {//config node: <NoShowOnStartup> * case YN.Yes: * noShowOnStartup=true; * break; * case YN.No: * case YN.Unknown: * //Either NoShowOnStartup was not found or was explicitly set to no. * noShowOnStartup=false; * break; * } * listAdminCompNames=listAdminCompNamesFile;//config node: <AdminCompNames> * connectionString=connectionStringFile;//config node: <ConnectionString> * centralConnection.IsAutomaticLogin=centralConnectionFile.IsAutomaticLogin;//config node: <UsingAutoLogin> #endregion ********************************************************************************************************/ } XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.IndentChars = (" "); using (XmlWriter writer = XmlWriter.Create(ODFileUtils.CombinePaths(Application.StartupPath, "FreeDentalConfig.xml"), settings)) { writer.WriteStartElement("ConnectionSettings"); if (!allowAutoLogin) { //Only add if it was added before. writer.WriteElementString("AllowAutoLogin", "False"); } if (connectionString != "") { writer.WriteStartElement("ConnectionString"); writer.WriteString(connectionString); writer.WriteEndElement(); } else if (RemotingClient.RemotingRole == RemotingRole.ClientDirect) { writer.WriteStartElement("DatabaseConnection"); writer.WriteStartElement("ComputerName"); writer.WriteString(centralConnection.ServerName); writer.WriteEndElement(); writer.WriteStartElement("Database"); writer.WriteString(centralConnection.DatabaseName); writer.WriteEndElement(); writer.WriteStartElement("User"); writer.WriteString(centralConnection.MySqlUser); writer.WriteEndElement(); string encryptedPwd; CDT.Class1.Encrypt(centralConnection.MySqlPassword, out encryptedPwd); //sets encryptedPwd ot value or null writer.WriteStartElement("Password"); //If encryption fails, write plain text password to xml file; maintains old behavior. writer.WriteString(string.IsNullOrEmpty(encryptedPwd) ? centralConnection.MySqlPassword : ""); writer.WriteEndElement(); writer.WriteStartElement("MySQLPassHash"); writer.WriteString(encryptedPwd ?? ""); writer.WriteEndElement(); writer.WriteStartElement("NoShowOnStartup"); if (noShowOnStartup) { writer.WriteString("True"); } else { writer.WriteString("False"); } writer.WriteEndElement(); writer.WriteEndElement(); } else if (RemotingClient.RemotingRole == RemotingRole.ClientWeb) { writer.WriteStartElement("ServerConnection"); writer.WriteStartElement("URI"); writer.WriteString(centralConnection.ServiceURI); writer.WriteEndElement(); //end URI if (centralConnection.IsAutomaticLogin) { writer.WriteStartElement("User"); writer.WriteString(centralConnection.OdUser); writer.WriteEndElement(); //end Username writer.WriteStartElement("UsingAutoLogin"); writer.WriteString("True"); writer.WriteEndElement(); //end UsingAutoLogin } writer.WriteStartElement("UsingEcw"); if (centralConnection.WebServiceIsEcw) { writer.WriteString("True"); } else { writer.WriteString("False"); } writer.WriteEndElement(); //end UsingEcw writer.WriteEndElement(); //end ServerConnection } writer.WriteStartElement("DatabaseType"); if (dbType == DatabaseType.MySql) { writer.WriteString("MySql"); } else { writer.WriteString("Oracle"); } writer.WriteEndElement(); writer.WriteStartElement("UseDynamicMode"); writer.WriteString(useDynamicMode.ToString()); writer.WriteEndElement(); if (OpenDentBusiness.WebTypes.Shared.XWeb.XWebs.UseXWebTestGateway) { writer.WriteStartElement("UseXWebTestGateway"); writer.WriteString("True"); writer.WriteEndElement(); } if (listAdminCompNames != null && listAdminCompNames.Count > 0) { writer.WriteStartElement("AdminCompNames"); foreach (string compName in listAdminCompNames) { writer.WriteStartElement("CompName"); writer.WriteString(compName); writer.WriteEndElement(); } writer.WriteEndElement(); } writer.WriteEndElement(); writer.Flush(); } //using writer //Input the user's credentials to WCM (Windows Credential Manager) if necessary. if (RemotingClient.RemotingRole == RemotingRole.ClientWeb && !string.IsNullOrWhiteSpace(centralConnection.ServiceURI) && centralConnection.IsAutomaticLogin) { bool isCredentialSaveRequired = true; //Assume credentials are not saved yet. if (PasswordVaultWrapper.TryRetrieveUserName(centralConnection.ServiceURI, out string userName)) //Found a saved OD MT UserName //Update the credentials if the user or password is different. { if (centralConnection.OdUser != userName || PasswordVaultWrapper.RetrievePassword(centralConnection.ServiceURI, userName) != centralConnection.OdPassword) { //UserName or Password in the saved credentials does not match current password. Delete the saved credentials and save the new //credentials. This will clear all the saved credentials from the PasswordVault for the currently logged in Windows User, which is //desired behavior as each Windows User should only have one set of OpenDental Middle Tier credentials. PasswordVaultWrapper.ClearCredentials(centralConnection.ServiceURI); } else { isCredentialSaveRequired = false; //Username and Password already saved and match current credentials. } } if (isCredentialSaveRequired) { if (!string.IsNullOrWhiteSpace(centralConnection.OdUser) && !string.IsNullOrWhiteSpace(centralConnection.OdPassword)) { //Save the new credentials. PasswordVaultWrapper.WritePassword(centralConnection.ServiceURI, centralConnection.OdUser, centralConnection.OdPassword); } else { return(false); //Invalid username/password. } } } } catch (Exception ex) { ex.DoNothing(); return(false); } return(true); }