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状态:
参考情况
参考了微软官方的MAUI身份验证教程以及Blazor Hybrid安全教程,但仅使用PlatformService相关方案时仍存在相同问题
内容的提问来源于stack exchange,提问作者MarchalPT
相关产品推荐
相关产品推荐

