Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blazor WebAssembly uses the AuthorizationMessageHandler exception #57956

Open
1 task done
wgh2019 opened this issue Sep 19, 2024 · 2 comments
Open
1 task done

Blazor WebAssembly uses the AuthorizationMessageHandler exception #57956

wgh2019 opened this issue Sep 19, 2024 · 2 comments
Labels
area-blazor Includes: Blazor, Razor Components Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue.

Comments

@wgh2019
Copy link

wgh2019 commented Sep 19, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I am following these instructions from the documentation for attaching authentication tokens to outgoing HTTP requests:
like the documentation

https://learn.microsoft.com/en-us/aspnet/core/blazor/security/webassembly/additional-scenarios?view=aspnetcore-8.0#handle-token-request-errors

image

Using a AuthorizationMessageHandler as described does not work at all; the request doesn't even send to the API.

Either something is broken or the documentation is unclear.

Can't the AuthorizationMessageHandler be used directly?

My code

        public static async Task Main(string[] args)
        {
            var builder = WebAssemblyHostBuilder.CreateDefault(args);
            builder.RootComponents.Add<App>("#app");
            builder.RootComponents.Add<HeadOutlet>("head::after");
            builder.Services.AddApiAuthorization();
            builder.Services.AddScoped<IAccessTokenProvider, CustomerAccessTokenProvider>();
            builder.Services.AddScoped<AuthorizationMessageHandler>();
            builder.Services.AddScoped(sp => new HttpClient(sp.GetRequiredService<AuthorizationMessageHandler>().ConfigureHandler(
                authorizedUrls: new[] { builder.HostEnvironment.BaseAddress }
                ))
            { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

            await builder.Build().RunAsync();
        }

Expected Behavior

Authorization: Bearer token should be attached to outgoing request

Steps To Reproduce

No response

Exceptions (if any)

crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: The inner handler has not been assigned.
System.InvalidOperationException: The inner handler has not been assigned.
   at System.Net.Http.DelegatingHandler.SetOperationStarted()
   at System.Net.Http.DelegatingHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Components.WebAssembly.Authentication.AuthorizationMessageHandler.<>n__0(HttpRequestMessage request, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Components.WebAssembly.Authentication.AuthorizationMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
   at System.Net.Http.Json.HttpClientJsonExtensions.<<FromJsonAsyncCore>g__Core|12_0>d`2[[BlazorApp2.Pages.Weather.WeatherForecast[], BlazorApp2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[System.Text.Json.JsonSerializerOptions, System.Text.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51]].MoveNext()
   at BlazorApp2.Pages.Weather.OnInitializedAsync() in E:\Users\wgh\source\repos\BlazorApp2\BlazorApp2\Pages\Weather.razor:line 44
   at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()

.NET Version

net 8.0

Anything else?

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-blazor Includes: Blazor, Razor Components label Sep 19, 2024
@MackinnonBuck
Copy link
Member

Thanks for reaching out, @wgh2019.

Per the docs, you'll need to call AddHttpMessageHandler().

Could you please try doing this and letting us know if that solves the issue? Thanks.

@MackinnonBuck MackinnonBuck added the Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. label Sep 19, 2024
Copy link
Contributor

Hi @wgh2019. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue.
Projects
None yet
Development

No branches or pull requests

2 participants