예제 #1
0
        public void SecureLoad()
        {
            KeyManager.Create();

            var providerLoader = new ProviderLoader();

            var loader = new SettingsLoader();
            loader.XmlFileBySection();
            loader.Loaded += providerLoader.TryExtractConfigProtectedData;

            loader.Loaded += (s, e) =>
            {
                Console.WriteLine("Loaded: {0} ({1})", e.Settings.GetType(), e.Settings.Identity);
            };

            var settings = loader
                .LoadSettings(new XmlFileSettings("Examples/AppDirectory/secureMain.config".ResolveTestPath()))
                .Joined.ToAppSettings();

            var addCfg = settings.TryGet<ExampleCombineConfig>("AdditionalConfig");

            Assert.IsNotNull(addCfg);
            Assert.AreEqual("InUpDirectory", addCfg.F);

            Assert.AreEqual("Server=localhost;Database=workDb;User ID=admin;Password=pass;", settings.TryGet<ConnectionConfig>("MyExtConnection").ConnectionString);
            Assert.AreEqual("Server=localhost;Database=workDb;User ID=admin;Password=pass;", settings.TryGet<ConnectionConfig>("MySecuredConnection").ConnectionString);

            KeyManager.Delete();
        }
예제 #2
0
        public void ProviderFiltering()
        {
            var settings = @"<Config>
            <configProtectedData>
            <providers>
            <add name='ClearingProvider' type='System.Configuration.RsaProtectedConfigurationProvider, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' keyContainerName='SampleKeys' useMachineContainer='true' />
            <clear/>
            <add name='RsaProvider' type='System.Configuration.RsaProtectedConfigurationProvider, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' keyContainerName='SampleKeys' useMachineContainer='true' />
            <add name='MissedProvider' type='System.Configuration.DpapiProtectedConfigurationProvider, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' />
            <add name='DpapiProvider' type='System.Configuration.DpapiProtectedConfigurationProvider, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' />
            </providers>
            </configProtectedData>
            </Config>".ToXmlSettings().ToAppSettings();

            var names = new List<string>();

            var providers = new ProviderLoader()
                .SubscribeLoading((s, e) =>
                {
                    names.Add(e.Name);
                    if (e.Name == "MissedProvider")
                        e.Canceled = true;
                })
                .LoadAppSettings(settings)
                .Providers;

            CollectionAssert.AreEqual(new string[] { "ClearingProvider", "RsaProvider", "MissedProvider", "DpapiProvider" }, names);
            Assert.IsNull(providers.Get("MissedProvider"));
            Assert.IsNull(providers.Get("ClearingProvider"));
            Assert.IsNotNull(providers.Get("RsaProvider"));
            Assert.IsNotNull(providers.Get("DpapiProvider"));
        }
        public void SecureLoad()
        {
            KeyManager.Create();

            var providerLoader = new ProviderLoader();
            var xmlFileLoader = new XmlFileSettingsLoader(Global.GenericDeserializer, Global.PlainConverter);

            var loader = new SettingsLoader(xmlFileLoader);
            loader.Loaded += providerLoader.TryExtractConfigProtectedData;

            loader.Loaded += (s, e) =>
            {
                Console.WriteLine("Loaded: {0} ({1})", e.Settings.GetType(), e.Settings.Identity);
            };

            loader.LoadSettings(xmlFileLoader.LoadFile("Examples/AppDirectory/secureMain.config"));

            IAppSettings settings = loader.Settings;

            var addCfg = settings.TryCombine<ExampleCombineConfig>("AdditionalConfig");

            Assert.IsNotNull(addCfg);
            Assert.AreEqual("InUpDirectory", addCfg.F);

            Assert.AreEqual("Server=localhost;Database=workDb;User ID=admin;Password=pass;", settings.TryFirst<ConnectionConfig>("MyExtConnection").ConnectionString);
            Assert.AreEqual("Server=localhost;Database=workDb;User ID=admin;Password=pass;", settings.TryFirst<ConnectionConfig>("MySecuredConnection").ConnectionString);

            KeyManager.Delete();
        }