【问题标题】:IdentityServer4 CORS issueIdentityServer4 CORS 问题
【发布时间】:2021-05-22 08:41:20
【问题描述】:

我们为使用 IdentityServer4 作为身份验证提供程序的 .NET 5 Web API 设置了 CORS。

从我们的 JS 客户端(Ionic/Capacitor)到例如 /account/register 的 HTTP 调用正常工作,但是当我们调用我们的“自定义”API 方法之一时,我们会收到以下错误消息。

2021-02-19 10:38:10.092 +00:00 [DBG] CORS request made for path: /connect/userinfo from origin: http://192.168.1.152:8100
2021-02-19 10:38:10.099 +00:00 [DBG] Client list checked and origin: http://192.168.1.152:8100 is allowed
2021-02-19 10:38:10.101 +00:00 [DBG] CorsPolicyService allowed origin: http://192.168.1.152:8100
2021-02-19 10:38:10.179 +00:00 [DBG] CORS request made for path: /api/schedule from origin: http://192.168.1.152:8100 but was ignored because path was not for an allowed IdentityServer CORS endpoint

我们搜索了这个问题,但我们似乎唯一找到的是如何为 IDS4 或 .NET 5 配置 CORS。我们的 CORS 设置是这样的。

public void ConfigureServices(IServiceCollection services)
{
    services.AddCors(options =>
    {
        options.AddDefaultPolicy(builder =>
            {
                builder.WithOrigins("http://192.168.1.152:8100", "capacitor://localhost", "https://localhost:5001")
                .AllowAnyHeader()
                .AllowAnyMethod();
            });
    });

    // ...

    services.AddIdentityServer();

    // ...
}

之后,我们还尝试像这样在适当的客户端上设置 AllowedCorsOrigins 属性。但没有运气。

AllowedCorsOrigins = new List<string>
{
    "http://192.168.1.152:8100", 
    "capacitor://localhost"
}

配置设置是这样完成的,我们尝试切换 UseCors() 和 UseIdentityServer() 但这没有帮助。

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IMapper mapper)
{
    mapper.ConfigurationProvider.AssertConfigurationIsValid();

    app.UseRouting();
    app.UseStaticFiles();

    app.UseCors();
    app.UseIdentityServer();
    
    app.UseAuthentication();
    app.UseAuthorization();

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Error");

        app.UseHsts();
    }

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapDefaultControllerRoute();
        endpoints.MapControllers();
    });
}

我们还尝试了 IDS4 文档中的“添加更多 API 端点”条目,但这似乎仅用于授权对这些端点的请求,我们已经自行设置了。

有人知道发生了什么吗?

【问题讨论】:

  • 你必须显示所有启动代码,因为 Cors 代码应该按特殊顺序放置。
  • @Sergey AddCors() 是 ConfigureServices 的第一条语句。 UseCors() 在 UseIdentityServer() 之前调用。
  • 你能否至少显示配置部分。那里有很多规则。
  • @Sergey 不多,但我为你添加了。

标签: c# .net-core asp.net-web-api cors identityserver4


【解决方案1】:

尝试使用命名策略而不是默认策略。有时效果更好:

  services.AddCors(o => o.AddPolicy("AllowSpecificOrigins", builder =>
            {
              builder.WithOrigins("http://192.168.1.152:8100", "capacitor://localhost", "https://localhost:5001")
                .AllowAnyHeader()
                .AllowAnyMethod();
        });
});

......
......

app.UseCors("AllowSpecificOrigins");

【讨论】:

  • 不太确定为什么我的答案被删除,它包含完整的修复。但我会再次在这里发布.. 添加命名策略并将来源添加到 AllowedCorsOrigins 属性后,它已修复。谢谢。
猜你喜欢
  • 1970-01-01
  • 2020-12-16
  • 2021-06-22
  • 2023-03-25
  • 2020-09-12
  • 2018-12-08
  • 2021-12-30
  • 2021-03-11
  • 2019-03-10
相关资源
最近更新 更多