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
{{ message }}
This repository was archived by the owner on May 17, 2024. It is now read-only.
Copy file name to clipboardExpand all lines: 5-AccessControl/1-call-api-roles/README.md
+59-27Lines changed: 59 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,6 +5,7 @@ services: ms-identity
5
5
languages:
6
6
- typescript
7
7
- csharp
8
+
- javascript
8
9
products:
9
10
- azure-active-directory
10
11
- msal-js
@@ -31,10 +32,14 @@ description: Angular single-page application calling a protected web API using A
31
32
32
33
This sample demonstrates a cross-platform application suite involving an Angular single-page application (*TodoListSPA*) calling an ASP.NET Core web API (*TodoListAPI*) secured with the Microsoft identity platform. In doing so, it implements **Role-based Access Control** (RBAC) by using Azure AD **App Roles**.
33
34
34
-
Access control in Azure AD can be done with **Security Groups** as well, as we will cover in the [next tutorial](../2-call-api-groups/README.md). **Security Groups** and **App Roles** in Azure AD are by no means mutually exclusive - they can be used in tandem to provide even finer grained access control.
35
+
Role based access control in Azure AD can be done with **Delegated** and **App** permissions and **Security Groups** as well. we will cover RBAC using Security Groups in the [next tutorial](../2-call-api-groups/README.md).**Delegated** and **App** permissions,**Security Groups** and **App Roles** in Azure AD are by no means mutually exclusive - they can be used in tandem to provide even finer grained access control.
35
36
36
37
In the sample, a **dashboard** component allows signed-in users to see the tasks assigned to users and is only accessible by users under an **app role** named **TaskAdmin**.
37
38
39
+
> :information_source: See the community call: [Implement authorization in your applications with the Microsoft identity platform](https://www.youtube.com/watch?v=LRoc-na27l0)
40
+
41
+
> :information_source: See the community call: [Deep dive on using MSAL.js to integrate Angular single-page applications with Azure Active Directory](https://www.youtube.com/watch?v=EJey9KP1dZA)
42
+
38
43
## Scenario
39
44
40
45
- The **TodoListSPA** uses [MSAL Angular](https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/lib/msal-angular) to authenticate a user with the Microsoft identity platform.
@@ -165,10 +170,10 @@ To manually register the apps, as a first step you'll need to:
165
170
1. Select **Add a scope** button open the **Add a scope** screen and Enter the values as indicated below:
166
171
1. For **Scope name**, use `access_as_user`.
167
172
1. Select **Admins and users** options for **Who can consent?**.
168
-
1. For **Admin consent display name** type in the details, `e.g. Allow the users of the app msal-angular-app to read ToDo list items`.
169
-
1. For **Admin consent description** type in the details `e.g. Allows the app msal-angular-app to read the signed-in users ToDo list items.`
170
-
1. For **User consent display name** type in the details `e.g. Read Todolist items as yourself`.
171
-
1. For **User consent description** type in the details `e.g. Allow the app msal-angular-app to read Todolist items on your behalf.`
173
+
1. For **Admin consent display name** type in *Access 'msal-angular-app' as the signed-in user.*.
174
+
1. For **Admin consent description** type in *Allow the app to access the 'msal-angular-app' as a signed-in user.*.
175
+
1. For **User consent display name** type in *Access 'msal-angular-app' on your behalf.*.
176
+
1. For **User consent description** type in *Allow the app to access the 'msal-angular-app' on your behalf.*.
172
177
1. Keep **State** as **Enabled**.
173
178
1. Select the **Add scope** button on the bottom to save this scope.
174
179
1. Select the **Manifest** blade on the left.
@@ -207,15 +212,24 @@ To add users to this app role, follow the guidelines here: [Assign users and gro
207
212
208
213
For more information, see: [How to: Add app roles in your application and receive them in the token](https://docs.microsoft.com/azure/active-directory/develop/howto-add-app-roles-in-azure-ad-apps)
209
214
210
-
##### Configure the app (msal-angular-app) to use your app registration
215
+
##### (Optional) Configure Optional Claims
216
+
217
+
1. Still on the same app registration, select the **Token configuration** blade to the left.
218
+
1. Select **Add optional claim**:
219
+
1. Select **optional claim type**, then choose **Access**.
220
+
1. Select the optional claim **acct**.
221
+
> Provides user's account status in tenant. If the user is a member of the tenant, the value is 0. If they're a guest, the value is 1.
222
+
1. Select **Add** to save your changes.
223
+
224
+
##### Configure the client app (msal-angular-app) to use your app registration
211
225
212
226
Open the project in your IDE (like Visual Studio or Visual Studio Code) to configure the code.
213
227
214
228
> In the steps below, "ClientID" is the same as "Application ID" or "AppId".
215
229
216
230
1. Open the `API\TodoListAPI\appsettings.json` file.
217
-
1. Find the key `Enter the domain of your Azure AD tenant, e.g. contoso.onmicrosoft.com` and replace the existing value with your Azure AD tenant name.
218
-
1. Find the key `Enter the ID of your Azure AD tenant copied from the Azure portal` and replace the existing value with your Azure AD tenant ID.
231
+
1. Find the key `Enter the domain of your Azure AD tenant, e.g. contoso.onmicrosoft.com` and replace the existing value with your Azure AD tenant domain, ex. `contoso.onmicrosoft.com`.
232
+
1. Find the key `Enter the ID of your Azure AD tenant copied from the Azure portal` and replace the existing value with your Azure AD tenant/directory ID.
219
233
1. Find the key `Enter the application ID (clientId) of the 'TodoListAPI' application copied from the Azure portal` and replace the existing value with the application ID (clientId) of `msal-angular-app` app copied from the Azure portal.
220
234
221
235
1. Open the `SPA\src\app\auth-config.ts` file.
@@ -282,41 +296,61 @@ To provide feedback on or suggest features for Azure Active Directory, visit [Us
282
296
283
297
### Angular RoleGuard and protected routes for role-based access control
284
298
285
-
The client application Angular SPA has a **RoleGuard** (`role.guard.ts`) component that checks whether a user has the right privileges to access a protected route. It does this by checking `roles` claim the ID token of the signed-in user:
299
+
The client application Angular SPA has a [role.guard.ts](./SPA/src/app/role.guard.ts) component that checks whether a user has the right privileges to access a protected route. It does this by checking `roles` claim the ID token of the signed-in user:
window.alert('You do not have access as the expected role is not found. Please ensure that your account is assigned to an app role and then sign-out and sign-in again.');
303
-
returnfalse;
304
337
}
305
338
306
-
returntrue;
307
-
}
339
+
returnof(hasRequiredRole);
340
+
})
341
+
);
342
+
}
308
343
}
309
344
```
310
345
311
-
We then enable **RoleGuard** in `app-routing.module.ts` as follows:
346
+
We then enable **RoleGuard** in [app-routing.module.ts](./SPA/src/app/app-routing.module.ts) as follows:
312
347
313
348
```typescript
314
349
const routes:Routes= [
315
350
{
316
351
path: 'todo-edit/:id',
317
352
component: TodoEditComponent,
318
353
canActivate: [
319
-
MsalGuard,
320
354
RoleGuard
321
355
],
322
356
data: {
@@ -327,7 +361,6 @@ const routes: Routes = [
327
361
path: 'todo-view',
328
362
component: TodoViewComponent,
329
363
canActivate: [
330
-
MsalGuard,
331
364
RoleGuard
332
365
],
333
366
data: {
@@ -338,7 +371,6 @@ const routes: Routes = [
338
371
path: 'dashboard',
339
372
component: DashboardComponent,
340
373
canActivate: [
341
-
MsalGuard,
342
374
RoleGuard,
343
375
],
344
376
data: {
@@ -395,7 +427,7 @@ Finally, in `TodoListController.cs`, we decorate our routes with the appropriate
0 commit comments