I managed to make it work in MAUI this way.
In your App.Xaml.cs get the service provider
public App(IServiceProvider provider)
{
InitializeComponent();
Services = provider;
}
Then you can get the configuration instance and replace HttpClient. You can call this in your custom onstart method
var configuration = Services.GetService<IConfiguration>();
configuration.HttpClient = new HttpClient(new AuthenticatedHttpImageClientHandler('your token here');
This is the class that inserts the token
public class AuthenticatedHttpImageClientHandler : HttpClientHandler
{
private readonly Func<Task<string>> _getToken;
public AuthenticatedHttpImageClientHandler(Func<Task<string>> getToken)
{
_getToken = getToken ?? throw new ArgumentNullException("getToken");
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
request.Headers.Add("Authorization", "Bearer " + _getToken.Invoke().Result);
return await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
}
}