page components doesn't update after oidc authentication in blazor web app server based

Zahra Ghadyani 40 Reputation points
2025-06-18T15:25:31.8166667+00:00

I have a blazor web app (server based) with page components defined parametrically as below:

@page "/System/{iSysnum:int?}"
@page "/System/{iSysnum:int?}"
@implements IDisposable
@rendermode InteractiveServer
@page "/System/{iSysnum:int?}"
...
@using Microsoft.AspNetCore.Authorization
@attribute [Authorize]

I have added authentication in program.cs as follows:

builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
    .AddMicrosoftIdentityWebApp(options =>
    {
        builder.Configuration.Bind("AzureAdB2C", options);
    });

After successful log in in the web app, the pages /system/1, /system/2, ... become available and I can navigate to any one (/system/n). But when I navigate to a different page (/system/m) , only the heading text is updated but presented data are still from previous system. The system data gets updated when refresh the browser, but it is not the desired behavior...

P.S. The web app did update data for each system when navigating between pages before implementing the B2C authorization.

Community Center Not monitored
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2025-06-18T22:28:09.7766667+00:00

    when your Blazor app uses Azure Ad to login, the Blazor app redirects to a razor page to authenticate. this unloads the Blazor app. After authentication the razor pages redirects back to the Blazor hosting page which reloads the app. no Blazor state is preserved automatically. There must be a coding error on your part.

    maybe your old code did redirects instead of Blazor navigation.

    1 person found this answer helpful.

  2. miss makima 0 Reputation points
    2025-06-24T13:22:02.2066667+00:00

    Answer:

    This issue is likely related to component reuse and state not being reset properly when navigating between routes that use the same Blazor component but with different route parameters (/system/{iSysnum:int?}).

    In Blazor Server, if a component is reused across multiple routes, Blazor may not reinitialize it fully unless it’s explicitly told to do so. This behavior is especially noticeable after adding authentication or changing how state is managed.

    Here’s how to resolve it:


    1. Use OnParametersSetAsync() Instead of OnInitializedAsync()

    If you're currently loading data in OnInitializedAsync(), try moving that logic to OnParametersSetAsync():

    csharp
    Copy
    protected override async Task OnParametersSetAsync()
    {
        await LoadSystemData(iSysnum);
    }
    

    Why? OnInitializedAsync() only runs once per component's lifetime. OnParametersSetAsync() runs every time the route parameter (iSysnum) changes.


    2. Clear Previous State Manually (If Needed)

    If your component has cached or bound data that isn’t updating, clear or reinitialize it at the beginning of OnParametersSetAsync() to avoid stale data:

    csharp
    Copy
    protected override async Task OnParametersSetAsync()
    {
        systemData = null; // clear previous data
        await LoadSystemData(iSysnum);
    }
    

    3. Add Logging to Debug Parameter Handling

    To confirm whether OnParametersSetAsync() is being called, add logging or use breakpoints to track parameter changes.


    4. Double-Check Authentication Flow

    Though the issue began after adding Azure B2C, it’s likely coincidental — unless token-related services or HttpClient configuration changed how your data service retrieves information.

    Ensure your authenticated service call (e.g., an injected API service) properly uses the current identity context.


    Summary

    The page component is being reused by Blazor, and unless OnParametersSetAsync() is used to handle dynamic route parameters, it won’t fetch new data on parameter changes. Moving your data-loading logic to that lifecycle method should fix the issue.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.