【问题标题】:Azure AD Preflight request not returning dataAzure AD Preflight 请求未返回数据
【发布时间】:2018-11-28 19:33:14
【问题描述】:

我目前正在尝试向https://login.microsoftonline.com/XXX/oauth2/token 端点发送发布请求,以检索应用程序的访问令牌和刷新令牌。使用 axios 向端点发送 post 请求时,preflight 被发送,但没有返回任何响应。

错误:

Response to preflight request doesn't pass access control check: 
No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'http://localhost:3000' is therefore not allowed access.

但是对 axios 发布请求使用不同的方法,它返回数据但没有预检并给出不同的错误:

No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'http://localhost:3000' is therefore not allowed access.

两个 Axios 请求:

const data = new FormData();

 data.append('grant_type', this.config.grant_type); 
 data.append('client_id', this.config.client_id);
 data.append('code', localStorage.getItem('auth_code'));
 data.append('redirect_uri', this.config.redirect_uri);
 data.append('client_secret', this.config.client_secret);
 data.append('resource', this.config.client_id);

axios.post(`https://login.microsoftonline.com/${this.config.tenant}/oauth2/token`, data);

方法二:

  axios({
  method: 'post',
  contentType: 'application/json',
  url: `https://login.microsoftonline.com/${this.config.tenant}/oauth2/token`,
  data: {
    grant_type: this.config.grant_type,
    client_id: this.config.client_id,
    code: localStorage.getItem('auth_code'),
    redirect_uri: this.config.redirect_uri,
    client_secret: this.config.client_secret,
    resource: this.config.client_id
  }
});

这是 axios 请求本身的问题还是端点的问题?

【问题讨论】:

    标签: javascript azure http http-headers azure-active-directory


    【解决方案1】:

    您应该使用隐式授予流程来获取访问令牌。您不能使用包含来自前端 JavaScript 的客户端密码的流程!

    您的客户端密码(也就是您的应用密码)目前对访问您网站的任何人都是公开的!

    不能在前端 JavaScript 中使用客户端密码。

    您需要在应用的清单中启用隐式流,然后在您的应用中使用如下 URL 重定向到 Azure AD:

    https://login.microsoftonline.com/tenant-id-here/oauth2/authorize?client_id=your-client-id&response_type=id_token+token&resource=resource-id-for-api&redirect_uri=your-app-redirect-url
    

    文档:https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-authentication-scenarios#single-page-application-spa

    【讨论】:

      猜你喜欢
      • 2019-01-05
      • 1970-01-01
      • 2012-12-22
      • 2020-06-30
      • 1970-01-01
      • 2023-04-08
      • 2012-12-31
      • 2018-04-13
      • 2018-01-05
      相关资源
      最近更新 更多