|
|
#1 |
|
.NUT
Join Date: May 05
Location: Sydney, Australia
Posts: 60,536
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
[.NET 2.0+] Protected Configuration (Encrypting Config Files)
The config file is the preferred location for application configuration data these days. .NET apps will use it themselves to store data in various situations. One such situation that many people may have encountered is when you create a Data Source the wizard offers to store the connection string in the config file. This is so that you can simply edit the config file when the app is installed to customise the connection for that system. One issue here is, what if the connection string contains a password? The config file is just plain text so its visible to the world. The answer is encryption and it's already built into the .NET configuration mechanism.
Try running the attached project and then opening the config file from the bin\Debug folder in VS. Notice that there's a <connectionStrings> section and it contains an item named PrimaryConnectionString. Now, select Tools -> Options from the menu and see that the components of that connection string are displayed as individual fields. Try editing those fields and then clicking OK. You don't actually have to have SQL Server installed to do this but if you want to use the drop-down lists to select a server and database you will need it. Go back to VS and you should be prompted to reload the config file. Do so and note that the values you entered now appear in the stored connection string. Next, select Tools -> Encrypt Connection Strings from the menu. Go back to VS and reload the config file again and observe the changes. Note that your actual connection string is nowhere to be seen. Now, if you select Tools -> Options again at this point an exception will be thrown. I'm not sure exactly why and i'm not sure how to fix it, but that isn't really a problem anyway. This error only occurs when you start the app with the <connectionStrings> section unencrypted, then encrypt it, then try to read a connection string via My.Settings. This should never occur so the error should never appear. So, close the app and then run it again. Select Tools -> Options from the menu and observe that the components of the connection string are displayed again, even though they cannot be seen in the config file. There was no extra code needed to accomplish this. The user code is exactly the same to read the connection string whether the config file is encrypted or not. the decryption is handled on-the-fly by the Framework. Now, edit the connection properties again and click OK. Go back to VS and reload the config file again and observe that the long strings of gibberish have changed. Those strings contain your data in encrypted format. Now, select Tools -> Decrypt Connection Strings from the menu and go back to VS and reload the config file again. Note that your connection string is now visible again and the changes you made are reflected. Go back to the app and select Tools -> Options again and observer that, using the same code again, the connection properties have been read from the unencrypted config file. Tada! N.B. - I've removed the original attachment, which was a VS 2008 project, and attached a VS 2005 version in its place.
__________________
![]() 2007, 2008, 2009, 2010 Why is my data not saved to my database? | Communicating between multiple forms | MSDN Data Walkthroughs MSDN "How Do I?" Videos: VB | C# VBForums Database Development FAQ My CodeBank Submissions: VB | C# (ForumAccount has translated some of my VB submissions to C#) My Blog: Defining and Raising Custom Events | Manipulating GDI+ Drawings | Using Parameters in ADO.NET Last edited by jmcilhinney; Oct 26th, 2008 at 05:50 AM. |
|
|
|
|
|
#2 |
|
.NUT
Join Date: May 05
Location: Sydney, Australia
Posts: 60,536
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Re: [.NET 2.0+] Protected Configuration (Encrypting Config Files)
To continue, that all demonstrates encrypting the <connectionStrings> section. You can encrypt other sections too. In the code for the OptionsDialogue in the project find the Load event handler, which looks like this:
vb.net Code:
vb.net Code:
vb.net Code:
vb.net Code:
Note that in this case you can still read the config file successfully even if encryption was applied after opening the app. Like I said though, this situation should never arise in the real world. So, speaking of the real world, how would you use this? Well, normally if you want to encrypt the config file then you'd want it encrypted all the time. As such you would use a Setup project to install your app and then you'd include the encryption code in a Custom Action, so the config file is encrypted when the app's installed. because you don't need to make any code changes to read and write an encrypted config file, you can read and write the unencrypted config file while you're developing and then read and write the encrypted config file when the app's deployed without having to care which is which. Note that what I've shown is for WinForms apps. ASP.NET apps are very similar except that you don't perform the encryption and decryption in code. You encrypt a web.config file using the aspnet_regiis utility at the commandline. That's basically because web apps get installed once only, and it's not going to be the end user who installs it. How To: Encrypt Configuration Sections in ASP.NET 2.0 Using DPAPI How To: Encrypt Configuration Sections in ASP.NET 2.0 Using RSA Please play with the code and feel free to ask questions BUT please make sure you have read the code THOROUGHLY first.
__________________
![]() 2007, 2008, 2009, 2010 Why is my data not saved to my database? | Communicating between multiple forms | MSDN Data Walkthroughs MSDN "How Do I?" Videos: VB | C# VBForums Database Development FAQ My CodeBank Submissions: VB | C# (ForumAccount has translated some of my VB submissions to C#) My Blog: Defining and Raising Custom Events | Manipulating GDI+ Drawings | Using Parameters in ADO.NET Last edited by jmcilhinney; Jul 22nd, 2008 at 10:52 PM. |
|
|
|
|
|
#3 |
|
Fanatic Member
Join Date: Jul 07
Posts: 512
![]() |
Re: [.NET 2.0+] Protected Configuration (Encrypting Config Files)
great! i always hope the best from jm
|
|
|
|
|
|
#4 |
|
Fanatic Member
Join Date: Jul 07
Posts: 512
![]() |
Re: [.NET 2.0+] Protected Configuration (Encrypting Config Files)
hcan you please provide, is there available version for 2005? im receiving this error "the selected file is a solution file, but was created by newer version of this application or cannot be opened."
tnx |
|
|
|
|
|
#5 | |
|
.NUT
Join Date: May 05
Location: Sydney, Australia
Posts: 60,536
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Re: [.NET 2.0+] Protected Configuration (Encrypting Config Files)
Quote:
__________________
![]() 2007, 2008, 2009, 2010 Why is my data not saved to my database? | Communicating between multiple forms | MSDN Data Walkthroughs MSDN "How Do I?" Videos: VB | C# VBForums Database Development FAQ My CodeBank Submissions: VB | C# (ForumAccount has translated some of my VB submissions to C#) My Blog: Defining and Raising Custom Events | Manipulating GDI+ Drawings | Using Parameters in ADO.NET |
|
|
|
|
|
|
#6 |
|
Fanatic Member
Join Date: Jul 07
Posts: 512
![]() |
Re: [.NET 2.0+] Protected Configuration (Encrypting Config Files)
thnx.
i tried to work around by chance to remove the blue squirky lines particular in the Dim config As System.Configuration.Configuration what i did is import configuration related namespace to make sure i wont miss a thing vb Code:
but still i received the same error. |
|
|
|
|
|
#7 |
|
.NUT
Join Date: May 05
Location: Sydney, Australia
Posts: 60,536
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Re: [.NET 2.0+] Protected Configuration (Encrypting Config Files)
There's no use importing the namespace a class is a member of if you haven't referenced the assembly it's declared in.
__________________
![]() 2007, 2008, 2009, 2010 Why is my data not saved to my database? | Communicating between multiple forms | MSDN Data Walkthroughs MSDN "How Do I?" Videos: VB | C# VBForums Database Development FAQ My CodeBank Submissions: VB | C# (ForumAccount has translated some of my VB submissions to C#) My Blog: Defining and Raising Custom Events | Manipulating GDI+ Drawings | Using Parameters in ADO.NET |
|
|
|
|
|
#8 |
|
Fanatic Member
Join Date: Jul 07
Posts: 512
![]() |
Re: [.NET 2.0+] Protected Configuration (Encrypting Config Files)
everything's clear except this line
vb Code:
i dont understand the line and its produce an error. i change the argument to Code:
Dim builder As New SqlConnectionStringBuilder("Data Source=ConnectionStringsServer;Initial Catalog=ConnectionStringsDatabase;User ID=ConnectionStringsUserName;Password=ConnectionStringsPassword")
![]() the program run but i was not able to encrypt/decrypt app.config Last edited by jlbantang; Jul 23rd, 2008 at 11:58 PM. |
|
|
|
|
|
#9 |
|
.NUT
Join Date: May 05
Location: Sydney, Australia
Posts: 60,536
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Re: [.NET 2.0+] Protected Configuration (Encrypting Config Files)
It's not clear what you mean when you say it's not clear. Do you mean you don't understand it or that it's not working?
__________________
![]() 2007, 2008, 2009, 2010 Why is my data not saved to my database? | Communicating between multiple forms | MSDN Data Walkthroughs MSDN "How Do I?" Videos: VB | C# VBForums Database Development FAQ My CodeBank Submissions: VB | C# (ForumAccount has translated some of my VB submissions to C#) My Blog: Defining and Raising Custom Events | Manipulating GDI+ Drawings | Using Parameters in ADO.NET |
|
|
|
|
|
#10 |
|
Fanatic Member
Join Date: Jul 07
Posts: 512
![]() |
Re: [.NET 2.0+] Protected Configuration (Encrypting Config Files)
hi jm. the prog runs but the app.config were not encrypted/decrypted. your code which i replace might have something to do w/ this.
your code: vb Code:
what was this line for anyway? replaced with this: vb Code:
|
|
|
|
|
|
#11 | |
|
.NUT
Join Date: May 05
Location: Sydney, Australia
Posts: 60,536
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Re: [.NET 2.0+] Protected Configuration (Encrypting Config Files)
Quote:
__________________
![]() 2007, 2008, 2009, 2010 Why is my data not saved to my database? | Communicating between multiple forms | MSDN Data Walkthroughs MSDN "How Do I?" Videos: VB | C# VBForums Database Development FAQ My CodeBank Submissions: VB | C# (ForumAccount has translated some of my VB submissions to C#) My Blog: Defining and Raising Custom Events | Manipulating GDI+ Drawings | Using Parameters in ADO.NET |
|
|
|
|
|
|
#12 | |
|
Lively Member
Join Date: Aug 07
Posts: 88
![]() |
Re: [.NET 2.0+] Protected Configuration (Encrypting Config Files)
Quote:
|
|
|
|
|
|
|
#13 |
|
.NUT
Join Date: May 05
Location: Sydney, Australia
Posts: 60,536
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Re: [.NET 2.0+] Protected Configuration (Encrypting Config Files)
I've removed the original attachment and attached a VS 2005 version of the project in its place.
__________________
![]() 2007, 2008, 2009, 2010 Why is my data not saved to my database? | Communicating between multiple forms | MSDN Data Walkthroughs MSDN "How Do I?" Videos: VB | C# VBForums Database Development FAQ My CodeBank Submissions: VB | C# (ForumAccount has translated some of my VB submissions to C#) My Blog: Defining and Raising Custom Events | Manipulating GDI+ Drawings | Using Parameters in ADO.NET |
|
|
|
|
|
#14 |
|
Lively Member
Join Date: Aug 07
Posts: 88
![]() |
Thanks jmc. This works great… and is an excellent example of encrypt/decrypt of and access to the config file.
Thanks very much. |
|
|
|
|
|
#15 | |
|
Fanatic Member
Join Date: Jun 08
Posts: 515
![]() |
Re: [.NET 2.0+] Protected Configuration (Encrypting Config Files)
Quote:
After one adds the connection string to the Settings tab of your project properties and the encryption takes place though your solution... If I want to use your data access examples to pull data to a form... How would I now open & close connections ?
__________________
using VB.Net 2008 Express on WinXP Check out some very interesting questions I have posed. Thanks VB Forums! Play youtube on a form | FormRestrictor on Multiple Monitors | Using BootStrap Manifest Generator | Add Custom Border to Images using GDI | Splitter Position Memory | Display Powerpoint slides on a form | Internet Connection Indicator |
|
|
|
|
|
|
#16 | |
|
.NUT
Join Date: May 05
Location: Sydney, Australia
Posts: 60,536
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Re: [.NET 2.0+] Protected Configuration (Encrypting Config Files)
Quote:
vb.net Code:
__________________
![]() 2007, 2008, 2009, 2010 Why is my data not saved to my database? | Communicating between multiple forms | MSDN Data Walkthroughs MSDN "How Do I?" Videos: VB | C# VBForums Database Development FAQ My CodeBank Submissions: VB | C# (ForumAccount has translated some of my VB submissions to C#) My Blog: Defining and Raising Custom Events | Manipulating GDI+ Drawings | Using Parameters in ADO.NET |
|
|
|
|
|
|
#17 |
|
Fanatic Member
Join Date: Jun 08
Posts: 515
![]() |
Re: [.NET 2.0+] Protected Configuration (Encrypting Config Files)
That's beautiful - thanks !
__________________
using VB.Net 2008 Express on WinXP Check out some very interesting questions I have posed. Thanks VB Forums! Play youtube on a form | FormRestrictor on Multiple Monitors | Using BootStrap Manifest Generator | Add Custom Border to Images using GDI | Splitter Position Memory | Display Powerpoint slides on a form | Internet Connection Indicator |
|
|
|
|
|
#18 |
|
Junior Member
Join Date: Aug 04
Posts: 30
![]() |
Re: [.NET 2.0+] Protected Configuration (Encrypting Config Files)
can you post also the vb2008 version of this code ! thanks
|
|
|
|
|
|
#19 |
|
.NUT
Join Date: May 05
Location: Sydney, Australia
Posts: 60,536
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Re: [.NET 2.0+] Protected Configuration (Encrypting Config Files)
The code is exactly the same. I only attached a VS 2005 project because VS 2005 users couldn't open a VS 2008 project. The converse is not the case so you can simply open the attached project as you normally would and say yes when VS offers to upgrade it to VS 2008 format.
__________________
![]() 2007, 2008, 2009, 2010 Why is my data not saved to my database? | Communicating between multiple forms | MSDN Data Walkthroughs MSDN "How Do I?" Videos: VB | C# VBForums Database Development FAQ My CodeBank Submissions: VB | C# (ForumAccount has translated some of my VB submissions to C#) My Blog: Defining and Raising Custom Events | Manipulating GDI+ Drawings | Using Parameters in ADO.NET |
|
|
|
|
|
#20 |
|
Lively Member
Join Date: Dec 05
Location: UK
Posts: 127
![]() |
Re: [.NET 2.0+] Protected Configuration (Encrypting Config Files)
many thanks im going to bookmark this and read it in the morning ;0)
|
|
|
|
![]() |
|
||||||
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|