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

Trouble with Authorization in Blazor during migration to .NET 8

$
0
0

I am migrating the Blazor Server project from .NET 7 to .NET 8. I ran into the following problem: the site starts normally from the main page, I log in and go to the pages requiring authorization - everything works as it should. But if I click the "refresh" button in the browser, an error occurs:

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).

The same error occurs when setting up the opening of the site immediately from a secure page. There are elements on the main page that are available only to authorized users. They are decorated with the following tags:

<AuthorizeView Roles="Admin"><Authorized Context="adminContext">...</AuthorizeView></Authorized>

There is a tag on the pages where the error occurs:

@attribute [Authorize(Roles = "Admin")]

If I remove this tag and put the entire page content in the AuthorizeView, then everything starts working correctly.I use the following constructions in the Program.cs:

var builder = WebApplication.CreateBuilder(args);builder.Services.AddRazorComponents()        .AddInteractiveServerComponents();builder.Services.AddAuthenticationCore();builder.Services.AddAuthentication();builder.Services.AddAuthorization();builder.Services.AddHttpContextAccessor();builder.Services.AddScoped<AuthenticationStateProvider, MyAuthenticationStateProvider>();builder.Services.AddServerSideBlazor().AddCircuitOptions(options => { options.DetailedErrors = true; });var app = builder.Build();app.UseHttpsRedirection();app.UseStaticFiles();app.UseAuthentication();app.UseAuthorization();app.UseAntiforgery();app.MapRazorComponents<App>()        .AddInteractiveServerRenderMode();app.Run();

I also tried replacing

.AddAuthentication(); 

with

.AddAuthentication(options =>{    options.DefaultScheme = IdentityConstants.ApplicationScheme;    options.DefaultSignInScheme = IdentityConstants.ExternalScheme;})    .AddIdentityCookies();

In this case, when refresh the page, the site tries to redirect me to an address https://localhost:7174/Account/Login that does not exist in my project.

Thank you for any help and advice!


Viewing all articles
Browse latest Browse all 4839

Trending Articles



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