App.config how to use 'ConnectionString' encrypted

6

In my app.config I have a section that for my String connection:

 </configSections>
<connectionStrings>
    <add name="Azure.Onee" connectionString="Server=tcp:tps****.database.windows.net,1433;Data Source=tps****.database.windows.net;Initial Catalog=Onee;Persist Security Info=False;User ID=*****;Password=*****;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30" providerName="System.Data.SqlClient" />
</connectionStrings>

Then, I encrypted the section using aspnet_regiis -pef , I got as a result:

 <connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider">
    <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"
        xmlns="http://www.w3.org/2001/04/xmlenc#">
        <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
        <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
            <EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
                <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
                <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
                    <KeyName>Rsa Key</KeyName>
                </KeyInfo>
                <CipherData>
                    <CipherValue>Z3lLfHojiPPq+ACyP3o0nM/XdwDVj2jsDIMwER/xT6gxR7qzBgJPJIb7kpaZIaUJwQQjlV9fnmsNlpOM0dFrH+8J2Z4tpYM5mIcDMaJjW/dIXwXvEXdk7ESgaKSbpPgHOElvRMwQgs5LSWVjdqpP9G39StgoGoeTKlaIi7CXeSo=</CipherValue>
                </CipherData>
            </EncryptedKey>
        </KeyInfo>
        <CipherData>
            <CipherValue>z0dsujvZ7MffBHfNj12d+TYHHbcvdW84vCrZKt0ldps=</CipherValue>
        </CipherData>
    </EncryptedData>
</connectionStrings>

Well, now how do I use to retrieve connectionStrings in order to open the connection to my database?

To make the connection I'm doing this:

 private void simpleButton1_Click(object sender, EventArgs e)
    {
        string Usuario = textBox2.Text, Password = textBox1.Text;

        SqlConnection sqlconn = new SqlConnection(ConfigurationManager.ConnectionStrings["Azure.Onee"].ConnectionString);

        if (Usuario != string.Empty && Password != string.Empty)
        {
            try
            {
                consql._sql = @"SELECT id_usu FROM login WHERE usuario = @usuario AND password = @password";

                SqlCommand cmd = new SqlCommand(consql._sql, sqlconn);

                cmd.Parameters.Add("@usuario", SqlDbType.VarChar).Value = Usuario;
                cmd.Parameters.Add("@password", SqlDbType.VarChar).Value = Password;

                sqlconn.Open();

                // etc..
            }
            catch (Exception)
            {

            }
            finally
            {
                sqlconn.Close();
            }
        }

When trying to run, the error is returned:

ItriedtorunonlywithConfigurationManager.ConnectionStringswithouttheindex,hereitneithercompiles:

WiththehelpofourcolleagueCigano,wherehesuggestedtousetheindex0,itpasses,butitisbringingaStringtotallydifferentfromthe%withoriginal%:

    
asked by anonymous 12.09.2016 / 14:52

1 answer

3

As I said in comment, use instead of the connection string name:

SqlConnection sqlconn = new SqlConnection(ConfigurationManager.ConnectionStrings[1].Conn‌​ectionString);

In encryption, the connection string name is lost.

Also be careful when encrypting projects whose Web.config has connection strings .

The configuration file name must be Web.config , not App.config . After encryption you can change it if you want.

    
13.09.2016 / 16:56