I am developing a Blazor server (.Net 8) application. In the OnAfterRenderAsync
of the index page, if there is no Oath token, I’m sending the user to an external API to get it. Once that is done, the API redirects back to my app. That has worked fine for several weeks and it was one of the first methods I wrote. Wherever I call the Navigation Manager I’m injecting it as [Inject] public NavigationManager NavManager { get; set; }
The call to GetBearerToken
is the relevant line:
protected override async Task a(bool firstRender)
{
var uri = NavManager.ToAbsoluteUri(NavManager.Uri);
QueryHelpers.ParseQuery(uri.Query).TryGetValue("code", out var code);
await AdobeService.GetBearerTokenAsync(code);
var token = (await LocalStorage.GetAsync<string>("token").ConfigureAwait(false)).Value;
if (firstRender)
{
if (!string.IsNullOrEmpty(token))
{
Agreements = await AdobeService.GetAgreements(token).ConfigureAwait(false);
AgreementGrid.FilterMode = Telerik.Blazor.GridFilterMode.FilterRow;
await InvokeAsync(StateHasChanged);
}
}
isLoadingVisible = false;
await InvokeAsync(StateHasChanged);
}
Here is the first part of the method:
public async Task GetBearerTokenAsync(string? code)
{
var adobeToken = (await LocalStorage.GetAsync<string>("token").ConfigureAwait(false)).Value;
HttpResponseMessage response = new();
if (string.IsNullOrEmpty(code) && string.IsNullOrEmpty(adobeToken))
{
var oathUrl = "https://<Oathurl>/public/oauth/v2?redirect_uri=https://localhost:7164&response_type=code&client_id=1AAh7Aod_QvbsTGlX3&scope=user_login:group+agreement_write:group+agreement_read:group";
NavManager!.NavigateTo(oathUrl);
}
else if (string.IsNullOrEmpty(adobeToken))
{
var request = new HttpRequestMessage(HttpMethod.Post, "https://<Oathurl>/oauth/v2/token");
var collection = new List<KeyValuePair<string, string>>();
collection.Add(new("grant_type", "authorization_code"));
collection.Add(new("client_id", ClientId));
collection.Add(new("client_secret", ClientSecret));
collection.Add(new("redirect_uri", "https://localhost:7164"));
collection.Add(new("code", code!));
var content = new FormUrlEncodedContent(collection);
request.Content = content;
<more code follows>
And when it reaches NavManager!.NavigateTo(oathUrl);
I get a null reference error for the Navigation Manager.
This has worked without issue until today. The Oath response includes a refresh token, which my code uses to renew Oath token. However, today I had to clear my browser cache and after that it doesn’t work. You may have noticed I call the Navigation Manager at the start of the OnAfterRenderAsync
to check for query parameters. It works fine there.
I’m at a loss. Any suggestions?