【发布时间】:2026-01-06 01:00:01
【问题描述】:
我使用 IdentityServer 4 作为我的应用程序的 oauth (Reactjs) 我在端口 http://localhost:5000 上运行 Identityserver,在 http://localhost:3000 上运行 reactjs 应用程序。我已经尝试通过以下代码为我的 identityserver4 使用 CORS。
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 app 发出请求时,我收到以下阻塞错误。
谁能帮我知道我做错了什么。我需要我的应用程序只允许一些来源列表(应用程序) 谢谢
【问题讨论】:
-
您能否发布整个 Startup.cs,因为中间件的顺序也很重要?
-
尝试只使用 AllowAnyOrigin() 而不使用 WithOrigins("localhost:3000/")
-
@zhuber 我已经更新了代码你现在可以看到订单了
-
@foadabd 我只希望 localhost:3000 访问身份服务器,因此 AllowAnyOrigin() 对我不起作用。
-
@p.durgashankar 好的尝试只使用 WithOrigins("localhost:3000/") 没有 AllowAnyOrigin() 并且可能需要 AllowCredentials()
标签: asp.net reactjs asp.net-core cors identityserver4