コード例 #1
0
        public void ReadAndWriteToEncryptedConfiguration()
        {
            const string sectionName = "MyEncryptedConfig";

            KeyAlgorithmPair keyAlgorithmPair = new KeyAlgorithmPair();
            keyAlgorithmPair.Key = new SymmetricAlgorithmKeyCreator(typeof(RijndaelManaged).AssemblyQualifiedName).GenerateKey();
            keyAlgorithmPair.AlgorithmTypeName = typeof(RijndaelManaged).AssemblyQualifiedName;

            FileKeyAlgorithmPairStorageProvider provider = new FileKeyAlgorithmPairStorageProvider();
            provider.ConfigurationName = "FileKeyAlgorithmPairStorageProviderData";

            using (ConfigurationBuilder builder = new ConfigurationBuilder())
            {
                // change wher the file is
                RuntimeConfigurationView view = new RuntimeConfigurationView(new ConfigurationContext(new NonDisposingWrapper(builder)));

                FileKeyAlgorithmPairStorageProviderData fileData = (FileKeyAlgorithmPairStorageProviderData)view.GetKeyAlgorithmPairStorageProviderData();
                fileData.Path = Path.GetTempFileName();
                provider.Initialize(view);
                provider.Save(keyAlgorithmPair);
                builder.WriteConfiguration(sectionName, data);
                MockConfigurationData myData = (MockConfigurationData)builder.ReadConfiguration(sectionName);
                Assert.AreEqual(myData.ToString(), data.ToString());
                builder.ClearSectionCache();
                myData = (MockConfigurationData)builder.ReadConfiguration(sectionName);
                Assert.AreEqual(myData.ToString(), data.ToString());
            }
        }
コード例 #2
0
        private void SaveAndLoad(RuntimeConfigurationView configurationView)
        {
            FileKeyAlgorithmPairStorageProvider provider = new FileKeyAlgorithmPairStorageProvider();
            provider.Initialize(configurationView);

            provider.Save(DefaultKey);
            KeyAlgorithmPair key = provider.Load();

            Assert.IsNotNull(key);
            Assert.AreEqual(DefaultKey.AlgorithmTypeName, key.AlgorithmTypeName);
            Assert.IsTrue(CryptographyUtility.CompareBytes(DefaultKey.Key, key.Key));
            FileKeyAlgorithmPairStorageProviderData data = (FileKeyAlgorithmPairStorageProviderData)configurationView.GetKeyAlgorithmPairStorageProviderData();
            // If protected, ensure it's protected properly
            if (null != data.DpapiSettings)
            {
                using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    BinaryFormatter formatter = new BinaryFormatter();
                    bool deserialized = true;
                    try
                    {
                        KeyAlgorithmPair deserializedKey = (KeyAlgorithmPair)formatter.Deserialize(fs);
                        Assert.IsNotNull(deserializedKey);
                    }
                    catch
                    {
                        deserialized = false;
                    }

                    // Shouldn't deserialize if it's DPAPI protected
                    Assert.IsFalse(deserialized);
                }
            }
        }
コード例 #3
0
        private void SaveAndLoad(RuntimeConfigurationView configurationView)
        {
            FileKeyAlgorithmPairStorageProvider provider = new FileKeyAlgorithmPairStorageProvider();

            provider.Initialize(configurationView);

            provider.Save(DefaultKey);
            KeyAlgorithmPair key = provider.Load();

            Assert.IsNotNull(key);
            Assert.AreEqual(DefaultKey.AlgorithmTypeName, key.AlgorithmTypeName);
            Assert.IsTrue(CryptographyUtility.CompareBytes(DefaultKey.Key, key.Key));
            FileKeyAlgorithmPairStorageProviderData data = (FileKeyAlgorithmPairStorageProviderData)configurationView.GetKeyAlgorithmPairStorageProviderData();

            // If protected, ensure it's protected properly
            if (null != data.DpapiSettings)
            {
                using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    BinaryFormatter formatter    = new BinaryFormatter();
                    bool            deserialized = true;
                    try
                    {
                        KeyAlgorithmPair deserializedKey = (KeyAlgorithmPair)formatter.Deserialize(fs);
                        Assert.IsNotNull(deserializedKey);
                    }
                    catch
                    {
                        deserialized = false;
                    }

                    // Shouldn't deserialize if it's DPAPI protected
                    Assert.IsFalse(deserialized);
                }
            }
        }
コード例 #4
0
        private FileKeyAlgorithmPairStorageProviderData GetFileKeyAlgorithmPairStorageProviderData()
        {
            KeyAlgorithmPairStorageProviderData keyAlgorithmPairStorageProviderData = runtimeConfigurationView.GetKeyAlgorithmPairStorageProviderData();

            ArgumentValidation.CheckExpectedType(keyAlgorithmPairStorageProviderData, typeof(FileKeyAlgorithmPairStorageProviderData));

            return((FileKeyAlgorithmPairStorageProviderData)keyAlgorithmPairStorageProviderData);
        }
コード例 #5
0
        public void ReadAndWriteToEncryptedConfiguration()
        {
            const string sectionName = "MyEncryptedConfig";

            KeyAlgorithmPair keyAlgorithmPair = new KeyAlgorithmPair();

            keyAlgorithmPair.Key = new SymmetricAlgorithmKeyCreator(typeof(RijndaelManaged).AssemblyQualifiedName).GenerateKey();
            keyAlgorithmPair.AlgorithmTypeName = typeof(RijndaelManaged).AssemblyQualifiedName;

            FileKeyAlgorithmPairStorageProvider provider = new FileKeyAlgorithmPairStorageProvider();

            provider.ConfigurationName = "FileKeyAlgorithmPairStorageProviderData";

            using (ConfigurationBuilder builder = new ConfigurationBuilder())
            {
                // change wher the file is
                RuntimeConfigurationView view = new RuntimeConfigurationView(new ConfigurationContext(new NonDisposingWrapper(builder)));

                FileKeyAlgorithmPairStorageProviderData fileData = (FileKeyAlgorithmPairStorageProviderData)view.GetKeyAlgorithmPairStorageProviderData();
                fileData.Path = Path.GetTempFileName();
                provider.Initialize(view);
                provider.Save(keyAlgorithmPair);
                builder.WriteConfiguration(sectionName, data);
                MockConfigurationData myData = (MockConfigurationData)builder.ReadConfiguration(sectionName);
                Assert.AreEqual(myData.ToString(), data.ToString());
                builder.ClearSectionCache();
                myData = (MockConfigurationData)builder.ReadConfiguration(sectionName);
                Assert.AreEqual(myData.ToString(), data.ToString());
            }
        }