Click here to Skip to main content
15,892,059 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to make client server Pin
King Shez6-Sep-06 20:55
King Shez6-Sep-06 20:55 
GeneralRe: How to make client server Pin
Andrei Ungureanu7-Sep-06 3:02
Andrei Ungureanu7-Sep-06 3:02 
AnswerRe: How to make client server Pin
Rob Philpott5-Sep-06 4:36
Rob Philpott5-Sep-06 4:36 
GeneralRe: How to make client server Pin
King Shez5-Sep-06 21:10
King Shez5-Sep-06 21:10 
Questioncrystal Report problem Pin
ranandbe5-Sep-06 1:27
ranandbe5-Sep-06 1:27 
AnswerRe: crystal Report problem Pin
trannguyen865-Sep-06 1:47
trannguyen865-Sep-06 1:47 
GeneralRe: crystal Report problem Pin
ranandbe5-Sep-06 2:49
ranandbe5-Sep-06 2:49 
QuestionEncrypted Connection Strings in app.config Pin
Gomac5-Sep-06 1:03
Gomac5-Sep-06 1:03 
Hi,

I am using the following code to encrypt/decrypt a connection string in my app.config file:
    private void ProtectConfiguration()<br />
        {<br />
<br />
            // Get the application configuration file.<br />
            config = ConfigurationManager.OpenExeConfiguration(myGlobalFilePath);<br />
        <br />
            // Define the Dpapi provider name.<br />
            string provider =<br />
                "DataProtectionConfigurationProvider";<br />
<br />
<br />
            // Get the section to protect.<br />
            ConfigurationSection connStrings =<br />
                config.ConnectionStrings;<br />
<br />
            if (connStrings != null)<br />
            {<br />
                if (!connStrings.SectionInformation.IsProtected)<br />
                {<br />
                    if (!connStrings.ElementInformation.IsLocked)<br />
                    {<br />
                        // Protect the section.<br />
                        connStrings.SectionInformation.ProtectSection(provider);<br />
<br />
                        connStrings.SectionInformation.ForceSave = true;<br />
                        config.Save(ConfigurationSaveMode.Full);<br />
<br />
                        Console.WriteLine("Section {0} is now protected by {1}",<br />
                            connStrings.SectionInformation.Name,<br />
                            connStrings.SectionInformation.ProtectionProvider.Name);<br />
<br />
                    }<br />
                    else<br />
                        Console.WriteLine(<br />
                             "Can't protect, section {0} is locked",<br />
                             connStrings.SectionInformation.Name);<br />
                }<br />
                else<br />
                    Console.WriteLine(<br />
                        "Section {0} is already protected by {1}",<br />
                        connStrings.SectionInformation.Name,<br />
                        connStrings.SectionInformation.ProtectionProvider.Name);<br />
<br />
            }<br />
            else<br />
                Console.WriteLine("Can't get the section {0}",<br />
                    connStrings.SectionInformation.Name);<br />
<br />
        }<br />
<br />
<br />
        // Unprotect the connectionStrings section.<br />
        private void UnProtectConfiguration()<br />
        {<br />
<br />
            // Get the application configuration file.<br />
            config = ConfigurationManager.OpenExeConfiguration(myGlobalFilePath);<br />
<br />
            // Get the section to unprotect.<br />
            ConfigurationSection connStrings =<br />
                config.ConnectionStrings;<br />
<br />
            if (connStrings != null)<br />
            {<br />
                if (connStrings.SectionInformation.IsProtected)<br />
                {<br />
                    if (!connStrings.ElementInformation.IsLocked)<br />
                    {<br />
                        // Unprotect the section.<br />
                        connStrings.SectionInformation.UnprotectSection();<br />
<br />
                        connStrings.SectionInformation.ForceSave = true;<br />
                        config.Save(ConfigurationSaveMode.Full);<br />
<br />
                        Console.WriteLine("Section {0} is now unprotected.",<br />
                            connStrings.SectionInformation.Name);<br />
<br />
                    }<br />
                    else<br />
                        Console.WriteLine(<br />
                             "Can't unprotect, section {0} is locked",<br />
                             connStrings.SectionInformation.Name);<br />
                }<br />
                else<br />
                    Console.WriteLine(<br />
                        "Section {0} is already unprotected.",<br />
                        connStrings.SectionInformation.Name);<br />
<br />
            }<br />
            else<br />
                Console.WriteLine("Can't get the section {0}",<br />
                    connStrings.SectionInformation.Name);<br />
<br />
        }


However whenever I encrypt and decrypt the connectionStrings section of the app.config file, the connection string is not what I entered, it comes out as:

<connectionStrings><br />
        <clear /><br />
        <add name="LocalSqlServer" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"<br />
            providerName="System.Data.SqlClient" /><br />
    </connectionStrings>


I am trying to encrypt the following connection string:

<connectionStrings><br />
  <add name="MyDbName" connectionString="Data Source=MYDBSERVER;Initial Catalog=MYDBNAME;Integrated Security=False;User ID=MYID ; Password=MYPASSWORD" providerName="System.Data.SqlClient"/><br />
</connectionStrings>


Any ideas what I could be doing wrong?

Thank you in advance.
AnswerRe: Encrypted Connection Strings in app.config Pin
Gomac5-Sep-06 5:39
Gomac5-Sep-06 5:39 
QuestionReturning value from threads Pin
samtam5-Sep-06 0:43
samtam5-Sep-06 0:43 
AnswerRe: Returning value from threads Pin
WillemM5-Sep-06 1:58
WillemM5-Sep-06 1:58 
QuestiondataGridView Pin
charbelasmar5-Sep-06 0:25
charbelasmar5-Sep-06 0:25 
AnswerRe: dataGridView Pin
Nader Elshehabi5-Sep-06 12:06
Nader Elshehabi5-Sep-06 12:06 
Questionhow to set focus to a child form after I clicked a TreeNode? Pin
CooperWu5-Sep-06 0:05
CooperWu5-Sep-06 0:05 
QuestionCOleVariant in C# ? Pin
anderslundsgard4-Sep-06 23:33
anderslundsgard4-Sep-06 23:33 
AnswerRe: COleVariant in C# ? Pin
Guffa5-Sep-06 0:46
Guffa5-Sep-06 0:46 
GeneralRe: COleVariant in C# ? Pin
anderslundsgard6-Sep-06 1:54
anderslundsgard6-Sep-06 1:54 
Questionmaking a full screen application Pin
Green Fuze4-Sep-06 23:33
Green Fuze4-Sep-06 23:33 
AnswerRe: making a full screen application Pin
Jonathan van de Veen5-Sep-06 0:28
Jonathan van de Veen5-Sep-06 0:28 
QuestionIPCChannel connection problem Pin
GDavy4-Sep-06 23:08
GDavy4-Sep-06 23:08 
AnswerRe: IPCChannel connection problem Pin
ejuanpp5-Sep-06 0:05
ejuanpp5-Sep-06 0:05 
AnswerRe: IPCChannel connection problem Pin
Gomac5-Sep-06 5:41
Gomac5-Sep-06 5:41 
QuestionHow to read values in GirdVew Pin
Murtuza Husain Miyan Patel4-Sep-06 21:34
professionalMurtuza Husain Miyan Patel4-Sep-06 21:34 
AnswerRe: How to read values in GirdVew Pin
WillemM5-Sep-06 2:06
WillemM5-Sep-06 2:06 
QuestionWindows Application (SetROP2 function in c#) Pin
rohita4-Sep-06 21:22
rohita4-Sep-06 21:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.