// </Snippet2>

        // <Snippet3>
        static void GetProviderCollection()
        {
            try
            {
                // Get the application configuration.
                Configuration config =
                    ConfigurationManager.OpenExeConfiguration(
                        ConfigurationUserLevel.None);

                // Get the protected configuration section.
                ProtectedConfigurationSection pcSection =
                    (System.Configuration.ProtectedConfigurationSection)
                    config.GetSection("configProtectedData");

                Console.WriteLine(
                    "Protected configuration section providers:");
                foreach (ProviderSettings ps in
                         pcSection.Providers)
                {
                    Console.WriteLine("  {0}", ps.Name);
                }
            }
            catch (ConfigurationErrorsException e)
            {
                Console.WriteLine(e.ToString());
            }
        }
Пример #2
0
        /// <summary>
        /// Gets or creates protected provider section
        /// </summary>
        /// <param name="config">The configuration</param>
        private static void CreateProtectProvider(Configuration config)
        {
            var protectedConfigurationSection = config.GetSection(ProtectionProviderSectionName) as ProtectedConfigurationSection;

            bool found = false;

            if (protectedConfigurationSection != null)
            {
                foreach (ProviderSettings provider in protectedConfigurationSection.Providers)
                {
                    if (provider.Name.Equals(ProtectionProvider))
                    {
                        found = true;
                        break;
                    }
                }
            }
            else
            {
                protectedConfigurationSection = new ProtectedConfigurationSection();
                config.Sections.Add(ProtectionProviderSectionName, protectedConfigurationSection);
            }

            if (!found)
            {
                protectedConfigurationSection.Providers.Add(
                    new ProviderSettings(ProtectionProvider, typeof(DpapiProtectedConfigurationProvider).AssemblyQualifiedName));
            }
        }
