KindeK
Kinde2y ago
13 replies
skywalker-kiwi#02131

ASP.NET API not validating token

Hi, I am configuring as ASP.NET API, and I am having issues getting my token to validate.

I have configured my validation like so:

builder.Services.AddAuthentication(options => 
{
    options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
    options.Authority = jwtIssuer;
    options.Audience = jwtAudience;
});


I get my token from a Nuxt front end and pass it as an authorization header on request to the ASP.NET backend.

I have checked my JWT here: https://jwt.io/ and everything appears to be in order. I can use it just fine on my front end.

When I run my controller, the HttpContext.User is always null.

I have also tried this configuration setup:

builder.Services.AddAuthentication(options => 
{
    options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
})
    .AddJwtBearer(options =>
    {
        options.TokenValidationParameters = new TokenValidationParameters
        {
            ValidateAudience = true,
            ValidateLifetime = true,
            ValidateIssuerSigningKey = true,
            ValidIssuer = jwtIssuer,
            ValidateIssuer = true,
            ValidAudience = jwtAudience,
            IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtKey))
        };
        options.IncludeErrorDetails = true;
    });


Any help would be greatly appreciated
JSON Web Tokens are an open, industry standard RFC 7519 method for representing claims securely between two parties.
JWT.IO
Was this page helpful?