Skip to content

Blazor Websocket disconnects when not authenticated (AllowAnonymous) #43218

@youss2017

Description

@youss2017

I'm using windows authentication but there are pages that are accessible by everyone. I allow anonymous by adding this @attribute [Authorize, AllowAnonymous] to _Hosts.cshtml.

Problem: Although the page still loads, since the websocket disconnects, all blazor events such as @onclick, @bind no longer work.

Expected behavior:
The user is not allowed to view Authorized pages however anonymous page should be visible and have all blazor events working.

Edge Console error:
image

Program.cs
`
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
Configuration = builder.Configuration;

        // Add services to the container.
        builder.Services.AddRazorPages();
        builder.Services.AddServerSideBlazor();
        builder.Services.AddHttpClient();
        builder.Services.AddControllers();
        builder.Services.AddHttpContextAccessor();

        // Add services to the container.
        builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
            .AddNegotiate();
       
        builder.Services.AddAuthorization(options =>
        {
            // By default, all incoming requests will be authorized according to the default policy.
            options.FallbackPolicy = options.DefaultPolicy;
        });

        var app = builder.Build();

        // Configure the HTTP request pipeline.
        if (!app.Environment.IsDevelopment())
        {
            app.UseExceptionHandler("/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        //app.UseHttpsRedirection();

        app.UseStaticFiles();
        app.UseRouting();

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

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
            endpoints.MapFallbackToPage("/_Host");
            endpoints.MapBlazorHub();
        });

        app.Run();
    }
}

`

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions