skip to Main Content

I have json object which is reading like configuration.Getsection("XYZconfig").Get(); in code it is working fine in local.
After inserting the config values in azure keyvault the XYZconfig section is taking as below, The quotation is appending before and after brackets. so it is unable to read using configuration.Getsection("XYZconfig").Get(). How to read this config values.

"XYZconfig": "{ "key1":"vaule1",
"key2":"value2",
"key3":"value3"
}"

2

Answers


  1. escape the string sequence

    "XYZconfig": "{ "key1":"vaule1", "key2":"value2", "key3":"value3" }"
    

    List of other string sequence:

    https://www.freeformatter.com/json-escape.html

    Login or Signup to reply.
  2. To access the configuration values you stored in Azure KeyVault from your ASP.NET Core web app, you can refer to following code snippet:

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
        .ConfigureAppConfiguration((context, config) =>
        {
    
            var root = config.Build();
            config.AddAzureKeyVault($"https://{your_keyvault_name_here}.vault.azure.net/", $"{clientid_here}", $"{client_secret_here}");
        })
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });
    

    Read configuration value:

    private readonly ILogger<HomeController> _logger;
    private readonly IConfiguration _config;
    
    public HomeController(ILogger<HomeController> logger, IConfiguration config)
    {
        _logger = logger;
        _config = config;
    }
    
    public async Task<IActionResult> Index()
    {
        var val = _config["XYZconfig"];
    

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search