IdentityServer 4 中的 CORS 问题
CORS issue in IdentityServer 4
我正在使用 IdentityServer 4 作为我的应用程序 (Reactjs) 的 oauth 我是 运行 端口 http://localhost:5000 上的 Identityserver 和 http://localhost:3000 上的 reactjs 应用程序.我尝试使用以下代码将 CORS 用于我的 identityserver4。
public void ConfigureServices(IServiceCollection services)
{
services.AddIdentityServer(options =>
{
options.Events.RaiseSuccessEvents = true;
options.Events.RaiseFailureEvents = true;
options.Events.RaiseErrorEvents = true;
})
.AddClientStore<ClientStore>()
//.AddInMemoryApiResources(Config.GetApiResources())
.AddResourceStore<ResourceStore>()
//.AddInMemoryClients(Config.GetClients())
.AddCustomUserStore()
.AddCertificateFromFile();
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
builder => builder.WithOrigins( "http://localhost:3000/")
.AllowAnyMethod()
.AllowAnyHeader());
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment environment)
{
app.UseForwardedHeaders();
if (environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseCors("CorsPolicy");
//app.UseCors("default");
app.UseIdentityServer();
app.UseStaticFiles();
// uncomment, if you want to add an MVC-based UI
app.UseMvcWithDefaultRoute();
}
}
即使我在 WithOrigins() 中添加了 localhost:3000,当我尝试使用 axios 从 React 应用程序发出请求时,我收到以下被阻止的错误。
谁能帮我知道我哪里做错了。我需要我的应用程序只允许一些来源列表(应用程序)
谢谢
这可能是因为尾部斜杠,the documentation 中提到了这一点。
Note: The specified URL must not contain a trailing slash (/). If the URL terminates with /, the comparison returns false and no header is returned.
尝试 http://localhost:3000
而不是 http://localhost:3000/
。
我还会质疑 .AllowAnyOrigin()
和 .WithOrigins()
的用法。您希望实现的目标应该仅使用 .WithOrigins()
.
如果您要向另一个域发送请求,请尝试从您的身份服务器而不是 react.js 应用程序发送 http 请求。我遇到了类似的问题,但我只是使用我的 API 作为代理并且它工作正常。
我正在使用 IdentityServer 4 作为我的应用程序 (Reactjs) 的 oauth 我是 运行 端口 http://localhost:5000 上的 Identityserver 和 http://localhost:3000 上的 reactjs 应用程序.我尝试使用以下代码将 CORS 用于我的 identityserver4。
public void ConfigureServices(IServiceCollection services)
{
services.AddIdentityServer(options =>
{
options.Events.RaiseSuccessEvents = true;
options.Events.RaiseFailureEvents = true;
options.Events.RaiseErrorEvents = true;
})
.AddClientStore<ClientStore>()
//.AddInMemoryApiResources(Config.GetApiResources())
.AddResourceStore<ResourceStore>()
//.AddInMemoryClients(Config.GetClients())
.AddCustomUserStore()
.AddCertificateFromFile();
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
builder => builder.WithOrigins( "http://localhost:3000/")
.AllowAnyMethod()
.AllowAnyHeader());
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment environment)
{
app.UseForwardedHeaders();
if (environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseCors("CorsPolicy");
//app.UseCors("default");
app.UseIdentityServer();
app.UseStaticFiles();
// uncomment, if you want to add an MVC-based UI
app.UseMvcWithDefaultRoute();
}
}
即使我在 WithOrigins() 中添加了 localhost:3000,当我尝试使用 axios 从 React 应用程序发出请求时,我收到以下被阻止的错误。
谁能帮我知道我哪里做错了。我需要我的应用程序只允许一些来源列表(应用程序) 谢谢
这可能是因为尾部斜杠,the documentation 中提到了这一点。
Note: The specified URL must not contain a trailing slash (/). If the URL terminates with /, the comparison returns false and no header is returned.
尝试 http://localhost:3000
而不是 http://localhost:3000/
。
我还会质疑 .AllowAnyOrigin()
和 .WithOrigins()
的用法。您希望实现的目标应该仅使用 .WithOrigins()
.
如果您要向另一个域发送请求,请尝试从您的身份服务器而不是 react.js 应用程序发送 http 请求。我遇到了类似的问题,但我只是使用我的 API 作为代理并且它工作正常。