Reputation: 131
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
Upvotes: 7
Views: 24671
Reputation: 1
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.
Upvotes: 0
Reputation: 131
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.
<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
Upvotes: 6
Reputation: 20067
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.
Upvotes: 11
Reputation: 2856
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/
Upvotes: 1