Search code examples
c#jwt.net-8.0secret-key

Why I am getting error trying to access app settings?


When trying to run my API I am getting this error:

Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 's')
at System.ArgumentNullException.Throw(String paramName)
at System.Text.Encoding.GetBytes(String s)
at Program.$(String[] args) in C:\Users\ramit\OneDrive\Documents\GitHub\Photos\HSPA2024\WebAPI\Program.cs:line 24

In my program.cs I have the below code in lines 23,24:

var secretKey = builder.Configuration.GetSection("AppSettings:Key").Value;
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey));

and in appsettings:

"AppSettings": {
"Key": "this is my top secret, try to add more for it to work correctly"}

What is the error suggests? What is (Parameter 's')?


Solution

  • It might sound embarrassing but I was putting the AppSettings in the wrong file. I put that in launchsettings.json instead of appsettings.json