Asp.net web api IdentityServer4 CORS问题

Asp.net web api IdentityServer4 CORS问题,asp.net-web-api,cors,identityserver4,.net-5,Asp.net Web Api,Cors,Identityserver4,.net 5,我们已经为使用IdentityServer4作为身份验证提供程序的.NET5WebAPI设置了CORS 从我们的JS客户端(Ionic/Capactor)到例如/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.16

我们已经为使用IdentityServer4作为身份验证提供程序的.NET5WebAPI设置了CORS

从我们的JS客户端(Ionic/Capactor)到例如/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"
}
我们还在IDS4文档中尝试了“添加更多API端点”条目,但这似乎只是为了授权对这些端点的请求,我们已经自己设置好了


有人知道发生了什么吗?

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

  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");

您必须显示所有启动代码,因为Cors代码应按特殊顺序排列。@Sergey AddCors()是ConfigureServices的第一条语句。在UseIdentityServer()之前调用UseCors()。是否至少显示配置部分。这里有很多规则。@Sergey没有太多,但我为你添加了它。我不确定为什么我的答案被删除了,它包含了完整的修复。但是我会再把它贴在这里。。添加命名策略并将源添加到AllowedCorsOrigins属性后,该策略已修复。谢谢
  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");