Skip to content

Latest commit

 

History

History
157 lines (92 loc) · 13.3 KB

File metadata and controls

157 lines (92 loc) · 13.3 KB

MSAL Angular FAQ


Compatibility

  1. What browsers are supported?
  2. What versions of Angular are supported?
  3. Does @azure/msal-angular support Server Side Rendering?
  4. Can @azure/msal-angular be used with Internet Explorer?
  5. Can @azure/msal-angular be used with Microsoft Graph JavaScript SDK?

Configuration

  1. What is the difference between @azure/msal-angular v2 and v1?
  2. How do I add tokens to API calls?
  3. How do I use my app with path/hash location strategy?

Authentication

  1. How do I log users in when they hit the application?
  2. Why is my app looping when logging in with redirect?
  3. How do I implement self-service sign-up?

Usage

  1. How do I secure routes?
  2. How do I get accounts?
  3. How do I get and set active accounts?

Errors

What if my question has not been answered?


Compatibility

What browsers are supported?

Please see here for supported browsers.

What versions of Angular are supported?

Msal Angular currently supports Angular 9, 10, 11, and 12.

Does @azure/msal-angular support Server Side Rendering?

Yes, server side rendering is supported through Angular universal. See our doc here for more information.

Can @azure/msal-angular be used with Internet Explorer?

Yes, @azure/msal-angular does support IE 11. More information can on configuration can be found here.

Can @azure/msal-angular be used with Microsoft Graph JavaScript SDK?

Yes, @azure/msal-angular can be used as a custom authentication provider for the Microsoft Graph JavaScript SDK. For an implementation, please refer to the sample: Angular SPA calling Graph API.

Configuration

What is the difference between @azure/msal-angular v2 and v1?

Please see our upgrade guide for information on the differences between @azure/msal-angular v1 and v2, as well as changes to watch out for when upgrading.

How do I add tokens to API calls?

@azure/msal-angular provides the MsalInterceptor for obtaining tokens and adding them to HTTP requests. You may find the following links helpful:

Please note that the MsalInterceptor is optional. You may wish to explicitly acquire tokens using the acquireToken APIs instead. The MsalInterceptor is provided for your convenience and may not fit all use cases. We encourage you to write your own interceptor if you have specific needs that are not addressed by the MsalInterceptor.

How do I use my app with path/hash location strategy?

@azure/msal-angular supports both the PathLocationStrategy and HashLocationStrategy, which can be configured in the app-routing.module.ts of your app. See our samples list for the routing strategy demonstrated by each sample. See the Angular docs for more details on routing strategies.

See below for additional considerations for each strategy if you are wanting to log users in on page load.

Authentication

How do I log users in when they hit the application?

To log your users in when they hit the application, without using a login button, do not call login in the ngOnInit in app.component.ts, as this can cause looping with redirects. Instead, we recommend setting the MsalGuard on your initial page, which will prompt users to log in before they reach other pages in your application. Our additional recommendations depend on your routing strategy below. Please all see our MsalGuard doc for more information.

PathLocationStrategy

For those using the PathLocationStrategy, we recommend:

  • Setting the MsalGuard on your initial page
  • Set your redirectUri to 'http://localhost:4200/auth'
  • Adding an 'auth' path to your routes, setting the MsalRedirectComponent as the component (this route should not be protected by the MsalGuard)
  • Making sure the MsalRedirectComponent is bootstrapped
  • Optionally: adding MsalGuard to all your routes if you want all your routes protected

Our Angular 10 sample demonstrates use of the PathLocationStrategy.

HashLocationStrategy

For those using the HashLocationStrategy, we recommend:

  • Setting the MsalGuard on your initial page
  • Not setting the MsalGuard on placeholder routes (e.g. /code, /error)
  • Making sure the MsalRedirectComponent is bootstrapped
  • Optionally: adding MsalGuard to all your routes if you want all your routes protected

See our Angular 11 sample for examples of how to protect routes with MsalGuard.

Why is my app looping when logging in with redirect?

One of the common reasons your app may be looping while logging in with redirects is due to improper usage of the loginRedirect() API. We recommend that you do not call loginRedirect() in the ngOnInit in the app.component.ts, as this will attempt to log in with every page load, often before any redirect has finished processing.

Redirects must be handled either with the MsalRedirectComponent or with calling handleRedirectObservable(). See our docs on redirects here for more information. Additionally, any interaction or account validation should be done after subscribing to the inProgress$ observable and filtering for InteractionStatus.None. Please see our sample for an example.

How do I implement self-service sign-up?

MSAL Angular supports self-service sign-up in the auth code flow. Please see our docs here for supported prompt values in the request and their expected outcomes, and here for an overview of self-service sign-up and configuration changes that need to be made to your Azure tenant. Please note that that self-service sign-up is not available in B2C and test environments.

Usage

How do I secure routes?

Please see our MsalGuard doc for more information about securing routes with the MsalGuard.

How do I get accounts?

The @azure/msal-browser instance used by @azure/msal-angular exposes multiple methods for getting account information. We recommend using getAllAccounts() to get all accounts, and getAccountByHomeId() and getAccountByLocalId() to get specific accounts. Note that while getAccountByUsername() is available, it should be a secondary choice, as it may be less reliable and is for convenience only. See the @azure/msal-browser docs for more details on account methods.

We recommend subscribing to the inProgress$ observable and filtering for InteractionStatus.None before retrieving account information. This ensures that all interactions have completed before getting account information. See our sample for an example of this use.

How do I get and set active accounts?

The msal-browser instance exposes getActiveAccount() and setActiveAccount() for active accounts.

We recommend subscribing to the inProgress$ observable and filtering for InteractionStatus.None before retrieving account information with getActiveAccount(). This ensures that all interactions have completed before getting account information.

We recommend setting the active account:

  • After any action that may change the account, especially if your app uses multiple accounts. See here for an example of setting the account after a successful login.
  • On initial page load. Wait until all interactions are complete (by subscribing to the inProgress$ observable and filtering for InteractionStatus.None), check if there is an active account, and if there is none, set the active account. This could be the first account retrieved by getAllAccounts(), or other account selection logic required by your app. See here for an example of checking and setting the active account on page load.

Note: Prior to @azure/msal-browser@2.16.0 active account did not persist across page loads. If you are using @azure/msal-browser@2.15.0 or earlier we recommend that you set the active account for each page load. In version 2.16.0 and above the active account will be cached in the cache location specified in your MSAL config and retrieved each time getActiveAccount is called.

Our Angular 11 sample demonstrates basic usage. Your app may require more complicated logic to choose accounts.

Errors

If you have questions about specific errors you are receiving please see the following documents detailing some of the common errors:

What if my question has not been answered?

First check the @azure/msal-browser FAQ to see if your question is answered there. Since @azure/msal-angular is a wrapper around @azure/msal-browser many questions you may have are answered there.

If you have questions about our roadmap you can find a high level overview of planned features and releases here.

If your question is not answered in this document or in the @azure/msal-browser FAQ you can open an issue and we will answer it as soon as we can.