IdentityServer4 icon indicating copy to clipboard operation
IdentityServer4 copied to clipboard

Authorization code flow token request is slow (7+ seconds)

Open anwtn opened this issue 9 years ago • 12 comments

  • [x ] I read and understood how to enable logging

Issue / Steps to reproduce the problem

I have setup Identity Server 4 with Entity Framework using .AddConfigurationStore(...), .AddOperationalStore(...) and .AddAspNetIdentity<ApplicationUser>(). My goal here was to setup an IDP with the simplest - path of least resistance - style configuration that can still be used in production.

I'm using the authorization code flow to issue JWT tokens. The tokens are issued, however the token request is quite slow, i.e. > 7 seconds. This is a problem because certain client's who are connecting to the service have set their token request time-out to 5 seconds - I don't have control over the client's behaviour. I want to reduce the amount of time it takes to return the access token.

I've had a read the following Identity Server 3 question:

https://github.com/IdentityServer/IdentityServer3.MembershipReboot/issues/39

While this is not my specific configuration - I'm using ASP.Net Identity rather than MembershipReboot - it hints that the problem could be the password hashing iteration. Therefore, I have added the following to my Startup.config:

services.Configure<PasswordHasherOptions>(options => { options.IterationCount = 10000; });

Unfortunately, this hasn't resolved the issue. I understand that this is the configuration for the user/Identity store, but my understanding is that the hashing is per user account, NOT per application. I did manage to locate details of the request in the logs below. When I added the cumulative time for the SQL queries it was only around 2 seconds, so I'm more convinced that hashing is the culprit.

I had a read of Task<TokenResponse> ProcessAuthorizationCodeRequestAsync(TokenRequestValidationResult request) but I couldn't quite get my head around where the long running/iterative hashing operation might be.

If someone can suggest the configuration I need to use that would be greatly appreciated. I'm quite new to Identity Server so please accept my apologies if I have misunderstood something here.

Relevant parts of the log file

Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request starting HTTP/1.1 POST http://REDACTED/connect/token application/x-www-form-urlencoded 263 IdentityServer4.Hosting.IdentityServerMiddleware:Information: Invoking IdentityServer endpoint: IdentityServer4.Endpoints.TokenEndpoint for /connect/token Microsoft.EntityFrameworkCore.Storage.IRelationalCommandBuilderFactory:Information: Executed DbCommand (174ms) [Parameters=[@__clientId_0='?' (Size = 200)], CommandType='Text', CommandTimeout='30']

... REDACTED many SQL queries in the log here taking ~ 170 milliseconds each

Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request finished in 7701.5831ms 200 application/json

anwtn avatar Sep 01 '17 03:09 anwtn

So you think you've narrowed it down to the password hashing? That's not part of IdentityServer -- that's part of MR. Open an issue there to discuss.

brockallen avatar Sep 04 '17 03:09 brockallen

That seems sensible, although I'm mostly speculating here. I was hoping that this was a common issue which can easily be solved using configuration before I spend another day trying to debug this. I'll dig a little deeper.

anwtn avatar Sep 04 '17 05:09 anwtn

No, we've not heard this type of complaint before, so yes please do some digging so we can get to the bottom of it.

brockallen avatar Sep 04 '17 11:09 brockallen

Any update on the issue? closing for now - feel free to re-open if it needs further discussion.

leastprivilege avatar Sep 19 '17 05:09 leastprivilege

I've been pulled on to other projects and haven't been able to investigate. When I get a chance I will fire up the VS profiler and figure out what's going on.

anwtn avatar Sep 19 '17 05:09 anwtn

I had a chance to run a debugger through this today and I've realised that most of my assumptions about this issue were incorrect. I had initially misunderstood the logs as I had only read up to the point where the token request was validated. It turns out there were further operations after this which generate the token.

The real culprit was the large number of queries. There were over 40 queries with a combined running time of over 4 seconds excluding network communications. I'm going to look for ways to improve this. I'm utilising Entity Framework stores - specifically the packages IdentityServer4.EntityFramework and Microsoft.AspNetCore.Identity.EntityFrameworkCore.

anwtn avatar Oct 05 '17 00:10 anwtn

Or use our caching: http://docs.identityserver.io/en/release/topics/startup.html

brockallen avatar Oct 05 '17 12:10 brockallen

Thank you brockallen.

I moved my Identity Server 4 instance into the same data centre as the database and the token generation is now sub-5 seconds.

I'll look into the caching.

anwtn avatar Oct 06 '17 03:10 anwtn

The caching is almost mandatory for production, IMO, given that our programming model has many places where the config needs to be loaded.

brockallen avatar Oct 06 '17 04:10 brockallen

@mrblurgle there a Caching sample to share?

alexsandro-xpt avatar Jan 03 '19 20:01 alexsandro-xpt

@brockallen I was added services.AddIdentityServer().AddAspNetIdentity<User>().AddInMemoryCaching(); but nothing changed. I continue get same ~6 seconds at /connect/authorize?client_id...... and /connect/userinfo URL at silent process.

alexsandro-xpt avatar Jan 30 '19 13:01 alexsandro-xpt

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Jan 11 '20 21:01 lock[bot]