|
1 | 1 | # Interactive Browser Credential |
2 | 2 |
|
3 | | -The `InteractiveBrowserCredential` uses [Auth Code Flow][AuthCodeFlow], which uses [Proof Key for Code Exchange (PKCE)](https://tools.ietf.org/html/rfc7636), uses [MSAL v2.x](https://github.com/AzureAD/microsoft-authentication-library-for-js), and is enabled by default. It also allows switching back to the [Implicit Grant Flow][ImplicitGrantFlow] by passing the `flow` property with the value `implicit-grant` through to the constructor of the `InteractiveBrowserCredential`. |
| 3 | +The `InteractiveBrowserCredential` uses [Authorization Code Flow][AuthCodeFlow], which uses [Proof Key for Code Exchange (PKCE)](https://tools.ietf.org/html/rfc7636) both on the browser and on NodeJS. Under the hood it uses [@azure/msal-node](https://www.npmjs.com/package/@azure/msal-node) for NodeJS. For the browser it uses [MSAL v2.x](https://github.com/AzureAD/microsoft-authentication-library-for-js) by default (which also uses Authorization Code Flow), while it also allows switching back to the older [Implicit Grant Flow][ImplicitGrantFlow] by passing the `flow` property with the value `implicit-grant` through to the constructor of the `InteractiveBrowserCredential`, as follows: |
| 4 | + |
| 5 | +```ts |
| 6 | +const credential = new InteractiveBrowserCredential({ |
| 7 | + // Authorization Code Flow is recommended and used by default. |
| 8 | + // But you can switch batch to the Implicit Grant Flow if you need to: |
| 9 | + flow: "implicit-grant", |
| 10 | +}); |
| 11 | +``` |
4 | 12 |
|
5 | 13 | Follow the instructions for [creating your single-page application](https://docs.microsoft.com/azure/active-directory/develop/scenario-spa-app-registration#redirect-uri-msaljs-20-with-auth-code-flow) to correctly mark your redirect URI as enabled for CORS. |
6 | 14 |
|
7 | | -If you attempt to use the authorization code flow and see this error: |
| 15 | +When using `InteractiveBrowserCredential` on Node, you may specify a `clientId` and `tenantId`, but otherwise we try to authenticate using a public client that's available for all Azure accounts and the default tenant of your account. For Node, this credential uses a web server to fulfill the redirection. This web server tries to use the port `80` by default. A `redirectUri` can be provided to determine the proper redirection URI with the adequate port, as follows: |
| 16 | + |
| 17 | +```ts |
| 18 | +const credential = new InteractiveBrowserCredential({ |
| 19 | + // You may provide a client ID if you have an application configured. |
| 20 | + clientId: "my-client-id", |
| 21 | + // You may provide a tenant ID based on the resource you are trying to access. |
| 22 | + tenantId: "my-tenant-id", |
| 23 | + // You may provide a redirectUri based on the redirectUri configured in your AAD application: |
| 24 | + redirectUri: "http://localhost:8080/" |
| 25 | +}); |
| 26 | +``` |
| 27 | + |
| 28 | +When using `InteractiveBrowserCredential` on the browser, you will be required to pass a `clientId` in the constructor parameters, such as: |
| 29 | + |
| 30 | +```ts |
| 31 | +// If you've bundled Identity for the browser... |
| 32 | + |
| 33 | +const credential = new InteractiveBrowserCredential({ |
| 34 | + // You MUST provide a client ID if you have an application configured. |
| 35 | + clientId: "my-client-id", |
| 36 | + // You may provide a tenant ID based on the resource you are trying to access. |
| 37 | + tenantId: "my-tenant-id", |
| 38 | + // You may provide a redirectUri based on the redirectUri configured in your AAD application: |
| 39 | + redirectUri: "http://localhost:8080/" |
| 40 | +}); |
| 41 | +``` |
| 42 | + |
| 43 | +Azure Active Directory enterprise applications configured with redirect URIs for `Web` environments are no longer supported by the Authorization Code Flow. You can either configure your AAD application to use Single Page Application redirect URis (type `spa`), or switch to the `implicit-grant` flow. |
| 44 | + |
| 45 | +## CORS error |
| 46 | + |
| 47 | +If you attempt to use the Authorization Code Flow and you get an error similar to this one: |
8 | 48 |
|
9 | 49 | ``` |
10 | 50 | access to XMLHttpRequest at 'https://login.microsoftonline.com/common/v2.0/oauth2/token' from origin 'yourApp.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. |
11 | 51 | ``` |
12 | 52 |
|
13 | | -Then you need to visit your app registration and update the redirect URI for your app to type `spa` (for "single page application"). |
| 53 | +Then you need to visit your app registration and update the redirect URI you're using to the type `spa` (for "single page application"). |
| 54 | + |
| 55 | +## Sample code |
14 | 56 |
|
15 | | -You can see a sample project that uses `InteractiveBrowserCredential` with both [Auth Code Flow][AuthCodeFlow] and [Implicit Grant Flow][ImplicitGrantFlow] here: [link to the sample project](https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/identity/identity/test/manual). |
| 57 | +You can see a sample project that uses `InteractiveBrowserCredential` with both [Authorization Code Flow][AuthCodeFlow] and [Implicit Grant Flow][ImplicitGrantFlow] here: [link to the sample project](https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/identity/identity/test/manual). |
16 | 58 |
|
17 | 59 | [AuthCodeFlow]: https://docs.microsoft.com/azure/active-directory/develop/v2-oauth2-auth-code-flow |
18 | 60 | [ImplicitGrantFlow]: https://docs.microsoft.com/azure/active-directory/develop/v2-oauth2-implicit-grant-flow |
0 commit comments