具有 Azure AD B2C 授权的 API 的 fetch() 方法示例

Example of fetch() method to API with Azure AD B2C authorization

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

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

使用具有授权的 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 }));
  }

您将 header 指定为 Authorization: Bearer token-value-here

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