I would like to identify a client using the GUID available in the ExtendedClients table. When a client requests a token using the client_credentials flow, the claim available in the token is the ClientId, which is a string evaluated with a speaking name.
I would like to use the GUID for the client since the user subject is also a GUID. In my application, I need to log the subject or the client performing actions, and having both in the same format will make it faster.
Is there a standard or custom way to do this?
Using a custom implementation of ICustomTokenRequestValidator I am able to read the client GUID from the DB and add manually the information at each token request but I think is not a good way for performance reason:
public class CustomTokenRequestValidator : ICustomTokenRequestValidator
{
private readonly string _connectionString;
public CustomTokenRequestValidator(ILogger<CustomTokenRequestValidator> logger, IAppSettings appsettings)
{
_connectionString = appsettings.ConnectionString_Default;
}
public Task ValidateAsync(CustomTokenRequestValidationContext context)
{
var request = context.Result.ValidatedRequest;
// Check if the grant type is client credentials
if (request.GrantType == GrantType.ClientCredentials)
{
// Add the client_id as a claim
request.ClientClaims.Add(new System.Security.Claims.Claim("client_identifier", GetClientIdentifier(request.ClientId)));
}
return Task.CompletedTask;
}
}
private string GetClientIdentifier(string clientId)
{
///DB query to obtain the GUID
}