Identity Server 4 CORS not configured



我已经把我的IS4放在服务器上,让我们调用它http://test.myis4.com.现在我正试图用一个有角度的应用程序登录,但我一直被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

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

在您显示的第二个代码块上,您需要首先添加AddIdentityServer(),然后添加Cors-Policy服务,也可以在将Identity server添加到服务后直接执行。类似于:

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

这是因为AddIdentitiyServer()覆盖了CORS服务代码的添加。

相关内容

最新更新