Search code examples
c#azure-active-directoryasp.net-core-webapibearer-token

.NET Core 8 Azure AD validation generates The signature is invalid


When using .NET 7 the code below runs well during bearer token validation. Unfortunately, when my Web API application is upgraded to version 8 this code doesn't run well and generates this error message:

Bearer error="invalid_token",error_description="The signature is invalid"

services
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(options =>
{
    options.IncludeErrorDetails = true;
    options.TokenValidationParameters = new TokenValidationParameters
    {
        ValidIssuer = $"https://sts.windows.net/{configuration["AD:TenantId"]}/", 
        ValidateIssuer = true,

        ValidAudience = "00000003-0000-0000-c000-000000000000",
        ValidateAudience = true,

        SignatureValidator = delegate (string token, TokenValidationParameters parameters) { return new JwtSecurityToken(token); },
        ValidateIssuerSigningKey = true,

        RequireSignedTokens = false,
        ClockSkew = TimeSpan.Zero
    };
}, options => configuration.Bind("AD", options));

Then token is generated and sent by a reactjs app. This doesn't work both locally nor published to Azure App Service.


Solution

  • For anyone who are facing the same issue here's the answer:

    SignatureValidator = delegate (string token, TokenValidationParameters parameters)
    {
        //NET 7
        //return new System.IdentityModel.Tokens.Jwt.JwtSecurityToken(token);
        //NET 8
        //return new Microsoft.IdentityModel.JsonWebTokens.JsonWebToken(token);
    }