1

I have a web form developed in ASP.NET and C#. I am storing the connection string to a database in the web.config file like this:

<connectionStrings configSource="MySecrets.config" />

This points to a local file in the same directory as the solution. Debugging locally works, however it is not advisable to commit this file to source control to avoid exposing these secrets.

This article mentions that it is possible to store connection strings on Azure - in the Configurations section of an App Service. The article also says that it's possible to retrieve the connection strings in the code by doing:

dbConn = System.Configuration.ConfigurationManager.AppSettings("myConnStringName")

The article also mentions that "if the application setting(s) happen to already exist in your web.config file, Windows Azure Web Sites will automatically override them at runtime using the values associated with your website. Connection strings work in a similar fashion.

(This assumes that your connection strings are explicit in the web.config file, and if committed to source control, they would be exposed.)

However, in my code, I already have a line with:

dbConn = WebConfigurationManager.ConnectionStrings["myConnStringName"].ConnectionString

Questions:

1) How am I supposed to reconcile these two lines without declaring the same variable (dbConn) twice?

2) How can I not commit MySecrets.config to source control, but at the same time use it when I debug my app locally, while using the connection string stored on Azure when working with the published app?

Zizzipupp
  • 1,301
  • 1
  • 11
  • 27
  • 1
    Have you looked into xml transformations? It allows you to have a developer-only xml file that contains _your_ secrets which can be ignored by version control. – gunr2171 Nov 19 '19 at 15:40

0 Answers0