【问题标题】:Example of fetch() method to API with Azure AD B2C authorization具有 Azure AD B2C 授权的 API 的 fetch() 方法示例
【发布时间】:2019-10-21 17:46:34
【问题描述】:

我正在寻找 调用安全 API 的 fetch() 方法示例(我使用 Azure AD B2C)

具体来说,我不知道我的标题应该是什么样子。

使用具有授权的 Postman 调用 API 有效。 我的 API 托管在 localhost:44320 我没有部署它。

在 React 中,我使用 'react-azure-adb2c' 库,它也可以工作。我可以登录,然后我将获得包含我需要的所有声明的令牌。

var token = auth.getToken(); //here is the token which is correct
fetch("https://localhost:44320/api/worker/", {
      method: "GET",
      headers: {
        Authorization: token,
        Accept: "application/json",
        Host: "localhost:44320"
      }
    })
      .then(res => res.json())
      .then(json => this.setState({ listOfWorkers: json.results }));
  }

【问题讨论】:

    标签: javascript reactjs azure azure-active-directory fetch-api


    【解决方案1】:

    您将标头指定为Authorization: Bearer token-value-here。

    所以 Authorization: 'Bearer ' + token 在你的情况下。

    【讨论】:

    • 我做到了,但仍然出现错误:Access to fetch at 'https://localhost:44320/api/worker/' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
    • 这是一个与 CORS 相关的单独问题
    • 非常感谢你这么好,回答了我的问题。我会搜索CORS,但是如果我可以调用没有授权的API(我将授权标头注释掉),这是否意味着CORS策略一切正常?
    • 是的...或者可能是 CORS 策略不允许 Authorization 标头
    猜你喜欢
    • 2017-01-04
    • 2020-04-10
    • 2017-04-20
    • 1970-01-01
    • 2020-05-09
    • 2016-06-29
    • 2020-04-21
    • 2019-12-10
    • 1970-01-01
    相关资源
    最近更新 更多