Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accessing appsetting.json values in startup.cs

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,
                    };
                });
        }
like image 976
Jordan McDonald Avatar asked Sep 15 '17 01:09

Jordan McDonald


People also ask

How do I run Appsettings json at startup?

In order to add AppSettings. json file, right click on the Project in Solution Explorer. Then click Add, then New Item and then choose App Settings File option (shown below) and click Add button. Once the File is created, it will have a DefaultConnection, below that a new AppSettings entry is added.

How do I get value from IConfiguration?

Using IConfiguration The IConfiguration is available in the dependency injection (DI) container, so you can directly access JSON properties by simply injecting IConfiguration in the constructor of a controller or class. It represents a set of key/value application configuration properties.

How get values from Appsettings in NET Core?

There are two methods to retrieve our values, string dbConn = configuration. GetSection("MySettings"). GetSection("DbConnection").


3 Answers

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",
}
like image 175
Tech Detail Avatar answered Oct 17 '22 14:10

Tech Detail


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"]
like image 28
Wah Yuen Avatar answered Oct 17 '22 14:10

Wah Yuen


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"
  },
like image 5
Majid joghataey Avatar answered Oct 17 '22 13:10

Majid joghataey