|
| 1 | +<html> |
| 2 | +<head> |
| 3 | + <title>Calling a Web API as a user authenticated with Msal.js app</title> |
| 4 | + <style> |
| 5 | + .hidden { |
| 6 | + visibility: hidden |
| 7 | + } |
| 8 | + |
| 9 | + .visible { |
| 10 | + visibility: visible |
| 11 | + } |
| 12 | + |
| 13 | + .response { |
| 14 | + border: solid; |
| 15 | + border-width: thin; |
| 16 | + background-color: azure; |
| 17 | + padding: 2px; |
| 18 | + } |
| 19 | + </style> |
| 20 | +</head> |
| 21 | +<body> |
| 22 | + <!-- bluebird only needed if this page needs to run on Internet Explorer --> |
| 23 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/bluebird/3.3.4/bluebird.min.js" class="pre"></script> |
| 24 | + <script src="out/msal.js" class="pre"></script> |
| 25 | + <script src="https://code.jquery.com/jquery-3.2.1.min.js" class="pre"></script> |
| 26 | + |
| 27 | + <h2>Getting an access token with Azure AD B2C and calling a Web API</h2> |
| 28 | + <div> |
| 29 | + <div id="label">Sign-in with Microsoft Azure AD B2C</div> |
| 30 | + <button id="auth" onclick="login()">Login</button> |
| 31 | + <button id="callApiButton" class="hidden" onclick="callApi()">Call Web API</button> |
| 32 | + </div> |
| 33 | + |
| 34 | + <pre class="response"></pre> |
| 35 | + |
| 36 | + <script class="pre"> |
| 37 | + // The current application coordinates were pre-registered in a B2C tenant. |
| 38 | + var applicationConfig = { |
| 39 | + clientID: 'e760cab2-b9a1-4c0d-86fb-ff7084abd902', |
| 40 | + authority: "https://login.microsoftonline.com/tfp/fabrikamb2c.onmicrosoft.com/b2c_1_susi", |
| 41 | + b2cScopes: ["https://fabrikamb2c.onmicrosoft.com/demoapi/demo.read"], |
| 42 | + webApi: 'https://fabrikamb2chello.azurewebsites.net/hello', |
| 43 | + }; |
| 44 | + </script> |
| 45 | + |
| 46 | + <script> |
| 47 | + "use strict"; |
| 48 | + var clientApplication = new Msal.UserAgentApplication(applicationConfig.clientID, applicationConfig.authority, function (errorDesc, token, error, tokenType) { |
| 49 | + // Called after loginRedirect or acquireTokenPopup |
| 50 | + }); |
| 51 | + |
| 52 | + function login() { |
| 53 | + clientApplication.loginPopup(applicationConfig.b2cScopes).then(function (idToken) { |
| 54 | + clientApplication.acquireTokenSilent(applicationConfig.b2cScopes).then(function (accessToken) { |
| 55 | + updateUI(); |
| 56 | + }, function (error) { |
| 57 | + clientApplication.acquireTokenPopup(applicationConfig.b2cScopes).then(function (accessToken) { |
| 58 | + updateUI(); |
| 59 | + }, function (error) { |
| 60 | + logMessage("Error acquiring the popup:\n" + error); |
| 61 | + }); |
| 62 | + }) |
| 63 | + }, function (error) { |
| 64 | + logMessage("Error during login:\n" + error); |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + function updateUI() { |
| 69 | + var userName = clientApplication.getUser().name; |
| 70 | + logMessage("User '" + userName + "' logged-in"); |
| 71 | + var authButton = document.getElementById('auth'); |
| 72 | + authButton.innerHTML = 'logout'; |
| 73 | + authButton.setAttribute('onclick', 'logout();'); |
| 74 | + var label = document.getElementById('label'); |
| 75 | + label.innerText = "Hello " + userName; |
| 76 | + var callWebApiButton = document.getElementById('callApiButton'); |
| 77 | + callWebApiButton.setAttribute('class', 'visible'); |
| 78 | + } |
| 79 | + |
| 80 | + function callApi() { |
| 81 | + clientApplication.acquireTokenSilent(applicationConfig.b2cScopes).then(function (accessToken) { |
| 82 | + callApiWithAccessToken(accessToken); |
| 83 | + }, function (error) { |
| 84 | + clientApplication.acquireTokenPopup(applicationConfig.b2cScopes).then(function (accessToken) { |
| 85 | + callApiWithAccessToken(accessToken); |
| 86 | + }, function (error) { |
| 87 | + logMessage("Error acquiring the access token to call the Web api:\n" + error); |
| 88 | + }); |
| 89 | + }) |
| 90 | + } |
| 91 | + |
| 92 | + function callApiWithAccessToken(accessToken) { |
| 93 | + // Call the Web API with the AccessToken |
| 94 | + $.ajax({ |
| 95 | + type: "GET", |
| 96 | + url: applicationConfig.webApi, |
| 97 | + headers: { |
| 98 | + 'Authorization': 'Bearer ' + accessToken, |
| 99 | + }, |
| 100 | + }).done(function (data) { |
| 101 | + logMessage("Web APi returned:\n" + JSON.stringify(data)); |
| 102 | + }) |
| 103 | + .fail(function (jqXHR, textStatus) { |
| 104 | + logMessage("Error calling the Web api:\n" + textStatus); |
| 105 | + }) |
| 106 | + } |
| 107 | + |
| 108 | + function logout() { |
| 109 | + // Removes all sessions, need to call AAD endpoint to do full logout |
| 110 | + clientApplication.logout(); |
| 111 | + } |
| 112 | + |
| 113 | + function logMessage(s) { |
| 114 | + document.body.querySelector('.response').appendChild(document.createTextNode('\n' + s)); |
| 115 | + } |
| 116 | + |
| 117 | + </script> |
| 118 | +</body> |
| 119 | +</html> |
0 commit comments