Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Using YARP, System Web Adapters and AddAuthenticationStateSerialization to authenticate a Blazor application

$
0
0

I suspect I am trying to use too many disparate technologies here, but I thought I would ask for a second opinion before giving up...

I have a .NET 9.0 project that uses YARP to reverse proxy onto our legacy ASP.NET Framework website. Authentication is handled in the legacy website so we are also using web adapters to implement authentication in the .NET 9.0 website. We want to avoid web adapters making unnecessary authentication requests to the YARP fallback route so have followed these guidelines and set the isDefaultScheme parameter to false.

I have also added a Blazor Web Assembly project to this solution and am using authentication state serialization to authenticate this.

The server side program.cs looks like this:

using BlazorClientWithReverseProxy.Components;var builder = WebApplication.CreateBuilder(args);builder.Services.AddReverseProxy()    .LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));builder.Services.AddSystemWebAdapters()    .AddRemoteAppClient(options =>    {        options.ApiKey = "F3A87C59-DA17-404F-B44C-E3594450FF23";        options.RemoteAppUrl = new Uri("http://localhost:8002/");    })    .AddAuthenticationClient(false);builder.Services.AddRazorComponents()    .AddInteractiveWebAssemblyComponents()    .AddAuthenticationStateSerialization(options => options.SerializeAllClaims = true);var app = builder.Build();app.UseWebAssemblyDebugging();app.UseSystemWebAdapters();app.UseHttpsRedirection();app.UseAntiforgery();app.MapStaticAssets();app.MapRazorComponents<App>()    .AddInteractiveWebAssemblyRenderMode()    .AddAdditionalAssemblies(typeof(BlazorClientWithReverseProxy.Client._Imports).Assembly);app.MapReverseProxy();app.Run();

And the client program.cs looks like this:

using Microsoft.AspNetCore.Components.WebAssembly.Hosting;var builder = WebAssemblyHostBuilder.CreateDefault(args);builder.Services.AddAuthorizationCore();builder.Services.AddCascadingAuthenticationState();builder.Services.AddAuthenticationStateDeserialization();await builder.Build().RunAsync();

My Blazor page looks like this:

@attribute [Authorize]@page "/hello"@using Microsoft.AspNetCore.Authorization@rendermode InteractiveWebAssembly<PageTitle>Hello world!</PageTitle>

When I try navigate to the hello page, I get this error:

An unhandled exception occurred while processing the request.
InvalidOperationException: No authenticationScheme was specified, and there was no DefaultChallengeScheme found. The default schemes can be set using either AddAuthentication(string defaultScheme) or AddAuthentication(Action configureOptions).

I can fix this by setting .AddAuthenticationClient(true); in the server side program.cs, but this will re-introduce the excessive authentication requests of the reverse proxy fallback.

How can Blazor use the authentication state serialization with web adapters without preforming the YARP fallback traffic requests?


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>