Blazor web App No authenticationScheme was specified?

929Free 641 Reputation points
2025-04-16T05:34:37.9066667+00:00

Hello, I added @attribute [Authorize (Roles="Administrator")] in test.razor component, Non Administrator role error when logging in.

Error Info:

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

Microsoft.AspNetCore.Authentication.AuthenticationService.ChallengeAsync(HttpContext context, string scheme, AuthenticationProperties properties)

I hope to be automatically redirected to the Login page when not logged in as an Administrator Roles.

test.razor:

@page "/test"
@attribute [Authorize(Roles = "Administrator")]

<PageTitle>Test</PageTitle>

Routes.razor:

<Router 
    AppAssembly="typeof(App).Assembly">
    <Found Context="routeData">
        <AuthorizeRouteView RouteData="routeData" DefaultLayout="typeof(MainLayout)" />
        <FocusOnNavigate RouteData="routeData" Selector="h1" />
    </Found>
</Router>

MainLayout.razor:

<AuthorizeView>
    <NotAuthorized>
        @if (!context.User.Identity!.IsAuthenticated)
        {
            <Login />
        }
        else
        {
            <p>You do not have permission to access resources.</p>
        }
    </NotAuthorized>
<AuthorizeView>
</AuthorizeView>

CustomAuthStateProvider.cs:

using System.Security.Claims;
using Microsoft.AspNetCore.Components.Authorization;

public class CustomAuthStateProvider : AuthenticationStateProvider
{
    public override Task<AuthenticationState> GetAuthenticationStateAsync()
    {
        var identity = new ClaimsIdentity(
        [
            new Claim(ClaimTypes.Name, "test"),
			new Claim(ClaimTypes.Role, "user")
        ], "Custom Authentication");

        var user = new ClaimsPrincipal(identity);

        return Task.FromResult(new AuthenticationState(user));
    }
}

Program.cs:

builder.Services.AddAuthorization(); 
builder.Services.AddCascadingAuthenticationState();

builder.Services.AddScoped<AuthenticationStateProvider, CustomAuthStateProvider>();

app.UseAuthentication();
app.UseAuthorization();
app.UseAntiforgery();

Can anyone help me? Thank you

Community Center Not monitored
0 comments No comments
{count} votes

Accepted answer
  1. Pradeep M 9,765 Reputation points Microsoft External Staff Volunteer Moderator
    2025-04-16T09:29:25.2733333+00:00

    Hi 929Free,

    Thank you for reaching out to Microsoft Q & A forum. 

    The error (No authenticationScheme was specified) occurs because your Blazor application hasn't been configured with a default authentication scheme. Even when using a custom AuthenticationStateProvider, Blazor requires an authentication scheme to properly handle role-based authorization and redirection. 

    1.In your Program.cs file, register an authentication scheme. If you’re using cookie-based authentication, you can configure it like this: 

    builder.Services.AddAuthentication("MyCookieAuth")
        .AddCookie("MyCookieAuth", options =>
        {
            options.LoginPath = "/login"; // Adjust to match your login route
        });
    builder.Services.AddAuthorization();
    
    

    Also, make sure the authentication and authorization middleware is added in the correct order: 

    app.UseAuthentication();
    app.UseAuthorization();
    
    

    With this setup, your application will be able to redirect unauthenticated users to the login page and enforce role-based access as expected. 

    If you have found the answer provided to be helpful, please click on the "Accept answer/Upvote" button so that it is useful for other members in the Microsoft Q&A community.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.