簡體   English   中英

ASP.NET Core 中的 AzureAD B2C ROPC 身份驗證

[英]AzureAD B2C ROPC Authentication in ASP.NET Core

我有一個使用 AzureB2C 進行身份驗證的 ASP.NET Core WebApplication (REST API)。 這是 Startup.cs

    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddAuthentication(AzureADB2CDefaults.BearerAuthenticationScheme)
                .AddAzureADB2CBearer(options =>
                {
                    options.Instance = "https://tenant.b2clogin.com/tfp/";
                    options.ClientId = "...";
                    options.Domain = "tenant.onmicrosoft.com";
                    options.SignUpSignInPolicyId = "B2C_1A_signup_signin";
                });
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();
            app.UseAuthentication();
            app.UseAuthorization();
            app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
        }
    }

為了在自動測試中支持身份驗證,我確實嘗試使用client credential流,但發現它在 AzureB2C 中不受支持 接下來我遇到了建議作為替代方案的ROPC Flow (例如: 這里)。 但我無法讓它工作。

如果我使用 ROPC 流程收到令牌。

            dynamic result = await $"https://{tenantName}.b2clogin.com"
                .AppendPathSegment($"{tenantName}.onmicrosoft.com")
                .AppendPathSegment("B2C_1_ROPC_Auth")
                .AppendPathSegment("oauth2/v2.0/token")
                .SetQueryParams(new
                    {
                        client_id,
                        scope = $"6d1aa76b-10fb-47bb-bbec-5d7f7a4e08c4 openid offline_access",
                        username,
                        password,
                        grant_type = "password",
                        repsonse_type = "token id_token"
                    }
                )
                .PostAsync(new StringContent(""))
                .ReceiveJson();

令牌看起來不錯且有效。 但是,如果我嘗試使用它來驗證對 REST Api 的調用,則會收到以下錯誤:

WWW-Authenticate Bearer error="invalid_token", error_description="未找到簽名密鑰"

經過一些調查,看起來 RestAPI 正在嘗試使用我的B2C_1A_signup_signin策略的眾所周知的配置來驗證 KID。 這是有道理的,因為它是AddAzureADB2CBearer的選項的AddAzureADB2CBearer 如果我確實將策略更改為B2C_1_ROPC_Auth ,則接受令牌。

但我確實需要兩個令牌才能工作,即使用B2C_1A_signup_signin Flow 和ROPC流令牌創建的令牌。

如何配置我的應用程序以接受這兩種配置?

如果僅通過無頭身份驗證訪問此 API,則使用 Azure AD 客戶端憑據對其進行保護。 您的 Azure AD B2C 中包含一個 Azure AD。 按照此處的說明操作: https : //docs.microsoft.com/en-us/azure/active-directory/develop/scenario-daemon-overview

暫無
暫無

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

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