You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/active-directory/develop/scenario-spa-acquire-token.md
+220-5Lines changed: 220 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,17 +27,52 @@ The silent token requests to Azure AD might fail for reasons like an expired Azu
27
27
28
28
## Choose between a pop-up or redirect experience
29
29
30
-
You can't use both the pop-up and redirect methods in your application. The choice between a pop-up or redirect experience depends on your application flow:
30
+
The choice between a pop-up or redirect experience depends on your application flow:
31
31
32
-
* If you don't want users to move away from your main application page during authentication, we recommended the pop-up method. Because the authentication redirect happens in a pop-up window, the state of the main application is preserved.
32
+
* If you don't want users to move away from your main application page during authentication, we recommend the pop-up method. Because the authentication redirect happens in a pop-up window, the state of the main application is preserved.
33
33
34
-
* If users have browser constraints or policies where pop-ups windows are disabled, you can use the redirect method. Use the redirect method with the Internet Explorer browser, because there are [known issues with pop-up windows on Internet Explorer](https://github.com/AzureAD/microsoft-authentication-library-for-js/wiki/Known-issues-on-IE-and-Edge-Browser).
34
+
* If users have browser constraints or policies where pop-up windows are disabled, you can use the redirect method. Use the redirect method with the Internet Explorer browser, because there are [known issues with pop-up windows on Internet Explorer](https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-browser/docs/internet-explorer.md#popups).
35
35
36
36
You can set the API scopes that you want the access token to include when it's building the access token request. Note that all requested scopes might not be granted in the access token. That depends on the user's consent.
37
37
38
38
## Acquire a token with a pop-up window
39
39
40
-
# [JavaScript](#tab/javascript)
40
+
# [JavaScript (MSAL.js v2)](#tab/javascript2)
41
+
42
+
The following code combines the previously described pattern with the methods for a pop-up experience:
43
+
44
+
```javascript
45
+
// MSAL.js v2 exposes several account APIs, logic to determine which account to use is the responsibility of the developer
return<p>Return your protected content here: {apiData}</p>
206
+
}
207
+
208
+
functionApp() {
209
+
return (
210
+
<AuthenticatedTemplate>
211
+
<ProtectedComponent />
212
+
</ AuthenticatedTemplate>
213
+
)
214
+
}
215
+
```
216
+
217
+
Alternatively, if you need to acquire a token outside of a React component you can call `acquireTokenSilent` but should not fallback to interaction if it fails. All interaction should take place underneath the `MsalProvider` component in your component tree.
218
+
219
+
```javascript
220
+
// MSAL.js v2 exposes several account APIs, logic to determine which account to use is the responsibility of the developer
The following pattern is as described earlier but shown with a redirect method to acquire tokens interactively. You'll need to call and await `handleRedirectPromise` on page load.
The following pattern is as described earlier but shown with a redirect method to acquire tokens interactively. You'll need to register the redirect callback as mentioned earlier.
136
283
@@ -192,6 +339,74 @@ To learn more, see [Optional claims](active-directory-optional-claims.md).
192
339
193
340
This code is the same as described earlier.
194
341
342
+
# [React](#tab/react)
343
+
344
+
If `acquireTokenSilent` fails, fallback to `acquireTokenRedirect`. This method will initiate a full-frame redirect and the response will be handled when returning to the application. When this component is rendered after returning from the redirect, `acquireTokenSilent` should now succeed as the tokens will be pulled from the cache.
return<p>Return your protected content here: {apiData}</p>
376
+
}
377
+
378
+
functionApp() {
379
+
return (
380
+
<AuthenticatedTemplate>
381
+
<ProtectedComponent />
382
+
</ AuthenticatedTemplate>
383
+
)
384
+
}
385
+
```
386
+
387
+
Alternatively, if you need to acquire a token outside of a React component you can call `acquireTokenSilent` but should not fallback to interaction if it fails. All interaction should take place underneath the `MsalProvider` component in your component tree.
388
+
389
+
```javascript
390
+
// MSAL.js v2 exposes several account APIs, logic to determine which account to use is the responsibility of the developer
0 commit comments