5

I have IdentityServer4 that generates signed JWT tokens. In my web api I added auth middleware to validate these tokens:

         app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
        {
            Authority = env.IsProduction() ? "https://www.example.com/api/" : "http://localhost/api/",
            AllowedScopes = { "WebAPI", "firm",
                IdentityServerConstants.StandardScopes.OpenId,
                IdentityServerConstants.StandardScopes.Profile },
            RequireHttpsMetadata = env.IsProduction(),
        });

It works perfectly. However, I suspect it doesn't verify signature of jwt token because there is no public key configured to validate token. How to configure token signature validation?

PS: I try to use UseJwtBearerAuthentication instead this way:

        var cert = new X509Certificate2("X509.pfx", "mypassword");
        var TokenValidationParameters = new TokenValidationParameters
        {
            ValidateIssuerSigningKey = true,
            ValidateIssuer = true,
            ValidIssuer = env.IsProduction() ? "https://www.example.com/api/" : "http://localhost/api/",
            IssuerSigningKey = new X509SecurityKey(cert),
        };
        app.UseJwtBearerAuthentication(new JwtBearerOptions
        {
            Authority = env.IsProduction() ? "https://www.wigwam3d.com/api/" : "http://localhost/api/",
            Audience = "WebAPI",
            RequireHttpsMetadata = env.IsProduction(),
            TokenValidationParameters = TokenValidationParameters
        });

It also works (and I hope validates token signature also!) but gives me another bug:

UserManager.GetUserAsync(HttpContext.HttpContext.User)

return null, while using UseIdentityServerAuthentication returns me correct User

Rem
  • 9,865
  • 11
  • 38
  • 59

2 Answers2

5

I think there is no need to add certificate to you API for validation. .UseIdentityServerAuthentication() middleware calls your IdentiyServer to retrieve public key on startup from https://www.example.com/api/.well-known/openid-configuration. At least that's my understanding how it works.

pauliusnrk
  • 583
  • 7
  • 17
  • Can you prove it? I can't find any calls to introspection endpoint in this repo https://github.com/IdentityServer/IdentityServer4.AccessTokenValidation – Rem Jun 17 '17 at 05:50
  • 2
    you are right https://stackoverflow.com/questions/38394545/usejwtbearerauthentication-signing-key Thanks! – Rem Jun 17 '17 at 05:58
2

Finally I done it with JwtBearerAuthentication,

GetUserAsync function failure can be fixed with call to:

JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

because of this issue: https://github.com/aspnet/Security/issues/1043

Any ideas to configure the same using IdentityServer auth are welcome!

Rem
  • 9,865
  • 11
  • 38
  • 59