Im trying to move my Blazor project from .net 6 to 8 and go from WASM to a Blazor Web App so i can use interactive rendering and keep within .net support. My current client uses the following code to attach the token in a custom message handler and i inject the httpclient on the components.
builder.Services.AddScoped<CustomAuthorizationMessageHandler>();builder.Services.AddHttpClient("API", client =>client.BaseAddress = new Uri("https://localhost:7191")).AddHttpMessageHandler<CustomAuthorizationMessageHandler>();builder.Services.AddTransient(sp => sp.GetRequiredService<IHttpClientFactory> ().CreateClient("API"));builder.Services.AddMsalAuthentication<RemoteAuthenticationState, CustomUserAccount>(options =>{ builder.Configuration.Bind("AzureAd", options.ProviderOptions.Authentication); options.ProviderOptions.DefaultAccessTokenScopes.Add("api://address.here/API.Access"); options.ProviderOptions.LoginMode = "redirect"; options.UserOptions.RoleClaim = "appRole";}).AddAccountClaimsPrincipalFactory<RemoteAuthenticationState, CustomUserAccount, CustomAccountFactory>();My Custom message handler contains the following:
public class CustomAuthorizationMessageHandler : AuthorizationMessageHandler{ public CustomAuthorizationMessageHandler(IAccessTokenProvider provider, NavigationManager navigationManager) : base(provider, navigationManager) { ConfigureHandler( authorizedUrls: new[] { "https://localhost:7191", "https://other.address.com" }); }}If i do this in the Client side of a Blazor Web App then the baseurl is null, I have tried creating a named httpclient but this is also the same result.
If i move this to the Server side and place it in the program.cs then it is unable to resolve due to Microsoft.AspNetCore.Components.WebAssembly.Authentication.IAccessTokenProvider not being available.
How can i get the token on the Server side and add it to the httpclient or is there a way to set the Web App to use the client side to create the httpclient for my external calls?