/// <summary> /// Reads data into configuration from an XML string into a passed /// instance of the a configuration object. /// </summary> /// <param name="config">An instance of a custom configuration object</param> /// <param name="xml">Xml of serialized configuration instance.</param> /// <returns>true or false</returns> public virtual bool Read(AppConfiguration config, string xml) { TAppConfiguration newConfig; // if no data was passed leave the object // in its initial state. if (string.IsNullOrEmpty(xml)) { return(true); } try { newConfig = SerializationUtils.DeSerializeObject(xml, config.GetType()) as TAppConfiguration; if (newConfig == null) { SetError(Resources.ObjectCouldNotBeDeserializedFromXml); return(false); } } catch (Exception ex) { SetError(ex); return(false); } DecryptFields(newConfig); DataUtils.CopyObjectData(newConfig, config, "Provider,ErrorMessage"); return(true); }
/// <summary> /// Encrypts all the fields in the current object based on the EncryptFieldList /// </summary> /// <returns></returns> public virtual void EncryptFields(AppConfiguration config) { if (string.IsNullOrEmpty(PropertiesToEncrypt)) { return; } MemberInfo[] mi = config.GetType().FindMembers(MemberTypes.Property | MemberTypes.Field, ReflectionUtils.MemberAccess, null, null); string encryptFieldList = "," + PropertiesToEncrypt.ToLower() + ","; foreach (MemberInfo Member in mi) { string FieldName = Member.Name.ToLower(); // Encrypt the field if in list if (encryptFieldList.Contains("," + FieldName + ",")) { object val = string.Empty; if (Member.MemberType == MemberTypes.Field) { val = ((FieldInfo)Member).GetValue(config); } else { val = ((PropertyInfo)Member).GetValue(config, null); } if (val == null || !(val is string)) { continue; } var strVal = val as string; if (string.IsNullOrEmpty(strVal)) { continue; } val = Encryption.EncryptString(strVal, EncryptionKey); if (Member.MemberType == MemberTypes.Field) { ((FieldInfo)Member).SetValue(config, val); } else { ((PropertyInfo)Member).SetValue(config, val, null); } } } }
/// <summary> /// Internally decryptes all the fields in the current object based on the EncryptFieldList /// </summary> /// <returns></returns> public virtual void DecryptFields(AppConfiguration config) { if (string.IsNullOrEmpty(PropertiesToEncrypt)) { return; } MemberInfo[] mi = config.GetType().FindMembers(MemberTypes.Property | MemberTypes.Field, ReflectionUtils.MemberAccess, null, null); string encryptFieldList = "," + PropertiesToEncrypt.ToLower() + ","; foreach (MemberInfo Member in mi) { string FieldName = Member.Name.ToLower(); // Encrypt the field if in list if (encryptFieldList.IndexOf("," + FieldName + ",") > -1) { object Value = string.Empty; if (Member.MemberType == MemberTypes.Field) { Value = ((FieldInfo)Member).GetValue(config); } else { Value = ((PropertyInfo)Member).GetValue(config, null); } Value = Encryption.DecryptString((string)Value, EncryptionKey); if (Member.MemberType == MemberTypes.Field) { ((FieldInfo)Member).SetValue(config, Value); } else { ((PropertyInfo)Member).SetValue(config, Value, null); } } } }
public override bool Write(AppConfiguration config) { EncryptFields(config); lock (syncWriteLock) { // Load the config file into DOM parser XmlDocument dom = new XmlDocument(); string configFile = ConfigurationFile; if (string.IsNullOrEmpty(configFile)) { configFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; } try { dom.Load(configFile); } catch { // Can't load the file - create an empty document dom.LoadXml(@"<?xml version='1.0'?> <configuration> </configuration>" ); } // Load up the Namespaces object so we can // reference the appropriate default namespace GetXmlNamespaceInfo(dom); // Parse through each of hte properties of the properties Type typeWebConfig = config.GetType(); MemberInfo[] fields = typeWebConfig.GetMembers(BindingFlags.Instance | BindingFlags.GetField | BindingFlags.GetProperty | BindingFlags.Public); string configSection = "appSettings"; if (!string.IsNullOrEmpty(ConfigurationSection)) { configSection = ConfigurationSection; } // make sure we're getting the latest values before we write ConfigurationManager.RefreshSection(configSection); foreach (MemberInfo field in fields) { // Don't persist ErrorMessage property if (field.Name == "ErrorMessage" || field.Name == "Provider") { continue; } object rawValue; switch (field.MemberType) { case MemberTypes.Field: rawValue = ((FieldInfo)field).GetValue(config); break; case MemberTypes.Property: rawValue = ((PropertyInfo)field).GetValue(config, null); break; default: continue; } string value = TypedValueToString(rawValue, CultureInfo.InvariantCulture); if (value == "ILIST_TYPE") { var count = 0; foreach (var item in rawValue as IList) { value = TypedValueToString(item, CultureInfo.InvariantCulture); WriteConfigurationValue(field.Name + ++count, value, field, dom, configSection); } } else { WriteConfigurationValue(field.Name, value, field, dom, configSection); } } try { // this will fail if permissions are not there dom.Save(configFile); ConfigurationManager.RefreshSection(configSection); } catch { return(false); } finally { DecryptFields(config); } } return(true); }
/// <summary> /// Reads configuration settings from the current configuration manager. /// Uses the internal APIs to write these values. /// </summary> /// <typeparam name="TAppConfiguration"></typeparam> /// <param name="config"></param> /// <returns></returns> public override bool Read(AppConfiguration config) { // Config reading from external files works a bit differently // so use a separate method to handle it if (!string.IsNullOrEmpty(ConfigurationFile)) { return(Read(config, ConfigurationFile)); } Type typeWebConfig = config.GetType(); MemberInfo[] fields = typeWebConfig.GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.GetField); // Set a flag for missing fields // If we have any we'll need to write them out into .config bool missingFields = false; // Refresh the sections - re-read after write operations // sometimes sections don't want to re-read ConfigurationManager.RefreshSection(string.IsNullOrEmpty(ConfigurationSection) ? "appSettings" : ConfigurationSection); NameValueCollection configManager = string.IsNullOrEmpty(ConfigurationSection) ? ConfigurationManager.AppSettings : ConfigurationManager.GetSection(ConfigurationSection) as NameValueCollection; if (configManager == null) { Write(config); return(true); } // Loop through all fields and properties foreach (MemberInfo member in fields) { Type fieldType = null; if (member.MemberType == MemberTypes.Field) { var field = (FieldInfo)member; fieldType = field.FieldType; } else if (member.MemberType == MemberTypes.Property) { var property = (PropertyInfo)member; fieldType = property.PropertyType; } else { continue; } string fieldName = member.Name.ToLower(); // Error Message is an internal public property if (fieldName == "errormessage" || fieldName == "provider") { continue; } if (!IsIList(fieldType)) { // Single value string value = configManager[fieldName]; if (value == null) { missingFields = true; continue; } try { // Assign the value to the property ReflectionUtils.SetPropertyEx(config, member.Name, StringToTypedValue(value, fieldType, CultureInfo.InvariantCulture)); } catch { } } else { // List Value var list = Activator.CreateInstance(fieldType) as IList; var elementType = fieldType.GetElementType(); if (elementType == null) { var generic = fieldType.GetGenericArguments(); if (generic != null && generic.Length > 0) { elementType = generic[0]; } } int count = 1; string value = string.Empty; while (value != null) { value = configManager[fieldName + count]; if (value == null) { break; } list.Add(StringToTypedValue(value, elementType, CultureInfo.InvariantCulture)); count++; } try { ReflectionUtils.SetPropertyEx(config, member.Name, list); } catch { } } } DecryptFields(config); // We have to write any missing keys if (missingFields) { Write(config); } return(true); }
/// <summary> /// Reads Configuration settings from an external file or explicitly from a file. /// Uses XML DOM to read values instead of using the native APIs. /// </summary> /// <typeparam name="TAppConfiguration"></typeparam> /// <param name="config">Configuration instance</param> /// <param name="filename">Filename to read from</param> /// <returns></returns> public override bool Read(AppConfiguration config, string filename) { Type typeWebConfig = config.GetType(); MemberInfo[] fields = typeWebConfig.GetMembers(BindingFlags.Public | BindingFlags.Instance); // Set a flag for missing fields // If we have any we'll need to write them out bool missingFields = false; XmlDocument dom = new XmlDocument(); try { dom.Load(filename); } catch { // Can't open or doesn't exist - so try to create it if (!Write(config)) { return(false); } // Now load again dom.Load(filename); } // Retrieve XML Namespace information to assign default // Namespace explicitly. GetXmlNamespaceInfo(dom); string configSection = ConfigurationSection; if (configSection == string.Empty) { configSection = "appSettings"; } foreach (MemberInfo member in fields) { Type fieldType = null; if (member.MemberType == MemberTypes.Field) { var field = (FieldInfo)member; fieldType = field.FieldType; } else if (member.MemberType == MemberTypes.Property) { var property = (PropertyInfo)member; fieldType = property.PropertyType; } else { continue; } string fieldname = member.Name; if (fieldname == "Provider" || fieldname == "ErrorMessage") { continue; } XmlNode sectionNode = dom.DocumentElement.SelectSingleNode(XmlNamespacePrefix + configSection, XmlNamespaces); if (sectionNode == null) { sectionNode = CreateConfigSection(dom, ConfigurationSection); dom.DocumentElement.AppendChild(sectionNode); } string value = GetNamedValueFromXml(dom, fieldname, configSection); if (value == null) { missingFields = true; continue; } // Assign the Property ReflectionUtils.SetPropertyEx(config, fieldname, StringToTypedValue(value, fieldType, CultureInfo.InvariantCulture)); } DecryptFields(config); // We have to write any missing keys if (missingFields) { Write(config); } return(true); }
/// <summary> /// Reads Configuration settings from an external file or explicitly from a file. /// Uses XML DOM to read values instead of using the native APIs. /// </summary> /// <typeparam name="TAppConfiguration"></typeparam> /// <param name="config">Configuration instance</param> /// <param name="filename">Filename to read from</param> /// <returns></returns> public override bool Read(AppConfiguration config, string filename) { Type typeWebConfig = config.GetType(); MemberInfo[] Fields = typeWebConfig.GetMembers(BindingFlags.Public | BindingFlags.Instance); // Set a flag for missing fields // If we have any we'll need to write them out bool missingFields = false; XmlDocument Dom = new XmlDocument(); try { Dom.Load(filename); } catch { // Can't open or doesn't exist - so try to create it if (!Write(config)) { return(false); } // Now load again Dom.Load(filename); } // Retrieve XML Namespace information to assign default // Namespace explicitly. GetXmlNamespaceInfo(Dom); string ConfigSection = ConfigurationSection; if (ConfigSection == string.Empty) { ConfigSection = "appSettings"; } string fieldsToEncrypt = "," + PropertiesToEncrypt.ToLower() + ","; foreach (MemberInfo Member in Fields) { FieldInfo Field = null; PropertyInfo Property = null; Type FieldType = null; string TypeName = null; if (Member.MemberType == MemberTypes.Field) { Field = (FieldInfo)Member; FieldType = Field.FieldType; TypeName = Field.FieldType.Name.ToLower(); } else if (Member.MemberType == MemberTypes.Property) { Property = (PropertyInfo)Member; FieldType = Property.PropertyType; TypeName = Property.PropertyType.Name.ToLower(); } else { continue; } string Fieldname = Member.Name; if (Fieldname == "Provider" || Fieldname == "ErrorMessage") { continue; } XmlNode Section = Dom.DocumentElement.SelectSingleNode(XmlNamespacePrefix + ConfigSection, XmlNamespaces); if (Section == null) { Section = CreateConfigSection(Dom, ConfigurationSection); Dom.DocumentElement.AppendChild(Section); } string Value = GetNamedValueFromXml(Dom, Fieldname, ConfigSection); if (Value == null) { missingFields = true; continue; } Fieldname = Fieldname.ToLower(); // If we're encrypting decrypt any field that are encyrpted if (Value != string.Empty && fieldsToEncrypt.IndexOf("," + Fieldname + ",") > -1) { Value = Encryption.DecryptString(Value, EncryptionKey); } // Assign the Property ReflectionUtils.SetPropertyEx(config, Fieldname, StringToTypedValue(Value, FieldType, CultureInfo.InvariantCulture)); } // We have to write any missing keys if (missingFields) { Write(config); } return(true); }
/// <summary> /// Reads Configuration settings from an external file or explicitly from a file. /// Uses XML DOM to read values instead of using the native APIs. /// </summary> /// <typeparam name="TAppConfiguration"></typeparam> /// <param name="config">Configuration instance</param> /// <param name="filename">Filename to read from</param> /// <returns></returns> public override bool Read(AppConfiguration config, string filename) { Type typeWebConfig = config.GetType(); PropertyInfo[] properties = typeWebConfig.GetProperties(BindingFlags.Public | BindingFlags.Instance); // Set a flag for missing fields // If we have any we'll need to write them out bool missingFields = false; XmlDocument Dom = new XmlDocument(); try { Dom.Load(filename); } catch { // Can't open or doesn't exist - so try to create it if (!Write(config)) { return(false); } // Now load again Dom.Load(filename); } // Retrieve XML Namespace information to assign default // Namespace explicitly. GetXmlNamespaceInfo(Dom); string ConfigSection = ConfigurationSection; if (ConfigSection == string.Empty) { ConfigSection = "appSettings"; } foreach (var property in properties) { Type fieldType = null; string typeName = null; fieldType = property.PropertyType; typeName = property.PropertyType.Name.ToLower(); string propertyName = property.Name; if (propertyName == "Provider" || propertyName == "ErrorMessage") { continue; } XmlNode Section = Dom.DocumentElement.SelectSingleNode(XmlNamespacePrefix + ConfigSection, XmlNamespaces); if (Section == null) { Section = CreateConfigSection(Dom, ConfigurationSection); Dom.DocumentElement.AppendChild(Section); } string Value = GetNamedValueFromXml(Dom, propertyName, ConfigSection); if (Value == null) { missingFields = true; continue; } // Assign the Property ReflectionUtils.SetPropertyEx(config, propertyName, StringToTypedValue(Value, fieldType, CultureInfo.InvariantCulture)); } DecryptFields(config); // We have to write any missing keys if (missingFields) { Write(config); } return(true); }
public override bool Write(AppConfiguration config) { EncryptFields(config); lock (syncWriteLock) { // Load the config file into DOM parser XmlDocument Dom = new XmlDocument(); string configFile = ConfigurationFile; if (string.IsNullOrEmpty(configFile)) { configFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; } try { Dom.Load(configFile); } catch { // Can't load the file - create an empty document string Xml = @"<?xml version='1.0'?> <configuration> </configuration>" ; Dom.LoadXml(Xml); } // Load up the Namespaces object so we can // reference the appropriate default namespace GetXmlNamespaceInfo(Dom); // Parse through each of hte properties of the properties Type typeWebConfig = config.GetType(); PropertyInfo[] properties = typeWebConfig.GetProperties(BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.Public); string ConfigSection = "appSettings"; if (!string.IsNullOrEmpty(ConfigurationSection)) { ConfigSection = ConfigurationSection; } // make sure we're getting the latest values before we write ConfigurationManager.RefreshSection(ConfigSection); foreach (var property in properties) { // Don't persist ErrorMessage property if (property.Name == "ErrorMessage" || property.Name == "Provider") { continue; } object rawValue = null; rawValue = property.GetValue(config, null); string value = TypedValueToString(rawValue, CultureInfo.InvariantCulture); if (value == "ILIST_TYPE") { var count = 0; foreach (var item in rawValue as IList) { value = TypedValueToString(item, CultureInfo.InvariantCulture); WriteConfigurationValue(property.Name + ++count, value, property, Dom, ConfigSection); } } else { WriteConfigurationValue(property.Name, value, property, Dom, ConfigSection); } } // for each try { // this will fail if permissions are not there Dom.Save(configFile); ConfigurationManager.RefreshSection(ConfigSection); } catch (Exception ex) { ErrorMessage = "Unable to save configuration file: " + ex.Message; return(false); } finally { DecryptFields(config); } } return(true); }
public override bool Write(AppConfiguration config) { lock (syncWriteLock) { // Load the config file into DOM parser XmlDocument Dom = new XmlDocument(); string configFile = ConfigurationFile; if (string.IsNullOrEmpty(configFile)) { configFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; } try { Dom.Load(configFile); } catch { // Can't load the file - create an empty document string Xml = @"<?xml version='1.0'?> <configuration> </configuration>" ; Dom.LoadXml(Xml); } // Load up the Namespaces object so we can // reference the appropriate default namespace GetXmlNamespaceInfo(Dom); // Parse through each of hte properties of the properties Type typeWebConfig = config.GetType(); MemberInfo[] Fields = typeWebConfig.GetMembers(BindingFlags.Instance | BindingFlags.GetField | BindingFlags.GetProperty | BindingFlags.Public); string fieldsToEncrypt = "," + PropertiesToEncrypt.ToLower() + ","; string ConfigSection = "appSettings"; if (!string.IsNullOrEmpty(ConfigurationSection)) { ConfigSection = ConfigurationSection; } ConfigurationManager.RefreshSection(ConfigSection); foreach (MemberInfo Field in Fields) { // If we can't find the key - write it out to the document string Value = null; object RawValue = null; if (Field.MemberType == MemberTypes.Field) { RawValue = ((FieldInfo)Field).GetValue(config); } else if (Field.MemberType == MemberTypes.Property) { RawValue = ((PropertyInfo)Field).GetValue(config, null); } else { continue; // not a property or field } // Don't persist ErrorMessage property if (Field.Name == "ErrorMessage" || Field.Name == "Provider") { continue; } Value = ReflectionUtils.TypedValueToString(RawValue, CultureInfo.InvariantCulture); // Encrypt the field if in list if (fieldsToEncrypt.IndexOf("," + Field.Name.ToLower() + ",") > -1) { Value = Encryption.EncryptString(Value, EncryptionKey); } XmlNode Node = Dom.DocumentElement.SelectSingleNode( XmlNamespacePrefix + ConfigSection + "/" + XmlNamespacePrefix + "add[@key='" + Field.Name + "']", XmlNamespaces); if (Node == null) { // Create the node and attributes and write it Node = Dom.CreateNode(XmlNodeType.Element, "add", Dom.DocumentElement.NamespaceURI); XmlAttribute Attr2 = Dom.CreateAttribute("key"); Attr2.Value = Field.Name; XmlAttribute Attr = Dom.CreateAttribute("value"); Attr.Value = Value; Node.Attributes.Append(Attr2); Node.Attributes.Append(Attr); XmlNode Parent = Dom.DocumentElement.SelectSingleNode( XmlNamespacePrefix + ConfigSection, XmlNamespaces); if (Parent == null) { Parent = CreateConfigSection(Dom, ConfigSection); } Parent.AppendChild(Node); } else { // just write the value into the attribute Node.Attributes.GetNamedItem("value").Value = Value; } string XML = Node.OuterXml; } // for each try { // this will fail if permissions are not there Dom.Save(configFile); ConfigurationManager.RefreshSection(ConfigSection); } catch { return(false); } } return(true); }
/// <summary> /// Reads configuration settings from the current configuration manager. /// Uses the internal APIs to write these values. /// </summary> /// <typeparam name="TAppConfiguration"></typeparam> /// <param name="config"></param> /// <returns></returns> public override bool Read(AppConfiguration config) { // Config reading from external files works a bit differently // so use a separate method to handle it if (!string.IsNullOrEmpty(ConfigurationFile)) { return(Read(config, ConfigurationFile)); } Type typeWebConfig = config.GetType(); MemberInfo[] Fields = typeWebConfig.GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.GetField); // Set a flag for missing fields // If we have any we'll need to write them out into .config bool missingFields = false; string fieldsToEncrypt = "," + PropertiesToEncrypt.ToLower() + ","; // Refresh the sections - req'd after write operations // sometimes sections don't want to re-read if (string.IsNullOrEmpty(ConfigurationSection)) { ConfigurationManager.RefreshSection("appSettings"); } else { ConfigurationManager.RefreshSection(ConfigurationSection); } // Loop through all fields and properties foreach (MemberInfo Member in Fields) { string typeName = null; FieldInfo field = null; PropertyInfo property = null; Type fieldType = null; if (Member.MemberType == MemberTypes.Field) { field = (FieldInfo)Member; fieldType = field.FieldType; typeName = fieldType.Name.ToLower(); } else if (Member.MemberType == MemberTypes.Property) { property = (PropertyInfo)Member; fieldType = property.PropertyType; typeName = fieldType.Name.ToLower(); } else { continue; } string fieldName = Member.Name.ToLower(); // Error Message is an internal public property if (fieldName == "errormessage" || fieldName == "provider") { continue; } string value = null; if (string.IsNullOrEmpty(ConfigurationSection)) { value = ConfigurationManager.AppSettings[fieldName]; } else { NameValueCollection Values = ConfigurationManager.GetSection(ConfigurationSection) as NameValueCollection; if (Values != null) { value = Values[fieldName]; } } if (value == null) { missingFields = true; continue; } // If we're encrypting decrypt any field that are encyrpted if (value != string.Empty && fieldsToEncrypt.IndexOf("," + fieldName + ",") > -1) { value = Encryption.DecryptString(value, EncryptionKey); } try { // Assign the value to the property ReflectionUtils.SetPropertyEx(config, fieldName, ReflectionUtils.StringToTypedValue(value, fieldType, CultureInfo.InvariantCulture)); } catch {; } } // We have to write any missing keys if (missingFields) { Write(config); } return(true); }