Home > Back-end >  Get Azure Configuration value for Blazor
Get Azure Configuration value for Blazor

Time:06-17

I am trying to pull appsetting.json settings (In Blazor Server app) when I run my code locally and have the settings be pulled from Azure's Configuration when it is running on Azure. But, my code will pull from appsettings.json even if it is live. I pull these values for my Startup.cs file, like this:

options.Authority = Configuration.GetValue<string>("AuthenticationServer:IDAuthority");
options.ClientId = Configuration.GetValue<string>("AuthenticationServer:IDClientID");                 
options.CallbackPath = Configuration.GetValue<string>("AuthenticationServer:IDRedirectURI");
               

In my appsetting.json file, I have the settings stored like this:

{
  "AuthenticationServer": {
    "IDAuthority": "Some Value",
    "IDClientID": "Another Value",
    "IDRedirectURI": "/Index/"
  },
}

And in Azure App Service, under Settings->Configuration, in the Application Settings Tab, I have three key/value pairs:

"IDAuthority" - "New Value"

"IDClientID" - "Another New Value"

"IDRedirectURI" - "/Index/"

But when I do this, the values still get pulled from appsettings.json and not Azure. I've also tried:

"AuthenticationServer_IDAuthority" - "New Value"

"AuthenticationServer_IDClientID" - "Another New Value"

"AuthenticationServer_IDRedirectURI" - "/Index/"

And get the same results. So, how should I pull these values from Azure?

As an aside, getting the Azure db connection string like this, works fine:

services.AddDbContext<DBContext>(options =>
                options.UseSqlServer(
                    Configuration.GetConnectionString("ConnString")));

CodePudding user response:

I think you can only change the code before publish it to AZURE.

Firstly we need to know when we want to read the value stored in azure app configuration, we need to make your application connect to Azure app configuration and then we can get what we set, and we can get value with the key name. And the opreation should be the same as getting value from appsettings.json, here's my code:

var builder = WebApplication.CreateBuilder(args);
ConfigurationManager configuration = builder.Configuration;

var connectionString = "connection_string_of_app_configuration";

builder.Host.ConfigureAppConfiguration(builder =>
{
    //Connect to your App Config Store using the connection string
    builder.AddAzureAppConfiguration(connectionString);
});

var a = configuration["Logging:LogLevel:Default"];//get log setting in appsetting.json
var c = configuration["Logging:LogLevel:Default"];//also set the same key in azure
var b = configuration["TestApp:Settings:Message"];//get from app configuration

enter image description here

So I think you may comment the code connect to Azure app configuration when you want to read configuration in appsettings.json, and un-comment the code before you want to publish to azure.

CodePudding user response:

Seems to be a naming convention issue. Because my values were nested in my appsettings.json file, I had to make sure the depth of the parameter was included in the Azure setting.

To get the value:

options.Authority = Configuration["AuthenticationServer:IDAuthority"];

and I then had to make sure the configuration setting, in Azure, was titled: "AuthenticationServer:IDAuthority"

  • Related