【发布时间】:2021-10-02 23:34:39
【问题描述】:
我已将以下内容添加到我的服务配置中。
services.AddCors(options
=> options.AddDefaultPolicy(builder
=> builder
//.AllowAnyHeader()
.WithHeaders("header-that-nobody-knows")
.WithOrigins("http://localhost:44304")));
我的期望是调用会反弹(因为我没有将 header-that-nobody-knows 添加到我的标题中)。但是,执行请求时就像设置了AllowAnyHeader()。
在WithOrigins() 中操作端口、域或协议会产生预期的结果,因此配置似乎已正确连接。我怀疑这是一种特殊情况,因为在涉及 GET 和 POST 时,WithMetod() 出现意外行为(而其他方法被阻止/允许取决于参数通过)。
检查 MSDN 并没有给出任何解释。
我怀疑它是否重要,但为了完整起见,这里是调用调用的 Angular 代码。
let url = "https://localhost:44301/security/corstest?input=blobb";
this.http.get<any>(url).subscribe(
next => console.log("next", next),
err => console.warn("err", err));
动作方法如下所示。
[HttpGet("corstest")]
public IActionResult CorsTest([FromQuery] string input)
{
return Ok(new { data = input + " indeed..." });
}
【问题讨论】:
标签: c# .net-core cors .net-core-3.1