473,883 Members | 2,049 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Saving Encrypted Data to a Configuration File in C#

153 New Member
Ok !

I am reading the book Pro ADO.net.

The author says that the <exename>.confi g files can store the connection string for security purposes.

Later he says that still the connection string is visible to it's better to encrypt it.

He explains that there is a new protectedData section in configuration files specifically for this purpose.

I created a DatabaseExercis e.vshost.exe.co nfig file and added the following code to it

Expand|Select|Wrap|Line Numbers
  1. <configuration>
  2. <connectionStrings>
  3. <EncryptedData/>
  4. </connectionStrings>
  5. <protectedData>
  6. <providers>
  7. <add name="MyProvider" 
  8. type="System.Configuration.RsaProtectedConfigurationProvider
  9. keyContainerName="MyKeys" 
  10. useMachineContainer="true" />
  11. </providers>
  12. <protectedDataSections>
  13. <add name="connectionStrings" 
  14. provider="MyProvider" 
  15. inheritedByChildren="false"/>
  16. </protectedDataSections>
  17. </protectedData>
  18. </configuration> 
And then to store the encrypted connection string in the config file the code to be run in c# is
Expand|Select|Wrap|Line Numbers
  1.             Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  2.             config.ConnectionStrings.ConnectionStrings.Add(
  3.             new ConnectionStringSettings(
  4.             "MyConnectionString",
  5.             "Provider=SQLNCLI.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Test;Data Source=(local)")
  6.             );
  7.             config.Save();
The problem is that after running the code above the file DatabaseExercis e.vshost.exe.co nfig
gets deleted !


The above was done in Visual studios.

And when i make a file DatabaseExercis e.exe.config file and run the DatabaseExercis e.exe file(found in debug folder) it throws an exception

Expand|Select|Wrap|Line Numbers
  1. 'MyKeys' is an unexpected token. Expecting white space. Line 9, position 19. (C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\DatabaseExercise\DatabaseExercise\bin\Debug\DatabaseExercise.vshost.exe.config line 9)

I am fed up by this.

I had also run aspnet_regiis -pc "MyKeys" exp .

Please help !
Sep 7 '08 #1
5 3716
akshaycjoshi
153 New Member
AFter some formatting the error now is

Expand|Select|Wrap|Line Numbers
  1. Unrecognized element 'EncryptedData'. (C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\DatabaseExercise\DatabaseExercise\bin\Debug\DatabaseExercise.vshost.exe.config line 3)
Sep 7 '08 #2
akshaycjoshi
153 New Member
Is this <edited for language> lot of difficult ! ?
Sep 8 '08 #3
Curtis Rutland
3,256 Recognized Expert Specialist
OK, first of all, you posted your question over the weekend. Traditionally the forum is very slow during the weekend, so give it some time.

Secondly, stop writing everything in bold.

Third, use [code] tags instead of [b] tags when you post code.

Fourth, please watch the language on this forum.

Please read the Posting Guidelines.

MODERATOR
Sep 8 '08 #4
balabaster
797 Recognized Expert Contributor
I don't understand what the problem is, it took me 20 seconds to find a link to this tutorial on Google which will point you in the right direction:

http://www.developer.com/net/vb/article.php/3500906
Sep 8 '08 #5
akshaycjoshi
153 New Member
Very sorry if i broke any rules of this forum .

Bala,
I had already seen that link .It solves the same problem but the approach is very different.
I was wondering if I can get the solution of my problem in the code that I had written.
Anyways it was posted on weekend so maybe others can help me.
Still waiting for the solution !

Thanks Bala and insetAlias !
Sep 8 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
2490
by: Ed P. | last post by:
I am trying to save a location to a database using the registry...I've been successful in this, but is there another way I can save this. I have searched on the net and have found that saving the info to an xml file is a good idea. My question is whether or not I can save this in a config file in C# (if so does anyone know how to create and use a file like this). Or can the information be saved somewhere else in the application! Thanks in...
11
2443
by: Tom | last post by:
I am planning on adding a Preferences form to my application and using the Property Grid to display the preferences to the user. What do you think would be the best way to save these preferences out to an XML file? Use the CAB (Configuration Application Block)? Save the preferences in a class object and serialize them out the the XML file? Write them out directly to the XML file? Other ways??? Just want to get some thoughts together......
0
1001
by: Heinz | last post by:
Hi all, is this the correct forum for questions regarding enterprise library (June 2005) ? I hope so.... I'm searching for closer information regarding encryption within configuration application block. I use Configuration application block and encrypted my configuration file. Everything works fine, my application is able to read the configuration and write new values if necessary. But I have several questions, e.g.:
5
6787
by: Michael Sperlle | last post by:
Is it possible? Bestcrypt can supposedly be set up on linux, but it seems to need changes to the kernel before it can be installed, and I have no intention of going through whatever hell that would cause. If I could create a large file that could be encrypted, and maybe add files to it by appending them and putting in some kind of delimiter between files, maybe a homemade version of truecrypt could be constructed. Any idea what it...
6
8130
by: Jeff | last post by:
Hey (and thank you for reading my post) In visual web developer 2005 express edition I've created a simple website project.. At this website I want users who register to be able to upload a picture of themselves to their profile... I admit that I'm a newbie... but this is how I understand this:
0
1895
by: Sid DeLuca | last post by:
I'm modifying the WSE2 HOL sample to develop my web service. I'm able to get the client application's username token encrypted on the way out (outputTrace.webinfo), but not on receipt from the server (inputTrace.webinfo). The HOL manual mentions adding the wse:UsernameToken() to the policyCache.config on the client, but not on the server. I have tried adding this to the <confidentiality> section on the server but it still shows the...
2
1684
by: Mufasa | last post by:
Can anybody give me an opinion on which is more efficient - Saving parameters in a database that is on the same machine as the web site or Reading parameters from a .config file ? It seems like the constant opening/closing of the connection would be a congestion spot - plus it would use lots of memory. But when you have the options in a file - is that slow there?
1
1705
by: WebCM | last post by:
We can do it in different ways. Configuration data are sent by POST. Some of them should be parsed before saving to file. Perhaps, I will use var_export(). However, how to do it best? 1. Without a class / function. Fields names have no prefix and are equal to $cfg array's indexes. <input name="option_name" /> In every file of options form (e.g. main options, content options, e-
5
3604
by: Lucvdv | last post by:
This would better be described by 'serialization' than 'interop', but I didn't find a newsgroup that seems closer on topic. The problem in a few words: I save data with DataSet.WriteXML, but I get different data back when I read it later with DataSet.ReadXml. More detail:
0
9945
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10763
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9586
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7978
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7136
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5807
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4622
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4229
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3241
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.