You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MAUI应用AAD认证后ClaimsPrincipal的IsAuthenticated始终为false问题

问题:MAUI Blazor应用AAD登录后ClaimsPrincipal.IsAuthenticated始终为false
  • 本地运行MAUI应用,启动时自动触发Microsoft登录流程,用户成功登录后,获取到的ClaimsPrincipal实例的IsAuthenticated属性始终为false
  • 由于Main.razor中的AuthorizeRouteView会在用户未认证时触发NotAuthorized模板,调用RedirectToLogin组件,导致已登录用户无法正常进入授权页面,推测该问题直接由IsAuthenticated为false导致
  • 了解到部分解决方案需要通过传入认证类型参数来构建ClaimsPrincipal,示例代码如下:
    new ClaimsPrincipal(new Identity("something"))
    
    但当前ClaimsPrincipal是从其他团队负责的AAD获取,无法查看AAD配置,因此不知道如何针对性处理

相关代码

Main.razor组件

<Router AppAssembly="@typeof(Main).Assembly">
    <Found Context="routeData">
        <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
            <Authorizing>
                Authorizing...
            </Authorizing>
            <NotAuthorized>
                <RedirectToLogin />
            </NotAuthorized>
        </AuthorizeRouteView>
    </Found>
    <NotFound>
        <CascadingAuthenticationState>
            <LayoutView Layout="@typeof(MainLayout)">
                <p role="alert">Sorry, there's nothing at this address.</p>
            </LayoutView>
        </CascadingAuthenticationState>
    </NotFound>
</Router>

RedirectToLogin组件

@using Microsoft.AspNetCore.Components.Authorization
@using OfficeManagerApp.Areas.Services.Implementations
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject NavigationManager NavigationManager


<div class="loader loader-bouncing"><span>Redirecting...</span></div>

@code {
    
    protected override async Task OnInitializedAsync()
    {
        await ((ExternalAuthStateProvider)AuthenticationStateProvider)
            .LogInAsync();            

        NavigationManager.NavigateTo("/", forceLoad: true);
    }
}

调试信息

在ExternalAuthStateProvider类中设置断点查看ClaimsPrincipal状态:
ClaimsPrincipal断点截图

参考情况

参考了微软官方的MAUI身份验证教程以及Blazor Hybrid安全教程,但仅使用PlatformService相关方案时仍存在相同问题

内容的提问来源于stack exchange,提问作者MarchalPT

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 18:43:12