After reading official documentation here I still don't understand how to load and use these appsetting.json config file. In theory all seems logic but when I try to setup this in my class I have issue.
Let me give you part of my code first. For those I know I'm doing this in a IndentityServer4 implementation but I need to do the same in my API and client.
I modified the Program.cs file to include this new way to load config file as explained in the documentation:
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.SetBasePath(Directory.GetCurrentDirectory());
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: false);
})
.UseStartup<Startup>();
}
I did nothing related to config in my Statup.cs
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddIdentityServer()
.AddDeveloperSigningCredential(persistKey: false)
.AddTestUsers(Config.GetUsers())
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients());
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseIdentityServer();
app.UseStaticFiles();
app.UseMvcWithDefaultRoute();
}
}
And finally my Config.cs, or a part of my config file. This is in this one I want to use my config. Let's say, to keep things simple, I would like to load all my strings from my config file.
public static class Config
{
public static IEnumerable<Client> GetClients()
{
return new List<Client>()
{
new Client
{
ClientName = "xxxxxxxx",
ClientId = "f26ee5d6-xxxx-xxxx-xxxx-xxxx0efa43d9.local.app",
AllowedGrantTypes = GrantTypes.Code,
AllowOfflineAccess = true,
IdentityTokenLifetime = 60 * 60 * 24,
AccessTokenLifetime = 60 * 60 * 24,
RedirectUris = new List<string>()
{
"https://www.getpostman.com/oauth2/callback"
},
PostLogoutRedirectUris = new List<string>()
{
"https://www.getpostman.com"
},
AllowedCorsOrigins = new List<string>()
{
"https://www.getpostman.com"
},
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
"xxxxxx",
},
ClientSecrets = new List<Secret>
{
new Secret("XXXXXXXX".Sha256())
},
AllowAccessTokensViaBrowser = true,
RequireConsent = false,
EnableLocalLogin = true,
Enabled = true
}
};
}
}
You probably noticed my config.cs is static and probably loaded by a code I cannot handle. So I don't know how I can "inject" this configuration here.
Create a strongly typed object model that maps to your settings
public class MySettings {
//Properties here
}
inject the IConfiguration
into Startup
private IConfiguration configuration;
public Startup(IConfiguration configuration) {
this.configuration = configuration;
}
Use configuration to bind to object model and explicitly inject it into dependent methods
public void ConfigureServices(IServiceCollection services) {
MySettings settings = configuration.GetSection("Section_Name_Here").Get<MySettings>();
services.AddMvc();
services.AddIdentityServer()
.AddDeveloperSigningCredential(persistKey: false)
.AddTestUsers(Config.GetUsers())
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients(settings)); //<--
}
Where the dependent method would have been refactored accordingly
public static IEnumerable<Client> GetClients(MySettings settings) {
return new List<Client>() {
new Client {
ClientName = settings.ClientName,
//...omitted for brevity