|
1 | 1 | ---
|
2 |
| -title: Angular single-page app tutorial |
| 2 | +title: Angular single-page app tutorial - Microsoft identity platform | Azure |
3 | 3 | description: Learn how Angular SPA applications can call an API that requires access tokens from the Microsoft identity platform endpoint
|
4 | 4 | services: active-directory
|
5 | 5 | documentationcenter: dev-center-name
|
@@ -90,89 +90,90 @@ Follow the instructions to [register a single-page application](https://docs.mic
|
90 | 90 |
|
91 | 91 | 1. In the *src/app* folder, edit *app.module.ts* and add the `MSALModule` to `imports` as well as the `isIE` const as shown below:
|
92 | 92 |
|
93 |
| -```javascript |
94 |
| -const isIE = window.navigator.userAgent.indexOf('MSIE ') > -1 || window.navigator.userAgent.indexOf('Trident/') > -1; |
95 |
| -@NgModule({ |
96 |
| - declarations: [ |
97 |
| - AppComponent |
98 |
| - ], |
99 |
| - imports: [ |
100 |
| - BrowserModule, |
101 |
| - AppRoutingModule, |
102 |
| - MsalModule.forRoot({ |
103 |
| - auth: { |
104 |
| - clientId: 'Enter_the_Application_Id_here', // This is your client ID |
105 |
| - authority: 'Enter_the_Cloud_Instance_Id_Here'/'Enter_the_Tenant_Info_Here', // This is your tenant id |
106 |
| - redirectUri: 'Enter_the_Redirect_Uri_Here'// This is your redirect URI |
107 |
| - }, |
108 |
| - cache: { |
109 |
| - cacheLocation: 'localStorage', |
110 |
| - storeAuthStateInCookie: isIE, // set to true for IE 11 |
111 |
| - }, |
112 |
| - }, { |
113 |
| - popUp: !isIE, |
114 |
| - consentScopes: [ |
115 |
| - 'user.read', |
116 |
| - 'openid', |
117 |
| - 'profile', |
| 93 | + ```javascript |
| 94 | + const isIE = window.navigator.userAgent.indexOf('MSIE ') > -1 || window.navigator.userAgent.indexOf('Trident/') > -1; |
| 95 | + @NgModule({ |
| 96 | + declarations: [ |
| 97 | + AppComponent |
118 | 98 | ],
|
119 |
| - unprotectedResources: [], |
120 |
| - protectedResourceMap: [ |
121 |
| - ['https://graph.microsoft.com/v1.0/me', ['user.read']] |
| 99 | + imports: [ |
| 100 | + BrowserModule, |
| 101 | + AppRoutingModule, |
| 102 | + MsalModule.forRoot({ |
| 103 | + auth: { |
| 104 | + clientId: 'Enter_the_Application_Id_here', // This is your client ID |
| 105 | + authority: 'Enter_the_Cloud_Instance_Id_Here'/'Enter_the_Tenant_Info_Here', // This is your tenant id |
| 106 | + redirectUri: 'Enter_the_Redirect_Uri_Here'// This is your redirect URI |
| 107 | + }, |
| 108 | + cache: { |
| 109 | + cacheLocation: 'localStorage', |
| 110 | + storeAuthStateInCookie: isIE, // set to true for IE 11 |
| 111 | + }, |
| 112 | + }, { |
| 113 | + popUp: !isIE, |
| 114 | + consentScopes: [ |
| 115 | + 'user.read', |
| 116 | + 'openid', |
| 117 | + 'profile', |
| 118 | + ], |
| 119 | + unprotectedResources: [], |
| 120 | + protectedResourceMap: [ |
| 121 | + ['https://graph.microsoft.com/v1.0/me', ['user.read']] |
| 122 | + ], |
| 123 | + extraQueryParameters: {} |
| 124 | + }) |
122 | 125 | ],
|
123 |
| - extraQueryParameters: {} |
| 126 | + providers: [], |
| 127 | + bootstrap: [AppComponent] |
124 | 128 | })
|
125 |
| - ], |
126 |
| - providers: [], |
127 |
| - bootstrap: [AppComponent] |
128 |
| -}) |
129 |
| -``` |
| 129 | + ``` |
130 | 130 |
|
131 |
| -Replace these values as such: |
| 131 | + Replace these values as such: |
132 | 132 |
|
133 |
| -|Value name|About| |
134 |
| -|---------|---------| |
135 |
| -|Enter_the_Application_Id_Here|In the **Overview** page of your application registration, this is your **Application (client) ID** | |
136 |
| -|Enter_the_Cloud_Instance_Id_Here|This is the instance of the Azure cloud. For the main or global Azure cloud, enter https://login.microsoftonline.com. For national clouds (for example, China), see [National clouds](https://docs.microsoft.com/azure/active-directory/develop/authentication-national-cloud).| |
137 |
| -|Enter_the_Tenant_Info_Here| Set to one of the following options: 1) If your application supports *accounts in this organizational directory*, replace this value with the **Directory (Tenant) ID** or **Tenant name** (for example, *contoso.microsoft.com*). 2) If your application supports *accounts in any organizational directory*, replace this value with **organizations**. 3) If your application supports *accounts in any organizational directory and personal Microsoft accounts*, replace this value with **common**. 4) To restrict support to *personal Microsoft accounts only*, replace this value with **consumers**. | |
138 |
| -|Enter_the_Redirect_Uri_Here|Replace with `http://localhost:4200`| |
| 133 | + |Value name|About| |
| 134 | + |---------|---------| |
| 135 | + |Enter_the_Application_Id_Here|In the **Overview** page of your application registration, this is your **Application (client) ID** | |
| 136 | + |Enter_the_Cloud_Instance_Id_Here|This is the instance of the Azure cloud. For the main or global Azure cloud, enter https://login.microsoftonline.com. For national clouds (for example, China), see [National clouds](https://docs.microsoft.com/azure/active-directory/develop/authentication-national-cloud).| |
| 137 | + |Enter_the_Tenant_Info_Here| Set to one of the following options: 1) If your application supports *accounts in this organizational directory*, replace this value with the **Directory (Tenant) ID** or **Tenant name** (for example, *contoso.microsoft.com*). 2) If your application supports *accounts in any organizational directory*, replace this value with **organizations**. 3) If your application supports *accounts in any organizational directory and personal Microsoft accounts*, replace this value with **common**. 4) To restrict support to *personal Microsoft accounts only*, replace this value with **consumers**. | |
| 138 | + |Enter_the_Redirect_Uri_Here|Replace with `http://localhost:4200`| |
139 | 139 |
|
140 |
| -For more information about available configurable options, see [Initialize client applications](msal-js-initializing-client-applications.md). |
| 140 | + For more information about available configurable options, see [Initialize client applications](msal-js-initializing-client-applications.md). |
141 | 141 |
|
142 | 142 | 2. In the same file, add the following import to the top of the file:
|
143 |
| -```javascript |
144 |
| -import { MsalModule, MsalInterceptor } from '@azure/msal-angular'; |
145 |
| -``` |
146 | 143 |
|
147 |
| -### Import modules |
148 |
| -Add the following import statements to the top of `src/app/app.component.ts` |
149 |
| -```javascript |
150 |
| -import { MsalService } from '@azure/msal-angular'; |
151 |
| -import { Component, OnInit } from '@angular/core'; |
152 |
| -``` |
153 |
| -## Sign in a user |
154 |
| - |
155 |
| -Add the following code to `AppComponent` to sign in a user: |
| 144 | + ```javascript |
| 145 | + import { MsalModule, MsalInterceptor } from '@azure/msal-angular'; |
| 146 | + ``` |
156 | 147 |
|
157 |
| -```javascript |
158 |
| -export class AppComponent implements OnInit { |
159 |
| - constructor(private broadcastService: BroadcastService, private authService: MsalService) { } |
160 |
| - |
161 |
| - login() { |
162 |
| - const isIE = window.navigator.userAgent.indexOf('MSIE ') > -1 || window.navigator.userAgent.indexOf('Trident/') > -1; |
163 |
| - |
164 |
| - if (isIE) { |
165 |
| - this.authService.loginRedirect({ |
166 |
| - extraScopesToConsent: ["user.read", "openid", "profile"] |
167 |
| - }); |
168 |
| - } else { |
169 |
| - this.authService.loginPopup({ |
170 |
| - extraScopesToConsent: ["user.read", "openid", "profile"] |
171 |
| - }); |
| 148 | + ### Import modules |
| 149 | + Add the following import statements to the top of `src/app/app.component.ts` |
| 150 | + ```javascript |
| 151 | + import { MsalService } from '@azure/msal-angular'; |
| 152 | + import { Component, OnInit } from '@angular/core'; |
| 153 | + ``` |
| 154 | + ## Sign in a user |
| 155 | + |
| 156 | + Add the following code to `AppComponent` to sign in a user: |
| 157 | + |
| 158 | + ```javascript |
| 159 | + export class AppComponent implements OnInit { |
| 160 | + constructor(private broadcastService: BroadcastService, private authService: MsalService) { } |
| 161 | +
|
| 162 | + login() { |
| 163 | + const isIE = window.navigator.userAgent.indexOf('MSIE ') > -1 || window.navigator.userAgent.indexOf('Trident/') > -1; |
| 164 | +
|
| 165 | + if (isIE) { |
| 166 | + this.authService.loginRedirect({ |
| 167 | + extraScopesToConsent: ["user.read", "openid", "profile"] |
| 168 | + }); |
| 169 | + } else { |
| 170 | + this.authService.loginPopup({ |
| 171 | + extraScopesToConsent: ["user.read", "openid", "profile"] |
| 172 | + }); |
| 173 | + } |
172 | 174 | }
|
173 | 175 | }
|
174 |
| -} |
175 |
| -``` |
| 176 | + ``` |
176 | 177 |
|
177 | 178 | > [!TIP]
|
178 | 179 | > We recommend using `loginRedirect` for Internet Explorer users.
|
|
0 commit comments