How to fix builder.Configuration in asp.net 7

LeMonsalve 20 Reputation points
2023-04-12T20:56:55.02+00:00

Hello. As you can see, I am passing the builder.Configuration as an argument to my ConfigureIdentityServices and AddInfrastructureServices functions. When I run the program the jwtKey variable is null and when I access the IConfigurationSection of the builder.Configuration it has its value.

The truth is that I don't know how to explain the situation correctly, so I leave you with images.

I'm using .net 7

At this point i pass the builder.Configuration as argument.
User's image

At this point builder.Configuration has all the values of JwtSettings, but when I try to get it, it returns null. User's image

Can anybody help me? Thank you so much!

Developer technologies ASP.NET ASP.NET Core
Developer technologies ASP.NET ASP.NET API
Developer technologies .NET Other
Developer technologies ASP.NET Other
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2023-04-12T22:58:04.0466667+00:00

    configuration.GetSection("sectionname") return a new Configuration with only the section values. "JwtSettings:Key" is probably a property and not a section. you can use the .Bind(object) to bind the parameters to an object.

    var jwtSettings = new JwtSettings();
    configuration.GetSection("JwtSettings").bind(jwtSettings);
    var key = jwtSettings.Key;
    

    your next line uses the options pattern, and you need to define an options class:

    https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-7.0&viewFallbackFrom=aspnetcore-2.1

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.