Open Closed

Refresh token in blazor server #9568


User avatar
0
neethucp created

Hi, We tried to refresh the token in blazor server as mentioned here. But this is resulting in cookie size to be huge and is blocked by the app gateway. Is there a better way to handle this?


8 Answer(s)
  • User Avatar
    0
    AI-Bot created
    Support Team ABP Agent

    To address the issue of large cookie sizes when implementing refresh tokens in Blazor Server with ABP, consider the following best practices:

    1. Avoid Storing Tokens in Cookies:

      • By default, Blazor Server uses cookie authentication, and storing large tokens (like refresh tokens or JWTs) directly in cookies can easily exceed browser or gateway limits.
      • Instead, rely on server-side session management. Store only a session identifier in the cookie, and keep tokens on the server side (e.g., in a distributed cache or database).
    2. Use Reference Tokens:

      • Configure your IdentityServer/OpenIddict to issue reference tokens instead of self-contained JWTs. Reference tokens are short identifiers, and the actual token data is stored server-side, reducing cookie size.
    3. Adjust Cookie Authentication Options:

      • Set the cookie expiration to match your desired session lifetime and avoid persisting refresh tokens in the cookie.
      • Example:
        context.Services.ConfigureApplicationCookie(options =>
        {
            options.ExpireTimeSpan = TimeSpan.FromMinutes(60); // Set as needed
            options.SlidingExpiration = true;
        });
        
    4. Use the "offline_access" Scope Only When Needed:

      • Only request the "offline_access" scope (which enables refresh tokens) if your application truly requires it. For Blazor Server, you may not need refresh tokens if you can manage user sessions with cookies.
    5. Consider Server-Side Token Storage:

      • If you must use refresh tokens, store them securely on the server (e.g., using distributed cache keyed by user/session) and only keep a session identifier in the cookie.
    6. Review Official Guidance:

    By following these practices, you can avoid large cookies and ensure compatibility with application gateways and browsers.

    Sources:


    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
    neethucp created

    How is this handled in abp generated projects? Is it better to issue reference token or store the token in cache? We also have the issue of menu items getting cleared when token expires when the application is idle for some time and user returns.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    But this is resulting in cookie size to be huge and is blocked by the app gateway.

    Can you share the error message?

    How can I reproduce the error?

    Thanks.

  • User Avatar
    0
    neethucp created

    Hi, It just shows site can't be reached after we login and redirect to home page. The cookie size becomes huge as it is now storing the refresh token also. If we revert token refresh flow, the site works as expected. Looks like app gateway is blocking when the cookie size is large.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Can you share the logs from the current website and the gateway?

    Thanks.

  • User Avatar
    0
    neethucp created

    Hi, We don't have access to Azure Application gateway logs, it's in client environment, we are not getting any logs in blazor server application, it's not even reaching blazor server application.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    ok, can you share a screenshot of cookies?

    eg:

    Thanks.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    You can consider storing tokens in Redis.

    The cache key can be userid + BrowserInfo

    Similar with https://abp.io/community/articles/switching-between-organization-units-i5tokpzt#gsc.tab=0

    Thanks.

Boost Your Development
ABP Live Training
Packages
See Trainings
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 July 17, 2025, 06:22