25

I understand how to Configure services for appsettings.json and inject them into a controller. However, I need to use the values in the ConfigureServices when I configure Auth. How would I do this? See my sample below. Specifically this line:

option.clientId = /*Need client Id from appsettings.json*/

Code:

public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.Configure<AADSettings>(Configuration.GetSection("AADSettings"));
            services.Configure<APISettings>(Configuration.GetSection("APISettings"));

            // Add Authentication services.
            services.AddAuthentication(sharedOptions =>
            {
                sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
            })
                // Configure the OWIN pipeline to use cookie auth.
                .AddCookie()
                // Configure the OWIN pipeline to use OpenID Connect auth.
                .AddOpenIdConnect(option =>
                {
                    option.clientId = /*Need client Id from appsettings.json*/

                    option.Events = new OpenIdConnectEvents
                    {
                        OnRemoteFailure = OnAuthenticationFailed,
                    };
                });
        }

3 Answers 3

44

You can access this ConfigureServices method like this

var config = Configuration.GetSection("AADSettings").Get<AADSettings>();
option.clientId = config.ClientId;

For the above code to work you need to have POCO class called AADSettings with ClientId as a property

public class AADSettings
{
 public string ClientId { get; set; }
}

and in appsettings.json file, you need to have an entry like this

"AADSettings": {
    "ClientId": "Client1",
}
0
15

Assuming in your appsettings.json you have it under a node like this:

"option": {
  "clientId": "example client id"
}

then you should be able to access it via the following code

option.clientId = Configuration["option:clientId"]
3
  • 3
    Where does 'Configuration' come from? It is not defined. Mar 1, 2019 at 16:44
  • 5
    It is generally declared at the top of your Startup.cs class and is injected in as from IConfiguration Startup.cs public Startup(IConfiguration configuration, IHostingEnvironment env) { Configuration = configuration; _environment = env; } public IConfiguration Configuration { get; }
    – Wah Yuen
    Mar 4, 2019 at 12:59
  • In my .NET 6 web API project I used the following var app = builder.Build(); var clientId = app.Configuration["option:clientId"]; Feb 24 at 11:58
5

Startup.cs :

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }
    
    public IConfiguration Configuration { get; }
    

    public void ConfigureServices(IServiceCollection services)
    {
   
    }
    public void Configure(IApplicationBuilder app, IWebHostEnvironment)
    {
         User = Configuration.GetSection("HangfireSettings:UserName").Value,
         Pass = Configuration.GetSection("HangfireSettings:Password").Value
    }
}

appsettings.json:

  "HangfireSettings": {
    "UserName": "admin",
    "Password": "admin"
  },

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.