我有一個在 localhost 上運行的 MAUI 應用程式,當您啟動該應用程式時,它會觸發 Microsoft 登錄,在用戶成功登錄后,我捕獲了 ClaimsPrincipal 并且 IsAuthenticated 始終為 false。根據我的 Main.razor 組件,如果用戶未經過身份驗證,它會再次呼叫 RedirectToLogin 組件,并且我不知道如何在應用程式中授權我的登錄用戶,因此它可能與 IsAuthenticated 為假有關。
我已經看到了一些解決方案,他們說您必須構建 ClaimsPrincipal 將身份驗證型別作為引數傳遞給 ClaimsIdentity ,如下所示:
new ClaimsPrincipal(new Identity("something"))
問題是我已經從 AAD 獲得了 ClaimsPrincipal,所以我不知道我應該做什么,因為我無法看到 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 ><span>Redirecting...</span></div>
@code {
protected override async Task OnInitializedAsync()
{
await ((ExternalAuthStateProvider)AuthenticationStateProvider)
.LogInAsync();
NavigationManager.NavigateTo("/", forceLoad: true);
}
}
這里是我在 ExternalAuthStateProvider 類中看到 CLaimsPrincipal 的斷點:

這是我為此遵循的教程。
平臺服務教程
ExternalAuthStateProvider 教程
我也有一個解決方案,只使用 PlatformService 教程來解決同樣的問題。
uj5u.com熱心網友回復:
過了一會兒,我找到了解決方案!
基本上,您會收到來自 AAD 的 ClaimsPrincipal,但您必須使用來自 AAD ClaimsPrincipal 的宣告在應用程式內創建自己的 ClaimsPrincipal。
在 ExternalAuthStateProvider.cs 的 LoginWithExternalProviderAsync() 方法中,我執行了以下操作:
private async Task<ClaimsPrincipal> LoginWithExternalProviderAsync()
{
var authenticationResult = await _platformService.GetAuthenticationResult();
var authenticatedUser = new ClaimsPrincipal(new ClaimsIdentity(authenticationResult.ClaimsPrincipal.Claims, "Basic"));
return await Task.FromResult(authenticatedUser);
}
你只需要這樣做,然后它就可以作業了!!
額外的 - - - - - -
為了改進流程登錄注銷,我創建了一個 LoginPage.razor:
@page "/login"
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Authorization
@using OfficeManagerApp.Areas.Services.Implementations
@attribute [AllowAnonymous]
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject NavigationManager NavigationManager
<button @onclick="Login">Log in</button>
@code
{
public async Task Login()
{
await ((ExternalAuthStateProvider)AuthenticationStateProvider)
.LogInAsync();
NavigationManager.NavigateTo("/");
}
}
更改了 RedirectToLogin,剃刀:
@inject NavigationManager NavigationManager
<div ><span>Redirecting...</span></div>
@code {
protected override void OnInitialized()
{
NavigationManager.NavigateTo("/login");
}
}
并添加了注銷方法:
private void Logout(){
((ExternalAuthStateProvider)AuthenticationStateProvider)
.Logout();
}
還對我的 Main.razor 做了一些更改:
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(Main).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<Authorizing>
Authorizing...
</Authorizing>
<NotAuthorized>
@if (!context.User.Identity.IsAuthenticated)
{
<RedirectToLogin />
}
else
{
<p>You are not authorized to access this resource.</p>
}
</NotAuthorized>
</AuthorizeRouteView>
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<p role="alert">Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
</CascadingAuthenticationState>
注意不要忘記添加到您的 _Imports.razor:
@using Microsoft.AspNetCore.Components.Authorization
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/523999.html
