Open Closed

OpenIdConnectProtocolException: Message contains error: 'invalid_grant', error_description: 'The specified token is invalid.', #9632


User avatar
0
trendline created

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)
  • User Avatar
    0
    AI-Bot created
    Support Team ABP Agent

    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:

    1. The authorization code is used more than once (it must be used only once).
    2. There is a time synchronization issue between the servers (AuthServer and Web).
    3. The redirect_uri used in the token request does not exactly match the one used in the initial authorization request.
    4. The code is expired (authorization codes are short-lived).
    5. 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.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    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, and AuthServer. Then, reproduce the problem and share all logs.txt files.

    https://abp.io/support/questions/8622/How-to-enable-Debug-logs-for-troubleshoot-problems

    liming.ma@volosoft.com

    Thanks.

  • User Avatar
    0
    trendline created

    Logs file sent to you

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    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" />
    
  • User Avatar
    0
    trendline created

    Thanks a lot, MaLiming

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    : )

Learn More, Pay Less
33% OFF
All Trainings!
Get Your Deal
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.0.0-preview. Updated on September 16, 2025, 10:35