Q:

How to get client information in every IdentityServer4 api calls? (for logging purpose)

I have an api protected by identityServer4:
   services.AddAuthentication(defaultScheme: IdentityServerAuthenticationDefaults.AuthenticationScheme)
        .AddIdentityServerAuthentication(options =>
        {
            options.Authority = "https://localhost:5000";
            options.ApiName = "api1";
            options.ApiSecret = "secret";

            options.EnableCaching = true;
            options.CacheDuration = TimeSpan.FromMinutes(10); // that's the default
        });

    // adds an authorization policy to make sure the token is for scope 'api1'
    services.AddAuthorization(options =>
    {
        // some policies here
    });

I want to log every client's api call (not just the authentication) with the client information like : ClientId, ClientName, ....
here is my interceptor middleware:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger logger)
{
    app.UseHttpsRedirection();
            
    app.Use(async (context, next) =>
    {                
        // Log the incoming request here (e.g clientId, clientName, ...)
        await next.Invoke();
     });

     // ...
}

how can I do that?

A:

I ended up with catching OAuth2 events :
options.OAuth2IntrospectionEvents.OnTokenValidated = async (context) =>
{
   var identity = context.Principal.Identity as ClaimsIdentity;
   identity.AddClaim(new Claim("my_claim", "claim_value"));
   // ...
   // log the request   
}

