ASP.NET Configuration and Environment Variables


ASP.NET Configuration and Environment Variables

Proper configuration and environment variable management in ASP.NET projects are critically important for the sustainability and security of software projects. In order for applications to run smoothly in different environments (development, test, production), configuration files and environment variables must be used effectively. In this article, we will discuss the functioning of the ASP.NET Configuration system and the main points to pay attention to when using environment variables with technical details.

How Does the ASP.NET Configuration System Work?

With ASP.NET Core, configuration operations have become quite flexible. Application configurations are usually kept in the appsettings.json file and can also be taken from different sources if needed. An example of a typical configuration file:

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=localhost;Database=mydb;User Id=myuser;Password=mypassword;"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning"
    }
  }
}

Configurations can be easily read via the IConfiguration interface. Sample usage:

public class HomeController : Controller
{
    private readonly IConfiguration _configuration;

    public HomeController(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    public IActionResult Index()
    {
        string connStr = _configuration.GetConnectionString("DefaultConnection");
        return View();
    }
}

Environment-Specific Settings with Environment Variables

In ASP.NET applications, thanks to environment variables, secret keys or environment-based variables can be easily managed. For example, sensitive information such as a username or password should never be written in plain text in the code or in the appsettings.json file.

.NET can read environment variables via Environment.GetEnvironmentVariable or through a configuration provider:

string apiKey = Environment.GetEnvironmentVariable("MY_API_KEY");

Alternatively, you can apply environment-specific settings for staging, development, or production by using the ASPNETCORE_ENVIRONMENT variable in the appsettings file:

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*"
}

Defining Environment Variables

To assign an environment variable in Windows using Command Prompt:

setx ASPNETCORE_ENVIRONMENT "Production"

For Linux or macOS:

export ASPNETCORE_ENVIRONMENT=Production

Conclusion: Secure and Flexible Configuration

The use of ASP.NET Configuration and environment variables allows projects to be easily moved among different environments while keeping sensitive values such as passwords hidden. Especially in production environments, the security and correctness of all configurations should be regularly checked. Thus, ASP.NET Core projects can be developed to be scalable, sustainable, and secure.