Tuesday, February 06, 2007

Windows Forms Connection String Encryption Challenges

 

Listed here are the issues and frustrations with the encryption/decryption of a simple connection string and the findings.

 

The scenario

Protect a connection string in the App.config file of a Windows Forms app by encrypting it.  The app will use Enterprise Library 2.0 and an internal util library used in both Windows Forms and Web apps and deployed via ClickOnce.  Simple enough?  Right?  Wrong!

 

Challenge #1 – Encrypting the connection string

The first thing I did was to encrypt directly the connection string.  Although you have everything you need to encrypt/decrypt right in the .NET Framework, Microsoft didn’t made this operation simple for those who never touched encryption before.  You quickly learn that some forms of encryption are tied to the machine where the encryption is taking place.  Since I wanted to encrypt, on one machine and distribute the result to multiple clients, I had to select a non-random and non-machine specific method.  A quick search lead me to the Obviex site where I found some well documented code:

http://www.obviex.com/samples/Encryption.aspx

 

I quickly incorporated a version of the above code and tested my app.

 

Challenge #2 – Enterprise Library 2.0

A quickly found out that Enterprise Library does not like connection strings that are encrypted.  One solution would be to change Enterprise Library source code a little bit but that’s one thing I try to avoid for compatibility reasons.  Back to square one.

 

Challenge #3 – Encrypting configuration sections

One cool new thing in the .NET Framework 2.0 is the ability to encrypt a whole section in the configuration file.  However, I quickly found out two things:

  1. Almost all the info on the Web and on the documentation explain the process for Web apps
  2. The encryption is machine dependant

 It’s very easy to encrypt a section in the configuration file, here’s the code:

 

public static void EncryptConfigSection(string sectionName)

{

    Configuration config = ConfigurationManager.OpenExeConfiguration(onfigurationUserLevel.None);

    ConfigurationSection section = config.GetSection(sectionName);

    if (section != null)

    {

        if (!section.IsReadOnly())

        {

    section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");

             section.SectionInformation.ForceSave = true;

             config.Save(ConfigurationSaveMode.Full);

         }

     }

     ConfigurationManager.RefreshSection(sectionName);

 }

 

What cool is that you don’t have to code anything special to read back the encrypted data.  The Configuration Manager figures out that the stuff inside the section is encrypted and decrypts it automatically.  Great, but this means that the config file must be deployed non encrypted.  In a Windows Forms scenario, that’s bad.  So what can you do?

 

Challenge #4 – When to encrypt?

So the encryption must be done locally.  How do you do that and of course, when?  You can do it the first time that the app will start. However, it leaves you with a small window of time when the information is unprotected.  You can let the setup program do it.  Hameer Saleem wrote a great article on The Code Project Website explaining how to do that:

http://www.codeproject.com/csharp/ProtectedConfigWinApps.asp

 

The drawback is that you must use an installer to deploy you app.  What if you use ClickOnce?

 

Challenge #5 – ClickOnce, secure?

The files needed to deploy a Windows Forms app with ClickOnce are usually located on a Web server.  What if someone could access the config file directly from the Web server?  He could have access to our non-encrypted connection string, right?  Right!

We found a way to access the config file directly from the Web server without installing the application.  Doh!

 

Challenge #6 - When to encrypt? (part 2)

So, the connection string must be protected before being deployed because of the possibility of a hack but it must be in a way that Enterprise Library can understand.

 

The solution is then:

  1. Encrypt the connection string using a non-random and non-machine specific method.
  2. Deploy using ClickOnce
  3. On the app first run
    1. Encrypt the connection string section
    2. Decrypt the connection string
    3. Save the connection string back to the encrypted section

 

Challenge #7 – Bugs?

Our solution works except for something really unexpected…a bug in the .NET Framework!

 

The standard way to access a connection string in the connectionStrings section of the config file is by using the static GetSection method of the ConfigurationManager object.  The beauty of this method is that it will work in Windows and Web apps.  It work beautifully when the connectionStrings section is not encrypted but as soon as it is, the code throws an error when run in a Windows Forms app however, it works in a Web app!

 

ConnectionStringsSection configurationSection = ConfigurationManager.GetSection("connectionStrings") as ConnectionStringsSection;   

string conStringValue = configurationSection.ConnectionStrings[conStringKey].ToString();

 

Worse, Enterprise Library 2.0 is using the bugged static method!

 

The bug is documented in this thread:

http://www.developersdex.com/asp/message.asp?p=2911&r=5205960&Page=1

 

From: Steven Cheng[MSFT]
Date Posted: 8/17/2006 8:08:00 AM

