Reputation: 438
We set up IdentityServer3 to issue a Reference Token so when we call our Web API, it calls back to IdentityServer to validate the access token at /sts/connect/accesstokenvalidation.
That call can often be unusually slow in our production environment - between 10 and 20 seconds.
Here's an example trace:
w3wp.exe Information: 0 : [Thinktecture.IdentityServer.Core.Endpoints.AccessTokenValidationController]: 5/1/2015 8:29:15 PM +00:00 -- Start access token validation request
w3wp.exe Information: 0 : [Thinktecture.IdentityServer.Core.Validation.TokenValidator]: 5/1/2015 8:29:15 PM +00:00 -- Start access token validation
Debug: [Cache]: 5/1/2015 8:29:25 PM +00:00 -- Cache miss: myApp
Debug: [Cache]: 5/1/2015 8:29:26 PM +00:00 -- Cache hit: myApp
w3wp.exe Information: 0 : [Thinktecture.IdentityServer.Core.Validation.TokenValidator]: 5/1/2015 8:29:26 PM +00:00 -- Token validation success { "ValidateLifetime": true,"AccessTokenType": "Reference","TokenHandle": "ec367c0bee68c8682e000f1526fc7b63"}
w3wp.exe Information: 0 : [Thinktecture.IdentityServer.Core.Endpoints.AccessTokenValidationController]: 5/1/2015 8:29:26 PM +00:00 -- End access token validation request
The cache hit/miss rows are suspicious.
Any Ideas on where to go next to speed up the access token validation?
Upvotes: 1
Views: 3163
Reputation: 438
The line of code that is slow is this one (also printed below, first line of the function)
public async Task<T> GetAsync(string key)
{
var token = await context.Tokens.FindAsync(key, tokenType);
if (token == null || token.Expiry < DateTimeOffset.UtcNow)
{
return null;
}
return ConvertFromJson(token.JsonCode);
}
It's initializing the Entity Framework OperationalDbContext object in the BaseTokenStore abstract class.
It turns out that only the first call is slow, subsequent calls are fast. Therefore we fixed the problem by simply making this call when the application starts up and then no one pays a performance penalty:
var token = await context.Tokens.FindAsync(key, tokenType);
That solved it.
Upvotes: 3