Azure AD-OAuth 2-隐式授予-尝试使用应用进行身份验证时出现CORS错误,但当我从浏览器访问URL时,效果很好

问题描述 投票:0回答:1

我正在为我要保护的非常简单的Web应用程序实现OAuth2隐式授予流程。我正在使用MSAL来做到这一点。我遇到的问题是,当我尝试从浏览器访问URL时,它会将我带到组织登录页面,然后将我带到重定向URI。

但是,当我从应用程序调用它时,出现了CORS错误。堆栈跟踪如下所示:>

Access to XMLHttpRequest at '**********/oauth2/authorize/.well-known/openid-configuration' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
index.js:99 ClientAuthError: Error: could not resolve endpoints. Please check network and try again. Details: function toString() { [native code] }
    at ClientAuthError.AuthError [as constructor] (webpack:///./node_modules/msal/lib-es6/error/AuthError.js?:26:28)
    at new ClientAuthError (webpack:///./node_modules/msal/lib-es6/error/ClientAuthError.js?:111:28)
    at Function.ClientAuthError.createEndpointResolutionError (webpack:///./node_modules/msal/lib-es6/error/ClientAuthError.js?:121:16)
    at eval (webpack:///./node_modules/msal/lib-es6/UserAgentApplication.js?:455:125)
XHRClient.js:43 GET ***********/authorize/.well-known/openid-configuration net::

我的代码如下所示

import $ from "jquery";
import 'bootstrap';
import * as Msal from "msal";

window.addEventListener("load", (event) => {
   const msalConfig = {
      auth: {
        clientId: “***************”,
        authority: "https://login.microsoftonline.com/<tenant-id>/oauth2/v2.0/authorize"
      },
      cache: {
        cacheLocation: "sessionStorage", // This configures where your cache will be stored
        storeAuthStateInCookie: false, // Set this to "true" if you are having issues on IE11 or Edge
        forceRefresh: false // Set this to "true" to skip a cached token and go to the server to get a new
      }
    };

    const loginRequest = {
      scopes: ["openid"],
    };


    console.log(new Msal.UserAgentApplication(msalConfig));

    const myMSALObj = new Msal.UserAgentApplication(msalConfig); 

    myMSALObj.handleRedirectCallback(authRedirectCallBack);

 function authRedirectCallBack(error, response) {
      if (error) {
        console.log(error);
      } else {
       console.log(response);
      }
 }


 myMSALObj.loginPopup(loginRequest)
    .then(loginResponse => {  
        console.log('id_token acquired at: ' + new Date().toString());
        if (myMSALObj.getAccount()) {
          console.log(myMSALObj.getAccount());
          // showWelcomeMessage(myMSALObj.getAccount());
        }
    }).catch(function (error) {
      console.log(error);
    });


}

我正在为我要保护的非常简单的Web应用程序实现OAuth2隐式授予流程。我正在使用MSAL来做到这一点。我遇到的问题是,当我尝试从浏览器访问URL时,...

javascript oauth-2.0 azure-active-directory msal implicit-grant
1个回答
1
投票

此网址看起来很奇怪:

© www.soinside.com 2019 - 2024. All rights reserved.