Thanks for your reply Rayn,

So you got the same test results as mine (test code 1 through error while
test code 2 works),  correct?

Yes, I agree that the problem is not specific to ConfigurationManager, it
is the GetSection function and its callee (from call stack it is a
recursive fuction which throw the error).

Sure, the ConfigurationManager.ConnectionStrings static member also wrapper
the "GetSection" method. However, the problem here is that the test result
here indicate that the exception somewhat depend on the approach we get the
Section.

Anyway, I'll perform some further test through custom section since that
won't rely on any built-in static properties. I'll update you my test
results.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


 

 

From: Steven Cheng[MSFT]
Date Posted: 8/25/2006 5:44:00 AM

I did have checked our internal database and this issue has been recorded
so far.

However, I think it'll be hard to address it in a short time. If you would
like to get a hotfix, you can contact CSS product support for assistance.


 

So we can request a hotfix but deploying it to hundred of workstations is really an issue and we don’t want to get there.

 

Challenge #8 – An alternative to the bugged method?

So the static method is bugged.  Is there an alternative?  Of course, there is.  A quick search resulted in finding the OpenExeConfiguration method of the ConfigurationManager and this one worked on encrypted sections.

 

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

string  = config.ConnectionStrings.ConnectionStrings[conStringKey].ToString();

 

So it’s just a matter of changing one line of code in Enterprise Library, right? Wrong!

 

Challenge #9 – Will it work in Web apps?

Sure it works in Windows Forms apps but three letters in the method name looks suspicious: EXE.  A quick try in a Web app confirmed the suspicion.  It is Windows Forms specific and yes, there’s something similar in the System.Web.Configuration namespace but it will work with Web apps only!

 

Final challenge

So we need to keep Enterprise Library code as generic as it is but make it work with encrypted and non-encrypted connection strings in both Windows Forms and Web apps.  Is there a way to force some connection string in Enterprise Library so we could use Windows Forms or Web specific code to retrieve it?  Yes but not with the generic database creation method of Enterprise Library:

 

Database db = DatabaseFactory.CreateDatabase(conStringKey);

 

Instead, we can use this code that takes a connection string as an argument instead of the key name but it’s less generic:

 

Database db = new SqlDatabase(conString);

 

Success!!!

 

Here’s the final code to retrieve the connection string.  Note that you have to specify if the app is a Windows Forms app of a Web app.  We did it by passing a hardcoded value (applicationType) but I guess you can found out at runtime if you're running in Win or Web.

 

public enum ApplicationType

{

   Web, Windows

}

 

public string GetConnectionStringFromKey(string conStringKey ApplicationType applicationType)

{

    string ret = string.Empty;

 

    switch (applicationType)

    {

        case ApplicationType.Windows:

            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            ret = config.ConnectionStrings.ConnectionStrings[conStringKey].ToString();

            break;

        case ApplicationType.Web:

            ConnectionStringsSection configurationSection = ConfigurationManager.GetSection("connectionStrings") as ConnectionStringsSection;   

            ret = configurationSection.ConnectionStrings[conStringKey].ToString();

            break;

    }

 

    return ret;

       

}

 

You can then call the specific Enterprise Library database objects and pass the connection string:

 

public enum DatabaseType

{

   SQLServer, Oracle

}

 

Database db = null;

 

switch (databaseType)

{

     case DatabaseType.SQLServer:

          db = new SqlDatabase(connectString);

          break;

     case DatabaseType.Oracle:

          db = new OracleDatabase(connectString);

          break;

}

