Error using Rsaprotectedconfigurationprovider

Asked

Viewed 32 times

1

I am making an example using Rsaprotectedconfigurationprovider and I generated an error, I am unable to solve the problem.

NOTE: correction, the error was solved, I had forgotten a detail, now if possible I would like to know if I can call these methods in my application, is how would do it?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.Security;
using System.Configuration;


namespace CriptografiaMD5
{
    public class cifrawebconfig
    {

        // Proteja a seção connectionStrings.
        private static void ProtegerConfiguration()
        {
            // Obter o arquivo de configuração do aplicativo.
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            // Define o nome do provedor de RSA.
            string provider = "RsaProtectedConfigurationProvider";


            ConfigurationSection connStrings = config.ConnectionStrings;

            if (connStrings != null)
            {
                if (!connStrings.SectionInformation.IsProtected)
                {
                    if (!connStrings.ElementInformation.IsLocked)
                    {
                        // Proteja a seção.
                        connStrings.SectionInformation.ProtectSection(provider);

                        connStrings.SectionInformation.ForceSave = true;
                        config.Save(ConfigurationSaveMode.Full);

                        Console.WriteLine("Seção {0} está agora protegida por {1}",
                            connStrings.SectionInformation.Name,
                            connStrings.SectionInformation.ProtectionProvider.Name);
                    }
                    else
                        Console.WriteLine(
                             "Não é possível proteger, section {0} está bloqueado",
                             connStrings.SectionInformation.Name);
                }
                else
                    Console.WriteLine(
                        "Seção {0} já está protegido por {1}",
                        connStrings.SectionInformation.Name,
                        connStrings.SectionInformation.ProtectionProvider.Name);
            }
            else
                Console.WriteLine("Não é possível obter a seção {0}",
                    connStrings.SectionInformation.Name);

        }

        // Desproteger a seção connectionStrings. 
        private static void DesprotegerConfiguration()
        {

            // Obter o arquivo de configuração do aplicativo.
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            // Obter seção para desproteger.
            ConfigurationSection connStrings = config.ConnectionStrings;

            if (connStrings != null)
            {
                if (connStrings.SectionInformation.IsProtected)
                {
                    if (!connStrings.ElementInformation.IsLocked)
                    {
                        // Desproteger a seção.
                        connStrings.SectionInformation.UnprotectSection();

                        connStrings.SectionInformation.ForceSave = true;
                        config.Save(ConfigurationSaveMode.Full);

                        Console.WriteLine("Seção {0} é agora desprotegido.",
                            connStrings.SectionInformation.Name);

                    }
                    else
                        Console.WriteLine(
                             "Não pode desproteger, section {0} está bloqueado",
                             connStrings.SectionInformation.Name);
                }
                else
                    Console.WriteLine(
                        "Seção {0} já está desprotegida.",
                        connStrings.SectionInformation.Name);

            }
            else
                Console.WriteLine("Não é possível obter a seção {0}",
                    connStrings.SectionInformation.Name);

        }


    }


}
  • On which line the error occurs?

  • Buddy, I made the code correction, see now if you can help, thank you!

1 answer

1

To call the functions:

btnCripto_Click(object sender, EventArgs e) { 
    new cifrawebconfig().ProtegerConfiguration();
    // new cifrawebconfig().DesprotegerConfiguration();
}

They can be called on Global.asax, in a Filter, in a Controller...

  • Friend, excuse my lack of knowledge, but what would that look like in this context? protected void btnCripto_Click(Object Sender, Eventargs and) { }

  • @itasouza I updated the response.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.