簡體   English   中英

Blazor WASM 未重定向到登錄頁面

[英]Blazor WASM Not Redirecting to Login Page

我是 Blazor 的新手,並嘗試使用 oidc 進行身份驗證。 我正在嘗試使用Secure an ASP.NET Core Blazor WebAssembly 獨立應用程序中的說明進行設置。

這是我希望應用程序執行的操作(我認為這是一個“正常”的應用程序流程):

  1. 啟動應用
  2. 被重定向到登錄頁面(此登錄頁面不在我的應用程序中)
  3. 登錄后重定向回應用程序

我一定錯過了一些簡單的東西。 當我嘗試運行該應用程序時,我在瀏覽器控制台錯誤中看到此錯誤:

拒絕在框架中顯示“https://xxxx.yyyy.com/”,因為它將“X-Frame-Options”設置為“拒絕”。

在響應標頭中,我看到了這一點:

X-Frame-選項:拒絕

X-MS 轉發狀態代碼:500

在請求標頭中:

Sec-Fetch-Dest:iframe Sec-Fetch-Mode:導航 Sec-Fetch-Site:跨站點

我正在嘗試模擬現有的、正在工作的 Angular 應用程序,並且在該應用程序標頭中,我在 header 中看到了這一點

Sec-Fetch-Dest:文檔

Sec-Fetch-Mode:導航

Sec-Fetch-Site:跨站點

我不確定 header 的差異是否是差異,但這是我唯一看到的。 正在尋找有關如何使此登錄流程正常工作的建議?

您必須將此代碼添加到您的App.razor以在用戶未通過身份驗證時重定向到登錄。

<Router AppAssembly="@typeof(Program).Assembly" PreferExactMatches="@true">
    <Found Context="routeData">
        <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
            <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>Sorry, there's nothing at this address.</p>
        </LayoutView>
    </NotFound>
</Router>

RedirectToLogin組件是這樣的:

@inject NavigationManager Navigation
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@code {
    protected override void OnInitialized()
    {
        Navigation.NavigateTo(
            $"authentication/login?returnUrl={Uri.EscapeDataString(Navigation.Uri)}");
    }
}

事實證明我的場景 - Blazor Webassembly 到 ADFS 不受支持。 經過幾天的努力,我發現了這個: BLAZOR WASM - ADFS - OIDC -表明這不是受支持的方案的文檔。

然后我發現文章指出了正確的方向Configure Blazor WASM with ADFS

事實證明 MSAL 提供程序似乎適用於這種情況:

builder.Services.AddMsalAuthentication(options => {
// Configure your authentication provider options here.
options.ProviderOptions.Authentication.Authority = "https://xxx.yyyy.com/adfs";
options.ProviderOptions.Authentication.ClientId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx";
options.ProviderOptions.Authentication.NavigateToLoginRequestUrl = true;
options.ProviderOptions.LoginMode = "redirect";

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM