Firefox无法读取获取响应头

时间:2019-06-12 13:48:42

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

我正在尝试使用提取API在我的应用程序中发送请求,并且如果收到401 Unauthorized响应,我想读取WWW-Authenticate响应标头的值。这在Chrome上可以正常运行,但是在Firefox上,即使我的回复的WWW-Authenticate标头中包含了Access-Control-Expose-Headers标头,我也看不到。

我的代码:

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无法读取该响应标头吗?

1 个答案:

答案 0 :(得分:2)

存在一个已知错误,其中包含多个WWW-Authenticate响应头,您可能会遇到以下问题:https://bugzilla.mozilla.org/show_bug.cgi?id=1491010