Microsoft MSAL.js两次包含domain_hint

我正在使用msal.js,并想使用domain_hint直接进入IdP页面。设置extraQueryParameters: {domain_hint: 'abc'}之后 msal.js确实向查询字符串中添加了domain_hint=xyz,但在域domain_hint=organizations之前也添加了域{,从而导致B2C显示我要跳过的IdP选择页面。

URL

https://xyz.b2clogin.com/xyz.onmicrosoft.com/b2csignupsignin/oauth2/v2.0/authorize?response_type=id_token&scope=https%3A%2F%test.onmicrosoft.com%2Fhelloapi%2Fdemo.read%20openid%20profile&client_id=e3443e90-18bc-4a23-9982-7fd5e67ff339&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2F&state=11eff659-29d9-49af-80db-a7ef5bfe55ee&nonce=daeafcda-5984-468b-8796-1b2655a8599e&client_info=1&x-client-SKU=MSAL.JS&x-client-Ver=1.1.2&login_req=9b8396fa-6441-466d-98da-3efd87ab7d07-b2c_1_primerosignupsignin&domain_req=48e05529-88b8-40e1-825a-18c4e1077b3a&domain_hint=organizations&domain_hint=abc&client-request-id=f2e88cb1-5edb-447f-8fc3-578f69c23b4e&response_mode=fragment

Index.html


<head>
  <title>Calling a Web API as a user authenticated with Msal.js app</title>
  <style>
    .hidden {
      visibility: hidden
    }

    .visible {
      visibility: visible
    }

    .response {
      border: solid;
      border-width: thin;
      background-color: azure;
      padding: 2px;
    }
  </style>
</head>

<body>
  <!-- bluebird only needed if this page needs to run on Internet Explorer -->
  <!-- msal.min.js can be used in the place of msal.js; included msal.js to make debug easy -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/bluebird/3.3.4/bluebird.min.js" class="pre"></script>
  <script src="https://secure.aadcdn.microsoftonline-p.com/lib/1.1.2/js/msal.js"></script>
  <script src="https://code.jquery.com/jquery-3.2.1.min.js" class="pre"></script>

  <h2>Getting an access token with Azure AD B2C and calling a Web API</h2>
  <div>
    <div id="label">Sign-in with microsoft Azure AD B2C</div>
    <button id="auth" onclick="signIn()">Login</button>
    <button id="callApiButton" class="hidden" onclick="callApi()">Call Web API</button>
  </div>

  <pre class="response"></pre>

  <script class="pre">

    // The current application coordinates were pre-registered in a B2C tenant.
    var appConfig = {
      b2cScopes: [""]
    };

  </script>

  <script>
    "use strict";

    // configuration to initialize msal
    const msalConfig = {
        auth: {

            clientId: "e3443e90-18bc-4a23-9982-7fd5e67ff339",//This is your client ID
            authority: "https://xyz.b2clogin.com/xyz.onmicrosoft.com/B2c_SignUpSignIn",//This is your tenant info
            validateAuthority: false
        },cache: {
            cacheLocation: "localStorage",storeAuthStateInCookie: true
        }
    };

    // instantiate MSAL
    const myMSALObj = new Msal.UserAgentApplication(msalConfig);

    // request to signin - returns an idToken
    const loginRequest = {
        scopes: appConfig.b2cScopes,extraQueryParameters: {domain_hint: 'abc'}
    };

    // request to acquire a token for resource access
    const tokenRequest = {
        scopes: appConfig.b2cScopes
    };

    // signin and acquire a token silently with POPUP flow. Fall back in case of failure with silent acquisition to popup
    function signIn() {
        myMSALObj.loginPopup(loginRequest).then(function (loginResponse) {
            getToken(tokenRequest).then(updateUI);
        }).catch(function (error) {
            logMessage(error);
        });
    }

    //acquire a token silently
    function getToken(tokenRequest) {
        return myMSALObj.acquireTokenSilent(tokenRequest).catch(function(error) {
          console.log("aquire token popup");
          // fallback to interaction when silent call fails
          return myMSALObj.acquireTokenPopup(tokenrequest).then(function (tokenResponse) {
          }).catch(function(error){
            logMessage("Failed token acquisition",error);
        });
      });
    }

    // updates the UI post login/token acqusition
    function updateUI() {
      const username = myMSALObj.getaccount().name;
      console.log(myMSALObj.getaccount());
      logMessage("User '" + username + "' logged-in");

      // add the logout button
      const authButton = document.getElementById('auth');
      authButton.innerHTML = 'logout';
      authButton.setattribute('onclick','logout();');

      // greet the user - specifying login
      const label = document.getElementById('label');
      label.innerText = "Hello " + username;

      // add the callWebApi button
      const callWebApiButton = document.getElementById('callApiButton');
      callWebApiButton.setattribute('class','visible');
    }

    // calls the resource API with the token
    function callApi() {
      getToken(tokenRequest).then(function(tokenResponse) {
        callApiWithaccessToken(tokenResponse.accessToken);
      });
    }

    // helper function to access the resource with the token
    function callApiWithaccessToken(accessToken) {
      // Call the Web API with the accessToken
      $.ajax({
        type: "GET",url: appConfig.webApi,headers: {
          'Authorization': 'Bearer ' + accessToken,},}).done(function (data) {
        logMessage("Web APi returned:\n" + JSON.stringify(data));
      })
        .fail(function (jqXHR,textStatus) {
          logMessage("Error calling the Web api:\n" + textStatus);
        })
    }

    // signout the user
    function logout() {
      // Removes all sessions,need to call AAD endpoint to do full logout
      myMSALObj.logout();
    }

    // debug helper
    function logMessage(s) {
      document.body.querySelector('.response').appendChild(document.createTextNode('\n' + s));
    }

  </script>
</body>

</html>
biefanwoxingme 回答:Microsoft MSAL.js两次包含domain_hint

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3032008.html

大家都在问