代码之家  ›  专栏  ›  技术社区  ›  Ryan

尝试使用B2C身份验证保护aspnet核心web api时出错:无法检索openid配置

  •  1
  • Ryan  · 技术社区  · 7 年前

    我正试图通过使用用户提供的JWT承载令牌对Azure B2C进行身份验证来保护我的aspnet核心web API服务器。我遵循了microsoft github官方页面上的一些示例代码,但似乎无法使其正常工作。

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

    在我的web应用程序中,我在JWT选项中指定了与权限相同的URL。

    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将不起作用,因为它似乎没有在查询字符串中包含所用令牌中的策略名称。因此,该URL确实不起作用。

    下面是代码,在我的aspnet核心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/";
        }
    

    1 回复  |  直到 7 年前
        1
  •  1
  •   Chris Padgett    7 年前

    你必须定下来 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应用程序可以下载任何策略的配置文档,然后验证为所有策略颁发的令牌。