簡體   English   中英

Blazor Wasm.Net 5 - 實現個人用戶帳戶和 Azure AD 身份驗證

[英]Blazor Wasm .Net 5 - Implement both Individual User Accounts and Azure AD Authentication

下面的 Microsoft 文檔說明了如何使用兩種不同的身份驗證方法保護 Blazor WASM 托管應用程序。

1.個人用戶JWT授權(IdentityServer)

2. Azure AD認證

需要通過在單個應用程序中結合兩種身份驗證機制來為最終用戶提供兩種選擇。 用戶應該能夠從登錄頁面中選擇其中一個選項。

Azure AD 選項只是為最終用戶提供 SSO 體驗,除此之外,所有授權邏輯都將使用個人用戶帳戶在本地處理。

一旦使用 Azure Adoption 對用戶進行身份驗證,應該有一種方法可以將用戶與本地 ID 聯系起來以處理授權邏輯等。

我做了很多在線研究,但我找不到實現這一點的指南或教程。 我試圖通過組合代碼來實現這一點,但我堅持:

  1. 在 Blazor 客戶端登錄頁面中啟用 Local/AzureAd 登錄選項
  2. 將 Azure AD 用戶與服務器中的本地用戶鏈接

Blazor 客戶端代碼

public class Program
{
    public static async Task Main(string[] args)
    {
        var builder = WebAssemblyHostBuilder.CreateDefault(args);
        builder.RootComponents.Add<App>("#app");
        builder.Services.AddHttpClient("BlazorWasmIndvAuth.ServerAPI", client => client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress))
            .AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();
        builder.Services.AddScoped(sp => sp.GetRequiredService<IHttpClientFactory>().CreateClient("BlazorWasmIndvAuth.ServerAPI"));


        //OPTION 1
        //Azure Ad Authentication
        builder.Services.AddMsalAuthentication(options =>
        {
            builder.Configuration.Bind("AzureAd", options.ProviderOptions.Authentication);
            options.ProviderOptions.DefaultAccessTokenScopes.Add("api://123456/Api.Access");
        });

        //OPTION 2
        //Individual User JWT authentication
        builder.Services.AddApiAuthorization();

        await builder.Build().RunAsync();
    }
}

快速的答案是您的客戶端應用程序和 API 更喜歡只需要處理一個授權服務器。 支持多個將非常混亂。

我的建議是您的應用程序只處理 IdentityServer,但允許通過 IdentityServer 登錄到 Azure AD(聯合身份驗證)。 請參閱 IdentityServer 中 QuickStartUI 中的 ExternalController.cs 文件。

看到這個頁面

暫無
暫無

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

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