Skip to content

Blazor multiple authentication schemes do not play nice with RenderModeServer #50122

@SPWizard01

Description

@SPWizard01

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Consider the following scenario:

An app is running inside an org that is using on prem AD which syncs with Azure, you want to be able to host your blazor app inside IIS server on prem, but you also want to be able to configure specific route(component) inside blazor to be protected by Microsoft.Identity.Web package.

It partially works until you specify render mode to be RenderModeServer.

Expected Behavior

My understanding that it should fulfill Authorize attribute completely and if you say you want the user to be authorized with OpenIdConnectDefaults.AuthenticationScheme via EntraId policy then it should use that and not Windows auth and vice versa,

Steps To Reproduce

Program.cs

//you can supply OpenIdConnectDefaults.AuthenticationScheme as default scheme
//the behaviour wont change, it will only change the user "Name" that is returned inside component.

builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
.AddNegotiate()
.AddMicrosoftIdentityWebApp(cfg =>
{
    cfg.Instance = globalConfig.AzureAd.Instance;
    cfg.TenantId = globalConfig.AzureAd.TenantId;
    cfg.ClientId = globalConfig.AzureAd.ClientId;
    cfg.CallbackPath = globalConfig.AzureAd.CallbackPath;
    ...
});
...
builder.Services.AddAuthorization(options =>
{
    var entraId = new AuthorizationPolicyBuilder()
    .AddAuthenticationSchemes(OpenIdConnectDefaults.AuthenticationScheme)
    .RequireAuthenticatedUser()
    .Build();

    options.AddPolicy("EntraId", entraId);

    var kerb = new AuthorizationPolicyBuilder()
    .AddAuthenticationSchemes(NegotiateDefaults.AuthenticationScheme)
    .RequireAuthenticatedUser()
    .Build();
    options.AddPolicy("Kerberos", kerb);

    // By default, all incoming requests will be authorized according to the default policy.
    options.FallbackPolicy = options.DefaultPolicy;
});

Component.razor

@using Microsoft.AspNetCore.Authorization;
@using Microsoft.Identity.Web;
@using System.Diagnostics;
@page "/authbugssr"
@attribute [Authorize("EntraId")]
@* Uncomment line bellow and observe OnInitializedAsync firing at least couple of time while providing different user each time first is the user required by configured policy, last is the user configured by default policy*@
@* @attribute [RenderModeServer] *@
@inject AuthenticationStateProvider AuthProvider

<CascadingAuthenticationState>
    <AuthorizeView Policy="EntraId">
        <Authorized>
            Hello, @context.User.Identity.Name!
        </Authorized>
        <NotAuthorized>
            Not authed
        </NotAuthorized>
    </AuthorizeView>
</CascadingAuthenticationState>
@code {

    protected async override Task OnInitializedAsync()
    {
        base.OnInitialized();
        var user = (await AuthProvider.GetAuthenticationStateAsync()).User;
        Debugger.Break();
    }
}

Authorize to your App (so it saves id token to your cookies) then try accessing /authbugssr with RenderModeServer and without it.

Exceptions (if any)

None

.NET Version

8.0.100-preview.7.23376.3

Anything else?

.NET SDK:
Version: 8.0.100-preview.7.23376.3
Commit: daebeea8ea

Runtime Environment:
OS Name: Windows
OS Version: 10.0.19044
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\8.0.100-preview.7.23376.3\

.NET workloads installed:
There are no installed workloads to display.

Host:
Version: 8.0.0-preview.7.23375.6
Architecture: x64
Commit: 65b696cf5e
RID: win-x64

.NET SDKs installed:
6.0.301 [C:\Program Files\dotnet\sdk]
8.0.100-preview.7.23376.3 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 6.0.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.0-preview.7.23375.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.0-preview.7.23375.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 6.0.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.0-preview.7.23376.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
Not set

global.json file:
Not found

Learn more:
https://aka.ms/dotnet/info

Download .NET:
https://aka.ms/dotnet/download

Metadata

Metadata

Assignees

No one assigned

    Labels

    Blazor ♥ SignalRThis issue is related to the experience of Signal R and Blazor working togetherarea-authIncludes: Authn, Authz, OAuth, OIDC, BearerenhancementThis issue represents an ask for new feature or an enhancement to an existing one

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions