Issue with IAccessTokenProvider on iOS devices

Enrico Rossini 176 Reputation points
2022-07-15T11:34:29.313+00:00

In my Blazor WebAssembly, I call the API to fetch some data. The user must be authenticated against the Identity Server. In the header of the request, I add the user token.

In the page, the user has to select some dropdown list and then press the submit button. On the click, the application calls the API with the following function (the result conversion is omitted).

private readonly IAccessTokenProvider _accessToken;  

public APIService(HttpClient httpClient, IAccessTokenProvider accessToken)  
{  
    _httpClient = httpClient;  
    _accessToken = accessToken;  
}  

public async Task<APIResponse> GetAttributeAsync(APIRequest apirequest)  
{  
		var tokenResult = await _accessToken.RequestAccessToken();  
		tokenResult.TryGetToken(out var token);  

		HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, $"typing");  
		request.Headers.Authorization =   
            new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token.Value);  

        // ...  

        return APIResponse;  
}  

Then, it hides the form and display the result from the API. There is a back button, that hides the result and shows again the form.

The web application is working fine as I expected. Then, I wanted to try the web application on iOS (iPhone or iPad). I fill the form, submit the request and display the data as expected. Then I click the button to display again the form and send another request: when I sent the request, the function above generates an error

Arg_NullReferenceException

221112-screenshot-2022-07-15-at-122019.png

If I refresh the page, the web application is working again. The behaviour is consistent in iOS with Safari, Edge and Chrome.

I guess, for some reasons, the RequestAccessToken() is not able the second time to retrieve the user token.

Have someone else had this issue? How can I fix it?

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,372 questions
{count} votes