Пример #3
0
        public static void Main(string[] args)
        {
            String keyContainerName = args[0];
            String description      = args[1];
            String providerName     = args[2];

            System.Configuration.Configuration machineConfig =
                System.Configuration.ConfigurationManager.OpenMachineConfiguration();
            System.Configuration.ProviderSettings settings =
                new System.Configuration.ProviderSettings(providerName,
                                                          "System.Configuration.RsaProtectedConfigurationProvider,
                System.Configuration,
                Version=2.0.0.0, Culture=neutral,
                PublicKeyToken=b03f5f7f11d50a3a");
            settings.Parameters["description"]         = description;
            settings.Parameters["keyContainerName"]    = keyContainerName;
            settings.Parameters["cspProviderName"]     = String.Empty;
            settings.Parameters["useMachineContainer"] = "true";
            settings.Parameters["useOAEP"]             = "false";
            settings.Parameters["name"] = providerName;
            ProtectedConfigurationSection pcSection =
                (System.Configuration.ProtectedConfigurationSection)machineConfig.GetSection("configProtectedData");

            pcSection.Providers.Add(settings);
            machineConfig.Save();
        }
Пример #4
0
        private static void GetProviderSettings()
        {
            // Get the application configuration file.
            System.Configuration.Configuration config =
                ConfigurationManager.OpenExeConfiguration(
                    ConfigurationUserLevel.None);

            ProtectedConfigurationSection pSection =
                config.GetSection("configProtectedData")
                as ProtectedConfigurationSection;

            ProviderSettingsCollection providerSettings =
                pSection.Providers;

            foreach (ProviderSettings pSettings in
                     providerSettings)
            {
                // <Snippet2>

                Console.WriteLine(
                    "Provider settings name: {0}",
                    pSettings.Name);

                // </Snippet2>

                // <Snippet3>
                Console.WriteLine(
                    "Provider settings type: {0}",
                    pSettings.Type);
                // </Snippet3>

                // <Snippet4>
                NameValueCollection parameters =
                    pSettings.Parameters;

                IEnumerator pEnum =
                    parameters.GetEnumerator();

                int i = 0;
                while (pEnum.MoveNext())
                {
                    string pLength =
                        parameters[i].Length.ToString();
                    Console.WriteLine(
                        "Provider ssettings: {0} has {1} parameters",
                        pSettings.Name, pLength);
                }
                // </Snippet4>
            }
        }
Пример #5
0
        public static void Main(string[] args)
        {
            String provider = args[0];  // example: DataProtectionConfigurationProvider

            System.Configuration.Configuration machineConfig =
                System.Configuration.ConfigurationManager.OpenMachineConfiguration();
            ProtectedConfigurationSection pcSection =
                (System.Configuration.ProtectedConfigurationSection)machineConfig.GetSection("configProtectedData");
            string oldEncryptionProviderName = pcSection.DefaultProvider;

            Console.WriteLine("The default provider is currently: " + oldEncryptionProviderName);
            Console.WriteLine("Changing the default provider to: " + provider);
            pcSection.DefaultProvider = provider;
            machineConfig.Save();
        }
Пример #6
0
        private void EnsureHKHProvider()
        {
            ProtectedConfigurationSection providerSection = config.GetSection("configProtectedData") as ProtectedConfigurationSection;
            bool hasHKHProvider = false;

            foreach (ProviderSettings provider in providerSection.Providers)
            {
                if (provider.Name == ProtectedProviderName_HKH)
                {
                    hasHKHProvider = true;
                    break;
                }
            }

            if (!hasHKHProvider)
            {
                providerSection.Providers.Add(new ProviderSettings(ProtectedProviderName_HKH, "HKH.Configuration.HKHProtectedConfigurationProvider, HKH.Common"));
            }
        }
Пример #7
0
        private void InitProviderNames(Configuration config)
        {
            cBProviderNames.Items.Clear();
            if (config == null)
            {
                return;
            }

            ProtectedConfigurationSection pcs = config.GetSection("configProtectedData") as ProtectedConfigurationSection;

            if (pcs == null)
            {
                return;
            }
            foreach (ProviderSettings ps in pcs.Providers)
            {
                cBProviderNames.Items.Add(ps.Name);
            }
            cBProviderNames.SelectedItem = pcs.DefaultProvider;
        }
        // <Snippet2>
        static void GetDefaultProvider()
        {
            try
            {
                // Get the application configuration.
                Configuration config =
                    ConfigurationManager.OpenExeConfiguration(
                        ConfigurationUserLevel.None);

                // Get the protected configuration section.
                ProtectedConfigurationSection pcSection =
                    (System.Configuration.ProtectedConfigurationSection)
                    config.GetSection("configProtectedData");

                // Get the current DefaultProvider.
                Console.WriteLine(
                    "Protected configuration section default provider:");
                Console.WriteLine("  {0}", pcSection.DefaultProvider);
            }
            catch (ConfigurationErrorsException e)
            {
                Console.WriteLine(e.ToString());
            }
        }
Пример #9
0
 string IInternalConfigHost.EncryptSection(string clearTextXml, ProtectedConfigurationProvider protectionProvider,
                                           ProtectedConfigurationSection protectedConfigSection)
 {
     return(ProtectedConfigurationSection.EncryptSection(clearTextXml, protectionProvider));
 }
Пример #10
0
 string IInternalConfigHost.DecryptSection(string encryptedXml, ProtectedConfigurationProvider protectionProvider,
                                           ProtectedConfigurationSection protectedConfigSection)
 {
     return(ProtectedConfigurationSection.DecryptSection(encryptedXml, protectionProvider));
 }
Пример #11
0
        public virtual string EncryptSection(string clearXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedSection)
        {
            if (protectedSection == null)
            {
                throw new ArgumentNullException("protectedSection");
            }

            return(protectedSection.EncryptSection(clearXml, protectionProvider));
        }
Пример #12
0
 string IInternalConfigHost.EncryptSection(string clearTextXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedConfigSection)
 {
     throw new NotImplementedException();
 }
Пример #13
0
 public virtual string EncryptSection(string clearTextXml, ProtectedConfigurationProvider protectionProvider,
     ProtectedConfigurationSection protectedConfigSection)
 {
     return Host.EncryptSection(clearTextXml, protectionProvider, protectedConfigSection);
 }
Пример #14
0
 public virtual string DecryptSection(string encryptedXml, ProtectedConfigurationProvider protectionProvider,
     ProtectedConfigurationSection protectedConfigSection)
 {
     return Host.DecryptSection(encryptedXml, protectionProvider, protectedConfigSection);
 }
 /////////////////////////////////////////////////////////////////////////////////
 /////////////////////////////////////////////////////////////////////////////////
 // Encrypt/decrypt support
 public override string DecryptSection(string encryptedXmlString, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedConfigSection)
 {
     return(CallEncryptOrDecrypt(false, encryptedXmlString, protectionProvider, protectedConfigSection));
 }
 public override string EncryptSection(string clearTextXmlString, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedConfigSection)
 {
     return(CallEncryptOrDecrypt(true, clearTextXmlString, protectionProvider, protectedConfigSection));
 }
Пример #17
0
        private string CallEncryptOrDecrypt(bool doEncrypt, string xmlString, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedConfigSection)
        {
            string str = null;
            WindowsImpersonationContext context    = null;
            string           assemblyQualifiedName = protectionProvider.GetType().AssemblyQualifiedName;
            ProviderSettings settings = protectedConfigSection.Providers[protectionProvider.Name];

            if (settings == null)
            {
                throw System.Web.Util.ExceptionUtil.ParameterInvalid("protectionProvider");
            }
            NameValueCollection parameters = settings.Parameters;

            if (parameters == null)
            {
                parameters = new NameValueCollection();
            }
            string[] allKeys         = parameters.AllKeys;
            string[] parameterValues = new string[allKeys.Length];
            for (int i = 0; i < allKeys.Length; i++)
            {
                parameterValues[i] = parameters[allKeys[i]];
            }
            if (this._Identity != null)
            {
                context = this._Identity.Impersonate();
            }
            try
            {
                try
                {
                    IRemoteWebConfigurationHostServer o = CreateRemoteObject(this._Server, this._Username, this._Domain, this._Password);
                    try
                    {
                        str = o.DoEncryptOrDecrypt(doEncrypt, xmlString, protectionProvider.Name, assemblyQualifiedName, allKeys, parameterValues);
                    }
                    finally
                    {
                        while (Marshal.ReleaseComObject(o) > 0)
                        {
                        }
                    }
                    return(str);
                }
                finally
                {
                    if (context != null)
                    {
                        context.Undo();
                    }
                }
            }
            catch
            {
            }
            return(str);
        }
 public virtual string DecryptSection(string encryptedXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedConfigSection)
 {
     return(host.DecryptSection(encryptedXml, protectionProvider, protectedConfigSection));
 }
Пример #19
0
        private void btnExecute_Click(object sender, EventArgs e)
        {
            if (currConf == null)
            {
                return;
            }
            try
            {
                ConfigurationSection cnStrSec = currConf.ConnectionStrings;
                string provider = txtRsaProvider.Text;
                if (string.IsNullOrEmpty(provider))
                {
                    provider = "RsaProtectedConfigurationProvider";
                }
                else
                {
                    //Create custom provider
                    ProtectedConfigurationSection pdSec =
                        currConf.GetSection("configProtectedData") as ProtectedConfigurationSection;
                    if (pdSec == null)
                    {
                        pdSec = new ProtectedConfigurationSection();
                        currConf.Sections.Add("configProtectedData", pdSec);
                    }
                    bool found = false;
                    foreach (ProviderSettings p in pdSec.Providers)
                    {
                        if (p.Name == provider)
                        {
                            found = true;
                        }
                    }
                    if (!found)
                    {
                        ProviderSettings ps = new ProviderSettings();
                        ps.Name = provider;
                        ps.Type = "System.Configuration.RsaProtectedConfigurationProvider,System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a";
                        ps.Parameters.Add("keyContainerName", provider);
                        ps.Parameters.Add("useMachineContainer", "true");
                        pdSec.Providers.Add(ps);
                        //Note: A new key container will be created silently, if the key container name doesn't exist.
                        //      But the auto created key container is not exportable and inappropriate for shared key among
                        //      web farm servers.  Please use RSA key manager to create exportable key container for web farm
                        //      scenarios.
                    }
                }

                if (btnExecute.Text == Resources.Message.Encrypt)
                {
                    cnStrSec.SectionInformation.ProtectSection(provider);
                }
                else
                {
                    cnStrSec.SectionInformation.UnprotectSection();
                }
                currConf.Save();
                cbxWebApp_SelectedIndexChanged(cbxWebApp, e);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
        }
Пример #20
0
 string IInternalConfigHost.EncryptSection(string clearTextXml, ProtectedConfigurationProvider protectionProvider,
     ProtectedConfigurationSection protectedConfigSection)
 {
     return ProtectedConfigurationSection.EncryptSection(clearTextXml, protectionProvider);
 }
Пример #21
0
 string IInternalConfigHost.DecryptSection(string encryptedXml, ProtectedConfigurationProvider protectionProvider,
     ProtectedConfigurationSection protectedConfigSection)
 {
     return ProtectedConfigurationSection.DecryptSection(encryptedXml, protectionProvider);
 }
        private string CallEncryptOrDecrypt(bool doEncrypt, string xmlString, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedConfigSection)
        {
#if !FEATURE_PAL // FEATURE_PAL has no COM objects => no encryption
            // ROTORTODO: COM Objects are not implemented.
            // CORIOLISTODO: COM Objects are not implemented.
            ProviderSettings    ps;
            NameValueCollection nvc;
            string  []          paramKeys;
            string  []          paramValues;
            string returnString = null;
            string typeName;
            WindowsImpersonationContext wiContext = null;

            ////////////////////////////////////////////////////////////
            // Step 1: Create list of parameters for the protection provider
            typeName = protectionProvider.GetType().AssemblyQualifiedName;
            ps       = protectedConfigSection.Providers[protectionProvider.Name];
            if (ps == null)
            {
                throw ExceptionUtil.ParameterInvalid("protectionProvider");
            }

            nvc = ps.Parameters;
            if (nvc == null)
            {
                nvc = new NameValueCollection();
            }

            paramKeys   = nvc.AllKeys;
            paramValues = new string[paramKeys.Length];
            for (int iter = 0; iter < paramKeys.Length; iter++)
            {
                paramValues[iter] = nvc[paramKeys[iter]];
            }

            ////////////////////////////////////////////////////////////
            // Step 2: Set the impersonation if required
            if (_Identity != null)
            {
                wiContext = _Identity.Impersonate();
            }

            try {
                try {
                    //////////////////////////////////////////////////////////////////
                    // Step 3: Get the type and create the object on the remote server
                    IRemoteWebConfigurationHostServer remoteSrv = CreateRemoteObject(_Server, _Username, _Domain, _Password);
                    try {
                        //////////////////////////////////////////////////////////////////
                        // Step 4: Call the API
                        returnString = remoteSrv.DoEncryptOrDecrypt(doEncrypt, xmlString, protectionProvider.Name, typeName, paramKeys, paramValues);
                    } finally {
                        while (Marshal.ReleaseComObject(remoteSrv) > 0)
                        {
                        }                                                   // release the COM object
                    }
                } finally {
                    if (wiContext != null)
                    {
                        wiContext.Undo(); // revert impersonation
                    }
                }
            }
            catch {
            }

            return(returnString);
#else       // !FEATURE_PAL
            throw new NotImplementedException("ROTORTODO");
#endif      // !FEATURE_PAL
        }
 public virtual string EncryptSection(string clearTextXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedConfigSection)
 {
     return(host.EncryptSection(clearTextXml, protectionProvider, protectedConfigSection));
 }
Пример #24
0
 public virtual string EncryptSection(string encryptedXml, ProtectedConfigurationProvider protectionProvider,
                                      ProtectedConfigurationSection protectedSection)
 {
     throw new NotImplementedException();
 }