Microsoft.AspNetCore.Authentication.AuthenticationFailureException: An error was encountered while handling the remote login. ---> Microsoft.IdentityModel.Protocols.OpenIdConnect.OpenIdConnectProtocolException: Message contains error: 'invalid_grant', error_description: 'The specified token is invalid.', error_uri: 'https://documentation.openiddict.com/errors/ID2004'. at Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler.RedeemAuthorizationCodeAsync(OpenIdConnectMessage tokenEndpointRequest) at Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler.HandleRemoteAuthenticateAsync() --- End of inner exception stack trace --- at Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler`1.HandleRequestAsync() at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) at Prometheus.HttpMetrics.HttpRequestDurationMiddleware.Invoke(HttpContext context) at Prometheus.HttpMetrics.HttpRequestCountMiddleware.Invoke(HttpContext context) at Prometheus.HttpMetrics.HttpInProgressMiddleware.Invoke(HttpContext context) at Volo.Abp.AspNetCore.Security.AbpSecurityHeadersMiddleware.InvokeAsync(HttpContext context, RequestDelegate next) at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.InterfaceMiddlewareBinder.<>c__DisplayClass2_0.<
- Steps to reproduce the issue: A micro service architecture solution, from a web or a public web redirect to a separated OpenIddict AuthServer, after the authority, redirected to the original the url, occurred this error
6 Answer(s)
-
0
Solution
The error message "invalid_grant", error_description: "The specified token is invalid." typically occurs during the OpenID Connect authorization code flow when the authorization code is invalid, expired, or has already been used. In ABP microservice solutions with a separated AuthServer (using OpenIddict), this is a common scenario if:
- The authorization code is used more than once (it must be used only once).
- There is a time synchronization issue between the servers (AuthServer and Web).
- The redirect_uri used in the token request does not exactly match the one used in the initial authorization request.
- The code is expired (authorization codes are short-lived).
- There is a misconfiguration in the client or server (e.g., wrong client_id, client_secret, or redirect_uri).
Recommended steps to resolve:
- Ensure the system clocks of all servers (AuthServer, Web, etc.) are synchronized (use NTP).
- Verify that the redirect_uri in your client configuration matches exactly (including trailing slashes) between the initial request and the token request.
- Make sure the authorization code is not being reused.
- Check that the client_id and client_secret are correct and match the configuration in the AuthServer.
- If you are behind a load balancer or using multiple instances, ensure sticky sessions are enabled or use a distributed cache for OpenIddict's data protection and authorization code storage.
- Clear browser cookies and try again to avoid stale or reused codes.
No verified source available for this specific query.
This response was generated by AI. If it didn’t solve your problem, feel free to post a new answer — our support team will jump in to help you soon.
-
0
hi
from a web or a public web redirect to a separated OpenIddict AuthServer, after the authority, redirected to the original the url, occurred this error
Please enable debug logs/ShowPII for
web
,public web
, andAuthServer
. Then, reproduce the problem and share alllogs.txt
files.https://abp.io/support/questions/8622/How-to-enable-Debug-logs-for-troubleshoot-problems
liming.ma@volosoft.com
Thanks.
-
0
Logs file sent to you
-
0
Add
Microsoft.IdentityModel
with the latest version that fixes the problem.<PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="8.12.1" /> <PackageReference Include="Microsoft.IdentityModel.Tokens" Version="8.12.1" /> <PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="8.12.1" />
-
0
Thanks a lot, MaLiming
-
0
: )