ASP.NET Core provides a variety of different configuration options. Application configuration data could come from files(e.g. JSON, XML, etc.), environment variables, in-memory collections and so on.
It works with the options model so that you could inject strongly typed settings into your application. You also could create custom configuration providers, which could bring you with more flexibility and extensibility.
According to your requirement, you could follow the steps below to achieve your purpose:
Create a section called AzureStorageConfig in your appsetting.json:
"AzureStorageConfig": {
"AccountName": "<yourStorageAccountName>",
"AccountKey": "<yourStorageAccountKey>"
}
Create a class called AzureStorageConfig like this:
public class AzureStorageConfig
{
public string AccountName { get; set; }
public string AccountKey { get; set; }
}
Then configure the services in your Startup.cs like this:
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
// Setup options with DI
services.AddOptions();
services.Configure<AzureStorageConfig>(Configuration.GetSection("AzureStorageConfig"));
}
Then access it through a controller like this:
private AzureStorageConfig _storageConfig;
public HomeController(IOptions<AzureStorageConfig> config)
{
_storageConfig = config.Value;
}
For more details, you could refer to this Tutorial.