【问题标题】:Identity Server 4 CORS not configuredIdentity Server 4 CORS 未配置
【发布时间】:2019-04-26 05:13:59
【问题描述】:

我已将我的 IS4 放在服务器上,我们称之为http://test.myis4.com。现在我正在尝试使用针对它的 Angular 应用程序登录,但是我一直被 CORS 阻止。 Angular 客户端由 MVC Core 应用程序提供服务,它在http://localhost:5002 端口上运行。我试图通过在该端口上运行它来从我的本地主机执行此操作。

在 IS4 服务器上,我尝试在客户端上添加以下内容:

new Client {
    // code is omitted
    AllowedCorsOrigins = new List<string>() { "http://localhost:5002" }
}

上面我仍然得到CORS错误,然后我尝试将它添加到服务直接:

var cors = new DefaultCorsPolicyService(null)
{
    AllowedOrigins = { "http://localhost:5002" }
};
services.AddSingleton<ICorsPolicyService>(cors);
services.AddIdentityServer()
//rest omitted

上面仍然给了我同样的 CORS 错误,我最后一次尝试是通过应用程序构建器添加它:

app.UseCors(builder => builder.WithOrigins("http://localhost:5002").AllowAnyHeader());
app.UseIdentityServer();
//rest omitted

最后一次尝试仍然没有成功。我不确定我在这一点上做错了什么。有什么建议吗?

【问题讨论】:

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


    【解决方案1】:

    在您展示的第二个代码块上 - 您需要首先 AddIdentityServer() 然后添加 Cors Policy 服务,您也可以在将身份服务器添加到服务后直接执行此操作。比如:

    services.AddIdentityServer(options =>
            {
                // .. if you need something in general (can go without the options)
            })
            .AddCorsPolicyService<YOUR_IMPLEMENTATION_OF_ICorsPolicyService>()
            // rest omitted
    

    这是因为 AddIdentitiyServer() 覆盖了 CORS 服务的添加 - Code

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-17
      • 2018-01-19
      • 2013-12-13
      • 2019-09-20
      • 2021-02-05
      • 2020-11-16
      • 1970-01-01
      相关资源
      最近更新 更多