gpt4 book ai didi

azure - 尝试使用 B2C 身份验证保护 aspnet core web api 时出现错误 : Unable to retrieve openid config

转载 作者:行者123 更新时间:2023-12-03 02:54:48 24 4
gpt4 key购买 nike

我正在尝试通过使用用户提供的 JWT 不记名 token 针对 Azure B2C 进行身份验证来保护我的 aspnet core Web API 服务器。我遵循了在官方 microsoft github 页面上找到的一些示例代码,但似乎无法使其工作。

在我的 B2C 策略中,我已将其设置为使用默认颁发者 URL 格式:https:////v2.0/

在我的 Web 应用程序中,我在 JWT 选项中指定了与授权机构相同的 URL。

当我向服务器提交 HTTP 请求时,身份服务器代码会失败,因为它尝试联系 B2C 来获取 openid 配置。它失败并出现以下错误...

HttpRequestException: Response status code does not indicate success: 404 (Not Found).
System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()

IOException: IDX20804: Unable to retrieve document from: 'https://innovativelitfoundry.b2clogin.com/0f55bfb6-6af5-4293-8963-29ae099183cc/v2.0/.well-known/openid-configuration'.
Microsoft.IdentityModel.Protocols.HttpDocumentRetriever.GetDocumentAsync(string address, CancellationToken cancel)

InvalidOperationException: IDX20803: Unable to obtain configuration from: 'https://innovativelitfoundry.b2clogin.com/0f55bfb6-6af5-4293-8963-29ae099183cc/v2.0/.well-known/openid-configuration'.
Microsoft.IdentityModel.Protocols.ConfigurationManager<T>.GetConfigurationAsync(CancellationToken cancel)

事实上,该 URL 将不起作用,因为它似乎未在查询字符串中包含来自所使用 token 的策略名称。所以,该 URL 确实不起作用。

但我不确定如何让代码在查询字符串中提供该策略名称?或者它应该自动执行此操作?

这是我的 aspnet core Web api 应用程序中的代码,我在其中配置身份验证设置...

    public void ConfigureServices(IServiceCollection services)
{
IdentityModelEventSource.ShowPII = true;

services
.AddAuthentication(ConfigureAuthentication)
.AddJwtBearer(ConfigureJwt);

services
.AddCors();

services
.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

services
.AddSingleton(Configuration);
}

private void ConfigureAuthentication(AuthenticationOptions options)
{
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
}

private void ConfigureJwt(JwtBearerOptions options)
{
var tenant = Configuration["AzureAd:TenantId"];

options.Audience = Configuration["AzureAd:ApplicationId"];
options.Authority = $"https://innovativelitfoundry.b2clogin.com/{tenant}/v2.0/";
}

有人可能知道我在这里做错了什么吗?如何让我的 aspnet core web api 应用程序正确下拉该 openid 配置文档?

最佳答案

您必须将 options.Authority 设置为包含策略 ID 的授权 URL:

options.Authority = $"https://innovativelitfoundry.b2clogin.com/{tenant}/{policy}/v2.0/";

只要你设置了the issuer claim for all policies to the issuer URL that doesn't contain the policy ID ,那么您的 API 应用程序就可以下载任何策略的配置文档,然后验证为所有策略颁发的 token 。

关于azure - 尝试使用 B2C 身份验证保护 aspnet core web api 时出现错误 : Unable to retrieve openid config,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53529107/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com