Skip to content

Commit b63efb2

Browse files
authored
Merge pull request #213265 from derisen/patch-10
Update JS code snippet
2 parents f74f1b9 + 8ca2b76 commit b63efb2

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

articles/active-directory/develop/app-resilience-continuous-access-evaluation.md

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,20 @@ You can test your application by signing in a user to the application then using
104104
When these conditions are met, the app can extract the claims challenge from the API response header as follows:
105105

106106
```javascript
107-
const authenticateHeader = response.headers.get('www-authenticate');
108-
const claimsChallenge = parseChallenges(authenticateHeader).claims;
109-
110-
// ...
107+
try {
108+
const response = await fetch(apiEndpoint, options);
109+
110+
if (response.status === 401 && response.headers.get('www-authenticate')) {
111+
const authenticateHeader = response.headers.get('www-authenticate');
112+
const claimsChallenge = parseChallenges(authenticateHeader).claims;
113+
114+
// use the claims challenge to acquire a new access token...
115+
}
116+
} catch(error) {
117+
// ...
118+
}
111119

120+
// helper function to parse the www-authenticate header
112121
function parseChallenges(header) {
113122
const schemeSeparator = header.indexOf(' ');
114123
const challenges = header.substring(schemeSeparator + 1).split(',');
@@ -126,24 +135,20 @@ function parseChallenges(header) {
126135
Your app would then use the claims challenge to acquire a new access token for the resource.
127136

128137
```javascript
138+
const tokenRequest = {
139+
claims: window.atob(claimsChallenge), // decode the base64 string
140+
scopes: ['User.Read']
141+
account: msalInstance.getActiveAccount();
142+
};
143+
129144
let tokenResponse;
130145

131146
try {
132-
tokenResponse = await msalInstance.acquireTokenSilent({
133-
claims: window.atob(claimsChallenge), // decode the base64 string
134-
scopes: scopes, // e.g ['User.Read', 'Contacts.Read']
135-
account: account, // current active account
136-
});
137-
147+
tokenResponse = await msalInstance.acquireTokenSilent(tokenRequest);
138148
} catch (error) {
139149
if (error instanceof InteractionRequiredAuthError) {
140-
tokenResponse = await msalInstance.acquireTokenPopup({
141-
claims: window.atob(claimsChallenge), // decode the base64 string
142-
scopes: scopes, // e.g ['User.Read', 'Contacts.Read']
143-
account: account, // current active account
144-
});
150+
tokenResponse = await msalInstance.acquireTokenPopup(tokenRequest);
145151
}
146-
147152
}
148153
```
149154

@@ -154,8 +159,7 @@ const msalConfig = {
154159
auth: {
155160
clientId: 'Enter_the_Application_Id_Here',
156161
clientCapabilities: ["CP1"]
157-
// the remaining settings
158-
// ...
162+
// remaining settings...
159163
}
160164
}
161165

0 commit comments

Comments
 (0)