首页 > 解决方案 > Chrome 90 在 CORS 预检中向 Angular localhost 应用添加 https

问题描述

我在 Google Chrome 90 中检测到异常行为。

我有一个以纯 http 形式提供的 Angular 应用程序(开发端口 4200),因为它只是一个本地环境。后端是一个在 Kestrel 的 5001 端口上运行 https 的 .NET Core 5 应用程序。

今天我所有的OPTIONS预检请求都开始失败。

我发现 Chrome 使用而不是发出预检请求,但由于 .NET 后端不允许在 https 中使用https://localhost:4200localhosthttp而失败。

当我找到 Preflight 请求时,我复制了它的 Powershell 等效项,即

Invoke-WebRequest -Uri "https://localhost:5001/api/v1/secure/xxxx" `
-Method "OPTIONS" `
-Headers @{
"Accept"="*/*"
  "Access-Control-Request-Method"="GET"
  "Access-Control-Request-Headers"="authorization"
  "Origin"="https://localhost:4200"
  "User-Agent"="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36"
  "Sec-Fetch-Mode"="cors"
  "Sec-Fetch-Site"="same-site"
  "Sec-Fetch-Dest"="empty"
  "Referer"="https://localhost:4200/"
  "Accept-Encoding"="gzip, deflate, br"
}

响应为空白204 No Content。如果我修改此脚本以https://用作源,则响应包含所需的 CORS 标头。

该应用程序在 http 中提供。我的浏览器栏上有Insecure警告,但它很好。

除了解决它,我该如何解决这个问题?这可能是 Chromium 中的错误吗?

标签: google-chromecors

解决方案


绕道而行

在后端添加https://localhost:4200为 CORS 源,但这可能意味着根据 YMMV 重新编译/重新加载/重新部署


推荐阅读