Every .NET developer might have used the following tags available in the Web.Config file while developing Web Application using the ASP.NET Framework.
AppSettings: These are used to store some Configurable items. Below are few of the examples.
- SMTPServer Details – Create Configurable values for SMTP Server, Username and Password. In production, you might store encrypted passwords even in Web.Config.
- Path – Path of some template files or log files that could change in different environments
You could use AppSettings key for storing anything that you would like to configure later without making any changes to the Code.
ConnectionStrings: These are used for Storing the Complete ConnectionStrings. For example: The ConnectionString of SQL Server.
Both the above tags and related attributes are very helpful for developers, architects and IT Administrators for configuring different values for different Environments.
However, one of the disadvantages of these files is that as an IT Administrator or Architect, you would need to understand the Syntax of these tags along with their attributes. You might have not have the required tools to avail the advantages like Intellisense in the Staging or Production Environments. As a result, you need to depend on your developers to make changes to the configuration files each time you would like to deploy on a new environment.
Sometimes, you might not like to reveal your credentials to developers. In this article, we will learn how to overcome this issue using the AppSettings and ConnectionStrings settings of the Application Settings blade of the Azure App Service.
Below is an example of how to use AppSettings in an MVC Application.
- Declare an App Setting in Config File
<add key=”SMTPServerName” value=”MySMTPServerfromWeb.Config”/>
- Use the App Setting variable in your Action Method – Please note that for simplicity
|public string Index()
ViewBag.Title = “Azure Application Settings – Example”;
return “This is the Configurable Value in Web.Config for my SMTP Server: ” + ConfigurationManager.AppSettings[“SMTPServerName”];
Below is the output of the view.
Let’s deploy the application to an Azure App Service using the steps explained in the following article.
Once you deploy the application, please navigate to the Application Settings blade by clicking on the Application Settings a shown below.
Clicking on the Application Settings will open the Application Settings blade. Please scroll down a bit to view the AppSettings and the ConnectionString Settings as shown below.
This section allows you to configure a different App Setting value than that of the value specified in the Web.Config.
Let’s give some other value to the “SMTPServerName” key as shown below.
Just to refresh, below are the values that we have provided for SMTPServerName key.
- Config: MySMTPServerfromWeb.Config
- Azure App Service – Application Settings: MySMTPServerfromAzureAppSettings
Once you are done, please click on Save button as shown below.
Azure Management portal will save the changes in just a few seconds. After the changes are saved, let’s navigate to the App Service URL by clicking on the link shown below.
Below is the output of the Azure App Service.
Please note that it has pulled the value from the App Settings configured in the Azure App Service’s – Application Settings blade but not from the Web.config file. This way, you can change these configurable values whenever you like to change without depending on developers. The same rule applies even for Connection String.
In this article, we have learnt how the AppSettings and ConnectionString settings work in the Application Settings blade.
Hope you enjoyed reading the article. Your feedback is appreciated.
Do you like this article? If you want to get more updates about these kind of articles, you can join my Learning Groups
When Azure stores the Application Settings connection string, does it encrypts and store it? where does it store this information? Any code (for example, code with less privileges also) can read this connection and display the sensitive information like connection credentials?
Any idea how internally it works?? How app setting override the value written in web.config.
can you please help me in writing the azure database servername in appsettings,i have already tried but getting
Error|Logon failure: unknown user name or bad password. Domain: D30