private DbConnector() { string connetionString = null; CredentialsCollection credentials = ReadDbSetting(); byte[] pw; var password = string.Empty; foreach (DbCredential cred in credentials.DbCredential) { if (cred.ConnectionName.Equals("TenantResolver")) { password = Cryptor.Decrypt(cred.Password); connetionString = string.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3};", cred.DataSource, cred.Catalog, cred.UserName, password); try { DbConnectionMetaData = new SqlConnection(connetionString); } catch (Exception ex) { throw ex; } } if (cred.ConnectionName.Equals("AppDB")) { pw = Encoding.ASCII.GetBytes(cred.Password); password = Encoding.ASCII.GetString(pw, 0, pw.Length); connetionString = string.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3};", cred.DataSource, cred.Catalog, cred.UserName, password); try { DbConnectionApp = new SqlConnection(connetionString); } catch (Exception ex) { throw ex; } } } ReadCommands(); // Read DB Commands from Commands.xml file }
/// <summary> /// Read Db Setting /// </summary> /// <param name="fileLocation"></param> /// <returns></returns> private CredentialsCollection ReadDbSetting() { string fileLocation = string.Empty; var dbSettingFilePath = ConfigurationManager.AppSettings.GetValues("DbSetting"); if (dbSettingFilePath != null) { fileLocation = dbSettingFilePath[0].ToString(); } if (string.IsNullOrEmpty(fileLocation)) { return(null); } CredentialsCollection credentials = null; XmlSerializer serializer = new XmlSerializer(typeof(CredentialsCollection)); string path = Path.Combine(Environment.CurrentDirectory, fileLocation); using (StreamReader reader = new StreamReader(path)) { credentials = (CredentialsCollection)serializer.Deserialize(reader); } // If the user credential changed please update the new password in ClearPassword element in the DbSetting file. // So below logic will encript the new password and allow user to login to DB as it does before. if (credentials != null && credentials.DbCredential.Any(record => record.ClearPassword != null && record.ClearPassword.Length > 0)) { using (TextWriter writer = new StreamWriter(path)) { foreach (DbCredential crdntl in credentials.DbCredential) { crdntl.DateGenarated = DateTime.Now; crdntl.Password = Cryptor.Encrypt(crdntl.ClearPassword); crdntl.ClearPassword = string.Empty; } serializer.Serialize(writer, credentials); } } return(credentials); }