【发布时间】:2020-06-23 10:31:08
【问题描述】:
如何在 asp.net core 3.1 中为每种类型的请求启用 Cors?
我正在关注MS Docs,但它们似乎不起作用。
注意:我能够为特定域实现 cors,但不能针对每个域。例如,我已将以下配置应用于我的项目:
-
在我添加的
ConfigureServices();方法中:services.AddCors();
-
在我添加的
Configure()方法中:app.UseCors(builder => { 建设者 .WithOrigins() .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials(); });
但是当我尝试使用 jQuery 从另一个 url 访问 API 时,我得到:
从源“https://localhost:44361”访问“https://localhost:44314/Reservation”处的 XMLHttpRequest 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:没有“Access-Control-Allow-Origin”标头出现在请求的资源上。
现在,如果我将 URL 放入 WithOrigins() 方法中,我就可以访问 API:
app.UseCors(builder =>
{
builder
.WithOrigins("https://localhost:44361")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
我的问题是如何不受限制地授予对每个 URL(即域、子域等)的 API 访问权限。应用 * 不起作用。
请帮忙。
【问题讨论】:
标签: c# asp.net-core cors asp.net-core-3.0 asp.net-core-3.1