3

I have a Web App in Azure and I configured the connectionStrgin in its application setting, but I do not know how I can set this configuration as an environment variable in the application web.config (.Net).

Someone has some document or knows how to make this possible, so far I have reviewed the documentation of Microsoft and other sites for troubleshooting, but I have not been able to find a solution

Nicolas Herrera
  • 81
  • 1
  • 1
  • 6
  • Hi, why do you need it as environment variable? I gather Azure is able to handle the connection strings section. – Stefan Dec 26 '18 at 19:42

4 Answers4

6

Connection strings

For .NET apps like ASP.NET, these connection strings are injected into your .NET configuration connectionStrings settings at runtime, overriding existing entries where the key equals the linked database name.

These settings will also be available as environment variables at runtime, prefixed with the connection type. The environment variable prefixes are as follows:

SQL Server: SQLCONNSTR_
MySQL: MYSQLCONNSTR_
SQL Database: SQLAZURECONNSTR_
Custom: CUSTOMCONNSTR_

You retrieve the settings in your application using ConfigurationManager.ConnectionStrings["keyname"];.

then in my web.config I should have the connectionString blank and will automatically use the connectionString of the application settings?

Yes, it is. When you both have connectionstring in azure application Setting and web.config, the azure settings will override web.config. You could set key/value in web.config when you test in local.

Joey Cai
  • 18,968
  • 1
  • 20
  • 30
  • Thanks for your comments, I did some tests and the answers are correct, the configuration of the application injects the connection string in my application, but I can not connect to my database, but I think this problem is because I am using a framework something strange, Gale – Nicolas Herrera Dec 27 '18 at 18:26
  • To be honest I don't see how this can work when you don't want to commit the web.config file to source control with the connection string, but still need those to debug your app locally. If you do `ConfigurationManager.ConnectionStrings["keyname"];` and then leave the `connectionStrings` blank in the web.config file, this will never be debugged locally. – Zizzipupp Nov 19 '19 at 11:57
5

Thank you all for your help, I managed to give a complete solution to my problem. Effectively in .Net the connection chain is automatically replaced, but in my case the connection continued to present problems. To solve this, configure the database as "Custom" and remove the value of the connection string from the code of my web.config.

enter image description here

<connectionStrings>
<add name="hidhiddenname1"
     connectionString=""
     providerName="Gale.Db.Factories.SQLServerFactory"/>
<add name="hidhiddenname2"
     connectionString=""
     providerName="Gale.Db.Factories.SQLServerFactory"/>
</connectionStrings>

I hope this can help other people :D

Joey Cai
  • 18,968
  • 1
  • 20
  • 30
Nicolas Herrera
  • 81
  • 1
  • 1
  • 6
  • Glad to see that you solve problem. But you could also mark my reply as answer. – Joey Cai Jan 02 '19 at 05:42
  • You can commit this to source control because you are not exposing anything. However, this will not work when you try to debug your app locally because your connection string isn't explicit. – Zizzipupp Nov 19 '19 at 11:59
1

When you a key/value pair in an applications app settings, the key/values will be injected into your configuration at runtime. Likewise, for your connection strings they are injected into your configuration at runtime. These settings will overwrite any settings from Web.Config at runtime. If the settings are missing in Azure your program will look in web.config. You retrive the settings in your application using ConfigurationManager.AppSettings["keyname"];

Read more at https://learn.microsoft.com/en-us/azure/app-service/web-sites-configure#app-settings and here https://buildazure.com/2015/11/30/azure-web-app-application-settings/

iikkoo
  • 2,810
  • 6
  • 33
  • 38
  • then in my web.config I should have the connectionString blank and will automatically use the connectionString of the application settings? – Nicolas Herrera Dec 26 '18 at 20:56
  • Right @NicolasHerrera. – iikkoo Dec 27 '18 at 11:50
  • Thanks for your comments, I did some tests and the answers are correct, the configuration of the application injects the connection string in my application, but I can not connect to my database, but I think this problem is because I am using a framework something strange, Gale. – Nicolas Herrera Dec 27 '18 at 18:25
0

I had further modified answer provided by Nicolas Herrera,because of security reason's we can't add plan text connection string to App service configuration section and on top of that, i was getting formatting issue for plan text connection string, so end up reading connection string value from key vault inside the configuration section

Connectionstring name = "your ConnectionString Key name"

Value = @Microsoft.KeyVault(SecretUri="your key vault uri")

Type = Custom ( it has to be custom , else it won't work) but again you can try diff type if you want to see what works for you

fyi i am still having connection string value in my config so that i can run my app locally for debugging purpose, connection string value is replaced per env, in App service as explained on microsoft documentation

For ASP.NET and ASP.NET Core developers, setting connection strings in App Service are like setting them in in Web.config, but the values you set in App Service override the ones in Web.config. You can keep development settings (for example, a database file) in Web.config and production secrets (for example, SQL Database credentials) safely in App Service. The same code uses your development settings when you debug locally, and it uses your production secrets when deployed to Azure.