javascript - Firefox 无法读取获取响应 header

标签 javascript asp.net-web-api fetch-api

我正在尝试使用获取 API 在我的应用程序中发送请求,如果我收到 401 Unauthorized 响应,我想读取 WWW-Authenticate< 的值 响应头。这在 Chrome 上运行良好,但在 Firefox 上,我看不到 WWW-Authenticate header ,即使它包含在 Access-Control-Expose-Headers header 中我的回应。

我的代码:

const api = async (endpoint, fetchOptions) => {
  // fetchOptions:
  // {
  //   "credentials": "same-origin",
  //   "method": "GET",
  //   "headers": {
  //     "Accept": "application/json",
  //     "Content-Type": "application/json"
  //   }
  // }

  const response = await fetch(endpoint, fetchOptions)
    .catch(r => r)
    .then(r => { r.headers.forEach(console.log.bind(console)); return r; });

  // handle 401 errors
  if (!response.status === 401 && response.headers.has('WWW-Authenticate')) {
    const authenticate = response.headers.get('WWW-Authenticate');
    const authEndpoint = authenticate.match(/authorization_endpoint="([^"]+)/i)[1];

    window.location.href = authEndpoint;
    return;
  }
};

我的要求:

GET /api/login HTTP/1.1
Host: localhost:3001
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0
Accept: application/json
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://localhost:3000/
Content-Type: application/json
Origin: http://localhost:3000
Connection: keep-alive

我的回复:

HTTP/1.1 401 Unauthorized
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json
Expires: -1
Server: Microsoft-IIS/10.0
Access-Control-Allow-Origin: http://localhost:3000
Access-Control-Expose-Headers: WWW-Authenticate
WWW-Authenticate: Bearer realm="http://localhost:3001", authorization_endpoint="<oauth endpoint>"
Bearer
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcYXNjaW50ZXJuXFNvdXJjZVxSZXBvc1xQb3J0YWxcQVBJXFNhbXMuV2ViQXBpXGFwaVxsb2dpbg==?=
Date: Wed, 12 Jun 2019 13:37:08 GMT
Content-Length: 128

控制台输出:

no-cache cache-control 
application/json content-type 
-1 expires 
no-cache pragma

有谁知道为什么 Firefox 无法读取该响应 header ?

最佳答案

多个 WWW-Authenticate 响应 header 存在一个已知错误,您可能会遇到:https://bugzilla.mozilla.org/show_bug.cgi?id=1491010 .

关于javascript - Firefox 无法读取获取响应 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56563807/

相关文章:

javascript - 在 PDF 页面上运行 Chrome 插件

c# - 在 ASP.NET Core 中从 ActionExecutionContext 的 HttpContext 中获取 RawBody

asp.net-mvc - REST 服务认证 : where to store user credentials?

reactjs - 批处理钩子(Hook)请求并等待它们在 React 中全部完成

javascript - 使用获取流的可恢复下载器

javascript - 如何在日期之前/之前禁用/启用输入元素?

javascript - jQuery 可以在分发为 "website"的 CD 上运行吗?

javascript - react native 获取()不工作

javascript - 预渲染 : difference between puppeteer/rendetron and prerender. io

java - 使用 gson 将日期从 c# webapi 交换到 java