Tuesday, February 06, 2007 8:11:14 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0]  |  Tracked by:
http://www.google.com/search?q=btaodvcr [Pingback]
http://9np-information.info/61231413/index.html [Pingback]
http://9nn-information.info/71777355/information-on-behavior-management.html [Pingback]
http://9nn-information.info/46133630/peoria-unified-school-district-homepage.htm... [Pingback]
http://9ne-information.info/51330072/real-estate-sales-ma.html [Pingback]
http://9nn-information.info/11297374/index.html [Pingback]
http://9no-information.info/46063300/load-flow-electric-power-system.html [Pingback]
http://9na-information.info/64504328/index.html [Pingback]
http://9nn-information.info/89067185/apa-research-paper-table-of-contents.html [Pingback]
http://9nc-information.info/09949478/index.html [Pingback]
http://9na-information.info/73733201/index.html [Pingback]
http://9qh-information.info/30203111/annunci-lavoro-treviso.html [Pingback]
http://9os-information.info/93128492/index.html [Pingback]
http://9oo-information.info/66598730/37th-chess-olympiad.html [Pingback]
http://9qm-information.info/53650529/wave-gothic-treffen.html [Pingback]
http://9qr-information.info/20536806/culto-mitriaci.html [Pingback]
http://9ok-information.info/10685589/index.html [Pingback]
http://9on-information.info/40654246/ford-explorer-service-light.html [Pingback]
http://9qo-information.info/03653708/index.html [Pingback]
http://9rw-information.info/92542908/index.html [Pingback]
http://9sr-information.info/67440581/download-firmware-qsi-dvdrw-sbw-081.html [Pingback]
http://9re-information.info/69643786/wedding-dresses-with-hand-cut-lace.html [Pingback]
http://9re-information.info/77692574/index.html [Pingback]
http://9sp-information.info/98863056/index.html [Pingback]
http://9rl-information.info/79505283/index.html [Pingback]
http://9rb-information.info/78336125/index.html [Pingback]
http://9sm-information.info/32966599/regret-malice-mizer.html [Pingback]
http://9uaeb-le-informazioni.info/49569965/index.html [Pingback]
http://9uafg-le-informazioni.info/09294318/sfondo-gallardo.html [Pingback]
http://9uafs-le-informazioni.info/66575460/index.html [Pingback]
http://9uaer-le-informazioni.info/50296807/lampadine-basso-consumo.html [Pingback]
http://9uaet-le-informazioni.info/22513757/index.html [Pingback]
http://9uafe-le-informazioni.info/11007531/index.html [Pingback]
http://9uaet-le-informazioni.info/35908628/foto-gratis-ragazza-autoreggente.html [Pingback]
http://9uafm-le-informazioni.info/27015408/index.html [Pingback]
http://9uaes-le-informazioni.info/14086408/index.html [Pingback]
http://9uaef-le-informazioni.info/51096099/index.html [Pingback]
http://9uago-le-informazioni.info/27034830/safir-hotel.html [Pingback]
http://9uagb-le-informazioni.info/50394501/telecamera-digitale-scheda.html [Pingback]
http://9uahj-le-informazioni.info/06692319/baltazar-aquino.html [Pingback]
http://9uahr-le-informazioni.info/02731845/index.html [Pingback]
http://9uahd-le-informazioni.info/40376869/index.html [Pingback]
http://9uagg-le-informazioni.info/11042861/index.html [Pingback]
http://9uahn-le-informazioni.info/77825910/index.html [Pingback]
http://9uagg-le-informazioni.info/38663304/index.html [Pingback]
http://kevruublog.tripod.com/21.html [Pingback]
http://kevruublog.tripod.com/133.html [Pingback]
http://awlelm.org/sitemap28.html [Pingback]
http://pinofranc.homestead.com/05/medicalassistant.html [Pingback]
http://pinofranc.homestead.com/00/indiana-university-northwest.html [Pingback]
http://talpeenews.tripod.com/171.html [Pingback]
http://maoguunews.netfirms.com/6.html [Pingback]
http://pasbeenews.tripod.com/166.html [Pingback]
http://njq8l-hhh.com/top-rape-clips.html [Pingback]
http://jjyfo-eee.com/boogie-woogie-bugle-boy.html [Pingback]
http://unibetkom.150m.com/00804-blog.html [Pingback]
http://ramambo.nl.eu.org/01/the-bachelor.html [Pingback]
http://ramambo.nl.eu.org/germain-ampitheater.html [Pingback]
http://ramambo.nl.eu.org/pet-gates.html [Pingback]
http://zx7vqtq.biz/vibezone-com.html [Pingback]
http://donakom.nl.eu.org/adult-movie-trailer.html [Pingback]
http://verotokom.nl.eu.org/www-office-depot-com.html [Pingback]
http://cyisevw.com/comcast-dsl.html [Pingback]
http://ple--blog.nl.eu.org/bank-of-america-checking.html [Pingback]
http://nasferablog.netfirms.com/397.html [Pingback]
http://lk2iuen.biz/wife-cumshots.html [Pingback]
http://nasferablog.netfirms.com/532.html [Pingback]
http://www.nonedotweb.org/st82.html [Pingback]
http://www.nonedotweb.org/st07.html [Pingback]
http://9ujvr-le-informazioni.cn/61079267/index.html [Pingback]
http://9ujsk-le-informazioni.cn/27551003/index.html [Pingback]
http://msve--lono.nl.eu.org/fayette-county-board-of-education.html [Pingback]
http://9ukft-le-informazioni.cn/95175865/index.html [Pingback]
http://9ujvi-le-informazioni.cn/29448339/index.html [Pingback]
http://9ujod-le-informazioni.cn/12421303/index.html [Pingback]
http://9ukcc-le-informazioni.cn/09181952/presepe-giuseppe-aprea.html [Pingback]
http://9ujsd-le-informazioni.cn/33803983/index.html [Pingback]
http://9ujzx-le-informazioni.cn/18465529/index.html [Pingback]
http://9ujnl-le-informazioni.cn/32063158/agevolazione-acquisto-pc-dipendenti-pub... [Pingback]
http://9ujyx-le-informazioni.cn/29834402/index.html [Pingback]
http://9ujza-le-informazioni.cn/03067427/index.html [Pingback]
http://9ujxs-le-informazioni.cn/25820631/index.html [Pingback]
http://9ukil-le-informazioni.cn/23512335/mondo-nuovo-ferdy-colloca.html [Pingback]
http://9ujyw-le-informazioni.cn/61456159/olimpiadi-matematica-biennio.html [Pingback]
http://9ujrf-le-informazioni.cn/75268604/index.html [Pingback]
http://9ujvf-le-informazioni.cn/16185131/mensile-marzo-2006.html [Pingback]
http://9ukik-le-informazioni.cn/63656442/index.html [Pingback]
http://9ujnm-le-informazioni.cn/52058967/index.html [Pingback]
http://9ukgx-le-informazioni.cn/77357527/index.html [Pingback]
http://9ukav-le-informazioni.cn/19314150/index.html [Pingback]
http://9ujsd-le-informazioni.cn/24296828/index.html [Pingback]
http://9ujse-le-informazioni.cn/16373574/index.html [Pingback]
http://9ujvw-le-informazioni.cn/80753685/centauro-net.html [Pingback]
http://9ujwt-le-informazioni.cn/73160680/mrx4-mini-quake.html [Pingback]
http://9ujvm-le-informazioni.cn/77186596/index.html [Pingback]
http://9ujmq-le-informazioni.cn/70364693/abito-sera-firmati-firenze.html [Pingback]
http://9ujnv-le-informazioni.cn/49736391/index.html [Pingback]
http://9ujug-le-informazioni.cn/75669681/index.html [Pingback]
http://nasferablog.netfirms.com/50.html [Pingback]
http://mromaner.tripod.com/3.html [Pingback]
http://zf1y1fs.biz/pennsylvanialottory.html [Pingback]
http://wwad6lf.biz/interpriserentalcar.html [Pingback]
http://9ucos-le-informazioni.biz/84305129/index.html [Pingback]
http://9ucop-le-informazioni.biz/76534638/index.html [Pingback]
http://9ucoh-le-informazioni.biz/19762133/sorellina-clitoride.html [Pingback]
http://9ucoo-le-informazioni.biz/91071762/comfortable-it.html [Pingback]
http://9ucoi-le-informazioni.biz/53274533/index.html [Pingback]
http://9ucot-le-informazioni.biz/32279063/index.html [Pingback]
http://olnt9pb.com/olsen-twins-uncensored.html [Pingback]
http://derfoblog.ifrance.com/sitemap3.html [Pingback]
http://galetgah.homestead.com/82.html [Pingback]
http://smapper12.ifrance.com/50.html [Pingback]
http://mernokob.nl.eu.org/gagged-bondage.html [Pingback]
http://petmeds.hooyack.com/588.html [Pingback]
http://petmeds.hooyack.com/91.html [Pingback]
http://kubaluin.ifrance.com/566.html [Pingback]
http://mazzoliks.ifrance.com/290.html [Pingback]
http://halloweenus.net/551.html [Pingback]
http://halloweenus.net/654.html [Pingback]
http://callingcard.usalegaldirect.org/37.html [Pingback]
http://odalteg1.ifrance.com/253.html [Pingback]
http://vuter.homestead.com/00/medical-coding-books.html [Pingback]
http://duter.homestead.com/00/firstpremierbank.html [Pingback]
http://2909071.ifrance.com/260.html [Pingback]
http://2909072.ifrance.com/6.html [Pingback]
http://0210071.ifrance.com/119.html [Pingback]
http://0210071.ifrance.com/120.html [Pingback]
http://03100711.ifrance.com/20.html [Pingback]
http://03100711.ifrance.com/21.html [Pingback]
http://bumbarin.tripod.com/126.html [Pingback]
http://bumbarin.tripod.com/127.html [Pingback]
http://fasxen.netfirms.com/23.html [Pingback]
http://maribuli.tripod.com/125.html [Pingback]
http://maribuli.tripod.com/126.html [Pingback]

All comments require the approval of the site owner before being displayed.
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

Live Comment Preview

Theme design by Jelle Druyts