2424 <!-- bluebird only needed if this page needs to run on Internet Explorer -->
2525 <!-- msal.min.js can be used in the place of msal.js; included msal.js to make debug easy -->
2626 < script src ="https://cdnjs.cloudflare.com/ajax/libs/bluebird/3.3.4/bluebird.min.js " class ="pre "> </ script >
27- < script src ="https://secure.aadcdn.microsoftonline-p.com/lib/1.0.0 /js/msal.js "> </ script >
27+ < script src ="https://secure.aadcdn.microsoftonline-p.com/lib/1.1.2 /js/msal.js "> </ script >
2828 < script src ="https://code.jquery.com/jquery-3.2.1.min.js " class ="pre "> </ script >
2929
3030 < h2 > Getting an access token with Azure AD B2C and calling a Web API</ h2 >
3131 < div >
3232 < div id ="label "> Sign-in with Microsoft Azure AD B2C</ div >
33- < button id ="auth " onclick ="login () "> Login</ button >
33+ < button id ="auth " onclick ="signIn () "> Login</ button >
3434 < button id ="callApiButton " class ="hidden " onclick ="callApi() "> Call Web API</ button >
3535 </ div >
3636
@@ -43,13 +43,14 @@ <h2>Getting an access token with Azure AD B2C and calling a Web API</h2>
4343 b2cScopes : [ "https://fabrikamb2c.onmicrosoft.com/helloapi/demo.read" ] ,
4444 webApi : "https://fabrikamb2chello.azurewebsites.net/hello"
4545 } ;
46+
4647 </ script >
4748
4849 < script >
4950 "use strict" ;
5051
5152 // configuration to initialize msal
52- var msalConfig = {
53+ const msalConfig = {
5354 auth : {
5455 clientId : "e760cab2-b9a1-4c0d-86fb-ff7084abd902" , //This is your client ID
5556 authority : "https://fabrikamb2c.b2clogin.com/fabrikamb2c.onmicrosoft.com/b2c_1_susi" , //This is your tenant info
@@ -61,64 +62,68 @@ <h2>Getting an access token with Azure AD B2C and calling a Web API</h2>
6162 }
6263 } ;
6364
64- var clientApplication = new Msal . UserAgentApplication ( msalConfig ) ;
65-
66- // Register a call back for redirect flow
67- // myMSALObj.handleRedirectCallback(authRedirectCallback);
68-
69- function login ( ) {
65+ // instantiate MSAL
66+ const myMSALObj = new Msal . UserAgentApplication ( msalConfig ) ;
7067
71- var loginRequest = {
68+ // request to signin - returns an idToken
69+ const loginRequest = {
7270 scopes : appConfig . b2cScopes
73- } ;
71+ } ;
7472
75- clientApplication . loginPopup ( loginRequest ) . then ( function ( loginResponse ) {
76- var tokenRequest = {
77- scopes : appConfig . b2cScopes
78- } ;
73+ // request to acquire a token for resource access
74+ const tokenRequest = {
75+ scopes : appConfig . b2cScopes
76+ } ;
7977
80- clientApplication . acquireTokenSilent ( tokenRequest ) . then ( function ( tokenResponse ) {
81- updateUI ( ) ;
78+ // signin and acquire a token silently with POPUP flow. Fall back in case of failure with silent acquisition to popup
79+ function signIn ( ) {
80+ myMSALObj . loginPopup ( loginRequest ) . then ( function ( loginResponse ) {
81+ getToken ( tokenRequest ) . then ( updateUI ) ;
8282 } ) . catch ( function ( error ) {
83- clientApplication . acquireTokenPopup ( tokenRequest ) . then ( function ( tokenResponse ) {
84- updateUI ( ) ;
85- } ) . catch ( function ( error ) {
86- logMessage ( "Error acquiring the popup:\n" + error ) ;
87- } ) ;
88- } )
89- } ) . catch ( function ( error ) {
90- logMessage ( "Error during login:\n" + error ) ;
83+ logMessage ( error ) ;
84+ } ) ;
85+ }
86+
87+ //acquire a token silently
88+ function getToken ( tokenRequest ) {
89+ return myMSALObj . acquireTokenSilent ( tokenRequest ) . catch ( function ( error ) {
90+ console . log ( "aquire token popup" ) ;
91+ // fallback to interaction when silent call fails
92+ return myMSALObj . acquireTokenPopup ( tokenrequest ) . then ( function ( tokenResponse ) {
93+ } ) . catch ( function ( error ) {
94+ logMessage ( "Failed token acquisition" , error ) ;
95+ } ) ;
9196 } ) ;
9297 }
9398
99+ // updates the UI post login/token acqusition
94100 function updateUI ( ) {
95- var userName = clientApplication . getAccount ( ) . name ;
96- console . log ( clientApplication . getAccount ( ) ) ;
101+ const userName = myMSALObj . getAccount ( ) . name ;
102+ console . log ( myMSALObj . getAccount ( ) ) ;
97103 logMessage ( "User '" + userName + "' logged-in" ) ;
98- var authButton = document . getElementById ( 'auth' ) ;
104+
105+ // add the logout button
106+ const authButton = document . getElementById ( 'auth' ) ;
99107 authButton . innerHTML = 'logout' ;
100108 authButton . setAttribute ( 'onclick' , 'logout();' ) ;
101- var label = document . getElementById ( 'label' ) ;
109+
110+ // greet the user - specifying login
111+ const label = document . getElementById ( 'label' ) ;
102112 label . innerText = "Hello " + userName ;
103- var callWebApiButton = document . getElementById ( 'callApiButton' ) ;
113+
114+ // add the callWebApi button
115+ const callWebApiButton = document . getElementById ( 'callApiButton' ) ;
104116 callWebApiButton . setAttribute ( 'class' , 'visible' ) ;
105117 }
106118
119+ // calls the resource API with the token
107120 function callApi ( ) {
108- var tokenRequest = {
109- scopes : appConfig . b2cScopes
110- }
111- clientApplication . acquireTokenSilent ( tokenRequest ) . then ( function ( tokenResponse ) {
121+ getToken ( tokenRequest ) . then ( function ( tokenResponse ) {
112122 callApiWithAccessToken ( tokenResponse . accessToken ) ;
113- } ) . catch ( function ( error ) {
114- clientApplication . acquireTokenPopup ( tokenRequest ) . then ( function ( tokenResponse ) {
115- callApiWithAccessToken ( tokenResponse . accessToken ) ;
116- } ) . catch ( function ( error ) {
117- logMessage ( "Error acquiring the access token to call the Web api:\n" + error ) ;
118- } ) ;
119- } )
123+ } ) ;
120124 }
121125
126+ // helper function to access the resource with the token
122127 function callApiWithAccessToken ( accessToken ) {
123128 // Call the Web API with the AccessToken
124129 $ . ajax ( {
@@ -135,11 +140,13 @@ <h2>Getting an access token with Azure AD B2C and calling a Web API</h2>
135140 } )
136141 }
137142
143+ // signout the user
138144 function logout ( ) {
139145 // Removes all sessions, need to call AAD endpoint to do full logout
140- clientApplication . logout ( ) ;
146+ myMSALObj . logout ( ) ;
141147 }
142148
149+ // debug helper
143150 function logMessage ( s ) {
144151 document . body . querySelector ( '.response' ) . appendChild ( document . createTextNode ( '\n' + s ) ) ;
145152 }
0 commit comments