Skip to content

JwtBearer doesn't populate the ValidIssuer and the ValidAudience properties of the JwtBearerOptions.TokenValidationParameters. #52820

@satma0745

Description

@satma0745

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

The JwtBearerConfigureOptions class reads values for both the ValidIssuer and the ValidAudience from the configuration and even saves them to the ValidIssuers and ValidAudiences collections in the TokenValidationParameters, but completely ignores the corresponding ValidIssuer and ValidAudience properties of the same TokenValidationParameters.

The simplified representation of what is happening right now:

// Load ValidIssuers from authentication configuration.
var issuers = configSection
    .GetSection(nameof(TokenValidationParameters.ValidIssuers))
    .GetChildren()
    .Select(iss => iss.Value)
    .ToList();

// Also load a ValidIssuer from the authentication configuration, then add it
// to the ValidIssuers collection.
var issuer = configSection[nameof(TokenValidationParameters.ValidIssuer)];
if (issuer is not null)
{
    issuers.Add(issuer);
}

// Load ValidAudiences from authentication configuration.
var audiences = configSection
    .GetSection(nameof(TokenValidationParameters.ValidAudiences))
    .GetChildren()
    .Select(aud => aud.Value)
    .ToList();

// Also load a ValidAudience from the authentication configuration, then add it
// to the ValidAudiences collection.
var audience = configSection[nameof(TokenValidationParameters.ValidAudience)];.
if (audience is not null)
{
    audiences.Add(audience);
}

// Only populate the ValidIssuers and the ValidAudiences properties, completely
// ignoring the ValidIssuer and the ValidAudience properties.
options.TokenValidationParameters = new()
{
    ValidIssuers = issuers,
    ValidAudiences = audiences
};

Note

Please note that this is a very abbreviated code example and that it differs significantly from the actual JwtBearerConfigureOptions.Configure method implementation.

Describe the solution you'd like

I would like that if configuration explicitly specifies a value for the ValidIssuer and ValidAudience, that value will end up in the TokenValidationParameters.

Basically, I would like the JwtBearerConfigureOptions.Configure method to also populate the ValidIssuer and the ValidAudience properties too:

// Load ValidIssuers from authentication configuration.
var issuers = configSection
    .GetSection(nameof(TokenValidationParameters.ValidIssuers))
    .GetChildren()
    .Select(iss => iss.Value)
    .ToList();

// Also load a ValidIssuer from the authentication configuration, then add it
// to the ValidIssuers collection.
var issuer = configSection[nameof(TokenValidationParameters.ValidIssuer)];
if (issuer is not null)
{
    issuers.Add(issuer);
}

// Load ValidAudiences from authentication configuration.
var audiences = configSection
    .GetSection(nameof(TokenValidationParameters.ValidAudiences))
    .GetChildren()
    .Select(aud => aud.Value)
    .ToList();

// Also load a ValidAudience from the authentication configuration, then add it
// to the ValidAudiences collection.
var audience = configSection[nameof(TokenValidationParameters.ValidAudience)];.
if (audience is not null)
{
    audiences.Add(audience);
}

// Only populate the ValidIssuers and the ValidAudiences properties, completely
// ignoring the ValidIssuer and the ValidAudience properties.
options.TokenValidationParameters = new()
{
+ ValidIssuer = issuer,
    ValidIssuers = issuers,
+ ValidAudience = audience,
    ValidAudiences = audiences
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-authIncludes: Authn, Authz, OAuth, OIDC, Bearerarea-identityIncludes: Identity and providersbugThis issue describes a behavior which is not expected - a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions