Solved

How to encrypt connection string?

Posted on 2009-05-05
21
1,294 Views
Last Modified: 2012-05-06
I have a .net 3.5 winforms app and want to encrypt the connectionString.  I have renamed app.config to web.config so it can be used with aspnet_regiis.  I'm in the app root folder and running this command line:

C:\src\project1\myapp>aspnet_regiis -pef "connectionSrings" -app "myapp"

No matter how I change the command line, I always get the help output.  No errors.  The app doesn't have an IIS website and is a web application project.  Any suggestions?
0
Comment
Question by:brettr
  • 11
  • 9
21 Comments
 
LVL 18

Expert Comment

by:David Robitaille
Comment Utility
try to put the local path of the website instead.
aspnet_regiis.exe -pef "connectionStrings" "C:\Inetpub\wwwroot\MySite"
 
here a ref:
http://www.4guysfromrolla.com/articles/021506-1.aspx
0
 
LVL 18

Expert Comment

by:David Robitaille
Comment Utility
<$/>you could also try to move into the %WINDOWSDIR%\Microsoft.Net\Framework\version directory before running the command.
0
 
LVL 14

Assisted Solution

by:shahprabal
shahprabal earned 200 total points
Comment Utility
0
 

Author Comment

by:brettr
Comment Utility
Last suggestion worked.  It output a larger amount of help at first until I got the command line correct.  I'm guessing it's a newer version of aspnet_iisreg.exe.

This section created by the encryption is giving errors:

  <connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider">
    <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"

//Error 1
The 'configProtectionProvider' attribute is not declared

//Error 2
The element 'connectionStrings' has invalid child element 'EncryptedData' in namespace 'http://www.w3.org/2001/04/xmlenc#'. List of possible elements expected: 'add, remove, clear'.

Do you know about those?  App seems to run ok.
0
 
LVL 18

Expert Comment

by:David Robitaille
Comment Utility
try to add this at the end of the command, it will uses DPAPI to encrypt and decrypt data instead of RSA
 prov "DataProtectionConfigurationProvider"

(you may have to restore your backup before doing it)
0
 
LVL 18

Expert Comment

by:David Robitaille
Comment Utility
the switch char seem to be replaced, "" = "-"
it`s
-prov "DataProtectionConfigurationProvider"

0
 

Author Comment

by:brettr
Comment Utility
Same problem just that now it looks like this:

 <connectionStrings configProtectionProvider="DataProtectionConfigurationProvider">
    <EncryptedData>
 ...
0
 
LVL 18

Accepted Solution

by:
David Robitaille earned 300 total points
Comment Utility
"This section created by the encryption is giving errors"

What program is giving those errors?

0
 

Author Comment

by:brettr
Comment Utility
Adding this makes the errors go away:

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
0
 

Author Comment

by:brettr
Comment Utility
Unfortunately, that namespace also prevents my app from running.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 18

Expert Comment

by:David Robitaille
Comment Utility
???
What error message did you get?

did you run the aspnet_regiis.exe on the IIS Server. the encryption key is diferent than the one on your developement machine.
0
 

Author Comment

by:brettr
Comment Utility
It complained that the app's configuration was invalid and to reinstall the app.
0
 
LVL 18

Expert Comment

by:David Robitaille
Comment Utility
Who complained? ISS? visual studio?
0
 

Author Comment

by:brettr
Comment Utility
I'm running the app in VS.NET.  I get the same error when double clicking the EXE.  It says the application configuration is incorrect.
0
 
LVL 18

Expert Comment

by:David Robitaille
Comment Utility
???????????
How do you get an EXE from web application? And what are you doing with it?
Anyway this web.config encryption thing is supposed to work when you want to "deploy" a web application to a web server (usually IIS). the machine that will execute the program (the IIS server) must be the same that the one you used to encrypt the Web.config.
There must be something i dont understand about your setup, because i got no idea of what you are trying to do...
0
 

Author Comment

by:brettr
Comment Utility
It is a winform app.  But that doesn't matter.  The point is that VS.NET complains about those particular attributes.

Yes - I found it that the machine which encyrpts the config needs to decrypt.  Not helpful.  How can you deploy this to other machines?
0
 
LVL 18

Expert Comment

by:David Robitaille
Comment Utility
you cannot.
That thing is designed for ASP.NET web site not Winform app. You posted the question in "NET, C# Programming Language, Programming for ASP.NET" zones. dont you noticed the tool name is aspnet_regIIS???
but, now i understand the alusion about the app.config.
I think this link will be more helpfull
http://msdn.microsoft.com/en-us/library/ms998283.aspx
 
0
 

Author Comment

by:brettr
Comment Utility
I posted here because there is no such tool for winforms and using aspnet_regiis is the defacto standard to encrypt app.config files as well.
0
 
LVL 18

Expert Comment

by:David Robitaille
Comment Utility
sorry, i keep the wrong link
http://www.codeproject.com/KB/cs/Configuration_File.aspx
if that cannot hlep, I sorry, but i connot help you futher, i diden`t do anything like this, my experience is of web.config and web app. I suggest you post a new question like
"How to encrypt connection string in app.config"
 
0
 

Author Comment

by:brettr
Comment Utility
Thanks on the links you've been posting.  The programatic technique won't work since it has to encrypt/decrypt with each app run.  I only encrypt on app install.

Your help set me in the right direction.  I leave the app.config unencrypted in SVN.  During the win app MSI is install, it runs a batch file that renames app.config to web.config, encrypts it as above and renames it back to app.config.  viola!  It works fine.
0
 
LVL 18

Expert Comment

by:David Robitaille
Comment Utility
I`m glad i gould help!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now