【问题标题】:AzureAD B2C ROPC Authentication in ASP.NET CoreASP.NET Core 中的 AzureAD B2C ROPC 身份验证
【发布时间】:2020-10-30 14:44:47
【问题描述】:

我有一个使用 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 Flow,但发现it is not supported in AzureB2C。接下来我遇到了ROPC Flow,建议作为替代方案(例如:here)。但我无法让它工作。

如果我使用 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-认证 Bearer error="invalid_token", error_description="找不到签名密钥"

经过一番调查,看起来 RestAPI 正在尝试使用我的 B2C_1A_signup_signin 策略的众所周知的配置来验证 KID。这是有道理的,因为它是AddAzureADB2CBearer 选项的一部分。如果我将 Policy 更改为 B2C_1_ROPC_Auth,则接受令牌。

但我确实需要两个令牌才能工作,即使用B2C_1A_signup_signin Flow 和 ROPC flow 令牌创建的令牌。

如何配置我的应用以接受这两种配置?

【问题讨论】:

    标签: c# asp.net-core jwt openid azure-ad-b2c


    【解决方案1】:

    如果仅通过无头身份验证访问此 API,则使用 Azure AD 客户端凭据对其进行保护。您的 Azure AD B2C 内部包含一个 Azure AD。请按照此处的说明进行操作: https://docs.microsoft.com/en-us/azure/active-directory/develop/scenario-daemon-overview

    【讨论】:

    • @jas-suri-msft 否 该应用程序不仅通过无头身份验证访问。大多数时候使用用户交互来访问它。我只需要集成测试的无头身份验证。
    • 看我的回答here
    猜你喜欢
    • 1970-01-01
    • 2021-03-13
    • 2021-04-09
    • 1970-01-01
    • 2019-01-14
    • 1970-01-01
    • 1970-01-01
    • 2017-03-30
    • 2018-06-25
    相关资源
    最近更新 更多