即使设置为允许,COR都会失败

发布于 2025-02-06 21:00:06 字数 1564 浏览 2 评论 0原文

我正在运行genhttp已设置为具有CORS允许的服务器(因此允许全部):

var inline = Inline.Create();
inline.Add(CorsPolicy.Permissive());

由于某些原因,请求仍然因COR而失败。对于上下文http:// localhost:55409是服务器,http:// localhost:55309是客户端。

选项(前飞行):

Request URL: http://localhost:55409/api/v1//metadata
Request Method: OPTIONS
Status Code: 204 No Content
Remote Address: [::1]:55409
Referrer Policy: strict-origin-when-cross-origin
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: *
Access-Control-Allow-Methods: *
Access-Control-Allow-Origin: http://localhost:55309
Access-Control-Expose-Headers: *
Access-Control-Max-Age: 86400
Connection: Keep-Alive
Content-Length: 0
Date: Mon, 13 Jun 2022 13:05:34 GMT
Server: GenHTTP/6.3.4.0
Vary: Origin

这将返回204 OK。

get:

Request URL: http://localhost:55409/api/v1//metadata
Referrer Policy: strict-origin-when-cross-origin
Provisional headers are shown
Learn more
Accept: application/json, text/plain, */*
Authorization: Bearer <token>
Referer: http://localhost:55309/
sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="102", "Google Chrome";v="102"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36

返回cors错误

,这也感觉很奇怪,因为没有授权标题返回200 OK,也很奇怪。选项似乎是正确的,因为它允许一切。那为什么失败呢?

另请注意,这与其他服务器(运行.net Web API>)一起使用,因此客户端不怪。我想我缺少一些关键的服务器设置

I'm running GenHTTP server that is setup to to have CORS permissive (so allow all):

var inline = Inline.Create();
inline.Add(CorsPolicy.Permissive());

For some reason the request still fails by CORS. For context http://localhost:55409 is the server and http://localhost:55309 is the client.

Options (preflight):

Request URL: http://localhost:55409/api/v1//metadata
Request Method: OPTIONS
Status Code: 204 No Content
Remote Address: [::1]:55409
Referrer Policy: strict-origin-when-cross-origin
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: *
Access-Control-Allow-Methods: *
Access-Control-Allow-Origin: http://localhost:55309
Access-Control-Expose-Headers: *
Access-Control-Max-Age: 86400
Connection: Keep-Alive
Content-Length: 0
Date: Mon, 13 Jun 2022 13:05:34 GMT
Server: GenHTTP/6.3.4.0
Vary: Origin

This returns 204 OK.

Get:

Request URL: http://localhost:55409/api/v1//metadata
Referrer Policy: strict-origin-when-cross-origin
Provisional headers are shown
Learn more
Accept: application/json, text/plain, */*
Authorization: Bearer <token>
Referer: http://localhost:55309/
sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="102", "Google Chrome";v="102"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36

Returns CORS error

This also feels weird as similar requests without Authorization header return 200 OK. The options seem to be correct in that it allows everything. Why does it fail then?

Also note that this works with the other server (more proper server running .Net Web Api), so client is not to blame. I think I'm missing some crucial server setup

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奶茶白久 2025-02-13 21:00:06

原来,我需要专门配置它以允许授权标题,就好像*不包括它吗?

inline.Add(new CorsPolicyBuilder().Default(new OriginPolicy(null, new List<string>() { "authorization" }, null, AllowCredentials: true, 86400u)));

这是答案,但是我将标记其他答案,任何人都可以解释为什么*不包括授权作为options 已返回,

Access-Control-Allow-Headers: *

所以我假设应该允许授权标题

Turns out I needed to specifically configure it to allow Authorization header, as if * does not include it?

inline.Add(new CorsPolicyBuilder().Default(new OriginPolicy(null, new List<string>() { "authorization" }, null, AllowCredentials: true, 86400u)));

This is the answer, but I'll mark the other answer whoever can explain why * does not include Authorization as OPTIONS has returned this

Access-Control-Allow-Headers: *

so I assume that should allow Authorization header as well

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文