0

I know that there's a way to get Configuration sections by adding it to the services like so:

Configuration.GetSection("SectionName")

And then using dependency injection, I can use that configuration in my controllers. I also know that there's a way to extract connectionstrings from appsettings.json into the startup.cs.

However, what I want is to use a non-connectionstring value from my appsettings.json to be used in my startup.cs. I have a group in my policy that I want to be configurable in the appsettings.json.

In my appsettings.json, there is a config section that looks like this:

"ConfigurableGroupName": "Administrators",

And my policy is being set like the following:

services.AddAuthorization(options =>
        {
            options.AddPolicy("PolicyName", policy => policy.Requirements.Add(new GroupRequirement("ConfigurableGroupName") ));
        });

How do I replace "ConfigurableGroupName" with the value that I have in my appsettings.json? Is there a way to do this or a better way to do this?

EDIT: This is different because I'm trying to use appsettings values that are not connectionstrings in Startup.cs. Not in the controller or anywhere else.

Hydromast
  • 280
  • 3
  • 10
  • https://stackoverflow.com/a/31453663/6027876 – Aarif Apr 17 '19 at 15:13
  • In aspnetcore2.1 the ctor of `Startup` gets the `IConfiguration` as argument `public Startup(IConfiguration configuration)` – Maximilian Ast Apr 17 '19 at 15:15
  • Unless I'm missing something, there isn't a way to use the appsettings values in the startup.cs itself or turn it into a raw string unless it's a connectionstring. – Hydromast Apr 17 '19 at 15:21
  • @Frosteeze you are mistaken. you can extract anything from the settings. not just the connection string. – Nkosi Apr 17 '19 at 15:22
  • @Frosteeze perhaps if you clarify what it is you are actually trying to extract in a [mcve] we might be able to help. Right now you have not provided enough details. – Nkosi Apr 17 '19 at 15:23
  • Rechecking the dupe target, this is in fact a duplicate as there are multiple answers there that should allow you to achieve what you require. – Nkosi Apr 17 '19 at 15:25
  • 1
    `var groupName = Configuration.GetValue("ConfigurableGroupName");` and you pass that to your `new GroupRequirement(groupName);` – Nkosi Apr 17 '19 at 15:29
  • 1
    @Nkosi Ok I see that this answer https://stackoverflow.com/a/54060567/3866585 might help. The other comment linked to the first top voted answer, which was not helpful. – Hydromast Apr 17 '19 at 15:30
  • @Nkosi that answer worked as well, thank you! – Hydromast Apr 17 '19 at 15:36

0 Answers0