Closed holpit closed 10 years ago
Can you check the test in the project here: https://github.com/RickStrahl/Westwind.ApplicationConfiguration/blob/master/Westwind.Utilities.Configuration.Tests/XmlFileConfigurationTests.cs
Check the last test method for the encryption test at the end of the file. That works, and should demonstrate what you need. Walk up to the config class to see how the properties are encrypted.
Make sure your provider is configured right:
protected override IConfigurationProvider OnCreateDefaultProvider(string sectionName, object configData)
{
string xmlFile = "XmlConfiguration.xml";
if (configData != null)
xmlFile = xmlFile;
var provider = new XmlFileConfigurationProvider<XmlFileConfiguration>()
{
XmlConfigurationFile = xmlFile,
EncryptionKey = "ultra-seekrit", // use a generated value here
PropertiesToEncrypt = "Password"
};
return provider;
}
If it doesn't work please provide a repro test or post code here.
Thank you Rick for the answer, but I can't figure out how to encrypt. This is my sample console test (note also that , related to other issue, there is no error on ErrorMesage if the file is readonly and the write fails)
class Program { static void Main(string[] args) {
ProfilesXmlConfig.Configuration.Initialize();
ProfilesXmlConfig.Configuration.Description = "Test encryption";
if (!ProfilesXmlConfig.Configuration.ConnectionsList.Any())
ProfilesXmlConfig.Configuration.ConnectionsList.Add(new MainClass.NestedClass() { Connection = "Connection 1", Password = "Encrypt me" });
//The Password field of NestedClass is not encrypted
ProfilesXmlConfig.Configuration.Write();
//Set file in ReadOnly must raise an error
File.SetAttributes("Profiles.xml", FileAttributes.ReadOnly);
ProfilesXmlConfig.Configuration.Description = "Test save not allowed";
var success = ProfilesXmlConfig.Configuration.Write();
if (!success)
//The ErroreMessage in empty
Console.WriteLine("Write failed. Error: {0}", ProfilesXmlConfig.Configuration.ErrorMessage);
Console.ReadLine();
}
}
public class ProfilesXmlConfig
{
public static MainClass Configuration { get; set; }
static ProfilesXmlConfig()
{
const string xmlFile = "Profiles.xml";
var provider = new XmlFileConfigurationProvider<MainClass>()
{
XmlConfigurationFile = xmlFile,
EncryptionKey = "test-cryptKey",
PropertiesToEncrypt = "Password"
};
Configuration = new MainClass();
Configuration.Initialize(provider);
}
}
}
public class MainClass : AppConfiguration {
public string Description { get; set; }
public List<NestedClass> ConnectionsList { get; set; }
public MainClass()
{
ConnectionsList = new List<NestedClass>();
}
public class NestedClass
{
public string Connection { get; set; }
public string Password { get; set; }
}
Thank you for support. Ivano
@holpit - I've added support for nested property syntax in the PropertiesToEncrypt property. You should now be able to use "Password,License.LicenseKey" for property syntax.
Hi Rick, I'm trying your ApplicationConfiguration with xmlProvider. I wonder if is possible to encrypt a nested property ? In your example, I need to encrypt LicenseKey. PropertiesToEncrypt = "LicenseKey" or PropertiesToEncrypt = "LicenseInformation.LicenseKey" doesn't work.
public class CustomConfigFileConfiguration : Westwind.Utilities.Configuration.AppConfiguration { public string ApplicationName { get; set; }
public LicenseInformation ComplexType { get; set; }
}
Thaks, Ivano