【发布时间】:2022-01-17 23:13:24
【问题描述】:
我正在尝试获取一个 API 以允许来自源的请求。到目前为止,什么都没有通过,所以现在允许任何来源是目标,一旦它工作,我可以收紧访问。
API 用 ASP.net 编写并部署到弹性 beanstalk。当我尝试使用任何前端访问 API 时,我收到以下错误:
从源“http://localhost:{PORT_NUM}”访问“https://api.website.com/endpoint”处的 XMLHttpRequest 已被 CORS 策略阻止:否“Access-Control-Allow-Origin”请求的资源上存在标头。
Startup.cs 中的 Cors 配置:
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddCors();
// ...
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseCors(x => x
.AllowAnyMethod()
.AllowAnyHeader()
.SetIsOriginAllowed(origin => true));
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
端点并没有以任何特殊方式进行图形化:
[AllowAnonymous]
[HttpPost]
public async Task<ActionResult> Create([FromBody]CreateRequest request)
{
//...
}
我有什么遗漏的吗?
【问题讨论】:
-
可能发生的情况是,在管道到达 CORS 中间件之前,您的管道中有另一个错误 - CORS 错误可能掩盖了其他内容。只是一个理论。
标签: c# amazon-web-services asp.net-core cors amazon-elastic-beanstalk