An Angular single-page application (SPA) calling a protected web API and using App Roles to implement Role-Based Access Control (RBAC)
- Overview
- Scenario
- Contents
- Prerequisites
- Setup
- Registration
- Running the sample
- Explore the sample
- About the code
- More information
- Community Help and Support
- Contributing
This sample demonstrates a cross-platform application suite involving an Angular SPA (TodoListSPA) calling an ASP.NET Core web API (TodoListAPI) secured with the Microsoft identity platform. In doing so, it implements role-based access control by using Azure AD App Roles.
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.
- The TodoListSPA uses MSAL Angular (Preview) to authenticate a user with the Microsoft identity platform.
- The app then obtains an access token from Azure Active Directory (Azure AD) on behalf of the authenticated user for the TodoListAPI.
- The access token is then used by the TodoListAPI to authorize the user.
- TodoListAPI uses MSAL.NET and Microsoft.Identity.Web to protect its endpoint and accept only authorized calls.
File/folder | Description |
---|---|
AppCreationScripts |
Contains Powershell scripts to automate app registrations. |
TodoListAPI |
Source code of the TodoList API. |
TodoListSPA |
Source code of the TodoList client SPA. |
CHANGELOG.md |
List of changes to the sample. |
CONTRIBUTING.md |
Guidelines for contributing to the sample. |
LICENSE |
The license for the sample. |
- No additional prerequisites
Using a command line interface such as VS Code integrated terminal, follow the steps below:
cd chapter2
cd TodoListAPI
dotnet restore
dotnet dev-certs https --clean
dotnet dev-certs https --trust
Learn more about HTTPS in .NET Core.
cd ../
cd TodoListSPA
npm install
There are two projects in this sample. Each needs to be separately registered in your Azure AD tenant. To register these projects, you can:
- follow the steps below for manually register your apps
- or use PowerShell scripts that:
- automatically creates the Azure AD applications and related objects (passwords, permissions, dependencies) for you.
- modify the projects' configuration files.
Expand this section if you want to use this automation:
⚠️ If you have never used Azure AD Powershell before, we recommend you go through the App Creation Scripts once to ensure that your environment is prepared correctly for this step.
-
On Windows, run PowerShell as Administrator and navigate to the root of the cloned directory
-
If you have never used Azure AD Powershell before, we recommend you go through the App Creation Scripts once to ensure that your environment is prepared correctly for this step.
-
In PowerShell run:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force
-
Run the script to create your Azure AD application and configure the code of the sample application accordingly.
-
In PowerShell run:
cd .\AppCreationScripts\ .\Configure.ps1
Other ways of running the scripts are described in App Creation Scripts The scripts also provide a guide to automated application registration, configuration and removal which can help in your CI/CD scenarios.
As a first step you'll need to:
- Sign in to the Azure portal.
- If your account is present in more than one Azure AD tenant, select your profile at the top right corner in the menu on top of the page, and then switch directory to change your portal session to the desired Azure AD tenant.
- Navigate to the Azure portal and select the Azure AD service.
- Select the App Registrations blade on the left, then select New registration.
- In the Register an application page that appears, enter your application's registration information:
- In the Name section, enter a meaningful application name that will be displayed to users of the app, for example
TodoListAPI
. - Under Supported account types, select Accounts in this organizational directory only.
- In the Name section, enter a meaningful application name that will be displayed to users of the app, for example
- Select Register to create the application.
- In the app's registration screen, find and note the Application (client) ID. You use this value in your app's configuration file(s) later in your code.
- Select Save to save your changes.
- In the app's registration screen, select the Expose an API blade to the left to open the page where you can declare the parameters to expose this app as an API for which client applications can obtain access tokens for.
The first thing that we need to do is to declare the unique resource URI that the clients will be using to obtain access tokens for this Api. To declare an resource URI, follow the following steps:
- Select
Set
next to the Application ID URI to generate a URI that is unique for this app. - For this sample, accept the proposed Application ID URI (
api://{clientId}
) by selecting Save.
- Select
- All APIs have to publish a minimum of one scope for the client's to obtain an access token successfully. To publish a scope, follow the following steps:
- Select Add a scope button open the Add a scope screen and Enter the values as indicated below:
- For Scope name, use
access_as_user
. - Select Admins and users options for Who can consent?.
- For Admin consent display name type
Access TodoListAPI
. - For Admin consent description type
Allows the app to access TodoListAPI as the signed-in user.
- For User consent display name type
Access TodoListAPI
. - For User consent description type
Allow the application to access TodoListAPI on your behalf.
- Keep State as Enabled.
- Select the Add scope button on the bottom to save this scope.
- For Scope name, use
- Select Add a scope button open the Add a scope screen and Enter the values as indicated below:
- Still on the same app registration, select the App roles blade to the left.
- Select Create app role:
- For Display name, enter a suitable name, for instance TaskAdmin.
- For Allowed member types, choose User.
- For Value, enter TaskAdmin.
- For Description, enter Admins can read any user's todo list.
- Select Create app role:
- For Display name, enter a suitable name, for instance TaskUser.
- For Allowed member types, choose User.
- For Value, enter TaskUser.
- For Description, enter Users can read and modify their todo lists.
- Select Apply to save your changes.
To add users to this app role, follow the guidelines here: Assign users and groups to roles.
💡 Important security tip
When you set User assignment required? to Yes, Azure AD will check that only users assigned to your application in the Users and groups blade are able to sign-in to your app. You can assign users directly or by assigning security groups they belong to.
For more information, see: How to: Add app roles in your application and receive them in the token
Open the project in your IDE (like Visual Studio or Visual Studio Code) to configure the code.
In the steps below, "ClientID" is the same as "Application ID" or "AppId".
- Open the
TodoListAPI\appsettings.json
file. - Find the key
Domain
and replace the existing value with your Azure AD tenant name. - Find the key
TenantId
and replace the existing value with your Azure AD tenant ID. - Find the key
ClientId
and replace the existing value with the application ID (clientId) of TodoListAPI app copied from the Azure portal.
- Navigate to the Azure portal and select the Azure AD service.
- Select the App Registrations blade on the left, then select New registration.
- In the Register an application page that appears, enter your application's registration information:
- In the Name section, enter a meaningful application name that will be displayed to users of the app, for example
TodoListSPA
. - Under Supported account types, select Accounts in this organizational directory only.
- In the Redirect URI (optional) section, select Single-page application in the combo-box and enter the following redirect URI:
http://localhost:4200/
.
- In the Name section, enter a meaningful application name that will be displayed to users of the app, for example
- Select Register to create the application.
- In the app's registration screen, find and note the Application (client) ID. You use this value in your app's configuration file(s) later in your code.
- Select Save to save your changes.
- In the app's registration screen, select the API permissions blade in the left to open the page where we add access to the APIs that your application needs.
- Select the Add a permission button and then,
- Ensure that the My APIs tab is selected.
- In the list of APIs, select the API
TodoListAPI
. - In the Delegated permissions section, select the Access 'TodoListAPI' in the list. Use the search box if necessary.
- Select the Add permissions button at the bottom.
- Still on the same app registration, select the App roles blade to the left.
- Select Create app role:
- For Display name, enter a suitable name, for instance TaskAdmin.
- For Allowed member types, choose User.
- For Value, enter TaskAdmin.
- For Description, enter Admins can read any user's todo list.
- Select Create app role:
- For Display name, enter a suitable name, for instance TaskUser.
- For Allowed member types, choose User.
- For Value, enter TaskUser.
- For Description, enter Users can read and modify their todo lists.
- Select Apply to save your changes.
To add users to this app role, follow the guidelines here: Assign users and groups to roles.
Open the project in your IDE (like Visual Studio or Visual Studio Code) to configure the code.
In the steps below, "ClientID" is the same as "Application ID" or "AppId".
-
Open the
TodoListSPA\src\app\auth-config.json
file. -
Find the key
clientId
and replace the existing value with the application ID (clientId) of TodoListSPA app copied from the Azure portal. -
Find the key
tenantId
and replace the existing value with your Azure AD tenant ID copied from the Azure portal. -
Find the key
resources.todoListApi.resourceUri
and replace the existing value with the endpoint TodoListAPI (by defaulthttps://localhost:44351/api/todolist
). -
Find the key
resources.todoListApi.resourceScopes
and replace the existing value with scope you created during the app registration ofTodoListAPI
. -
The number of App Roles that can be created for an app are limited by the App Manifest limits.
-
To receive the
roles
claim in Id and Access tokens with the name of the app roles this user is assigned to, make sure that the user accounts you plan to sign-in to this app is assigned to the app roles of this app. The guide, Assign a user or group to an enterprise app in Azure Active Directory provides step by step instructions.Role Rights TaskAdmin
Admins can read others' TodoLists but cannot add/remove todos. TaskUser
Users can read and modify their TodoList but cannot see others' lists.
Using a command line interface such as VS Code integrated terminal, locate the application directory. Then:
cd ../
cd TodoListSPA
npm start
In a separate console window, execute the following commands:
cd TodoListAPI
dotnet run
For Visual Studio Users
Clean the solution, rebuild the solution, and run it. You might want to go into the solution properties and set both projects as startup projects, with the service project starting first.
- Open your browser and navigate to
http://localhost:4200
. - Sign-in using the button on top-right:
- Click on the Get My Tasks button to access your (the signed-in user's) todo list:
- If the signed-in user has the right privileges (i.e. in the right "role"), click on the See All Tasks button to access every users' todo list:
- If the signed-in user does not have the right privileges, clicking on the See All Tasks will give an error:
ℹ️ Consider taking a moment to share your experience with us
Our client application, TodoListSPA, has a RoleGuard (role-guard.service.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:
export class RoleGuardService implements CanActivate {
constructor(private authService: MsalService) {}
canActivate(route: ActivatedRouteSnapshot): boolean {
const expectedRole = route.data.expectedRole;
if (!this.authService.getAccount().idTokenClaims.roles) {
window.alert('Token does not have roles claim. Please ensure that your account is assigned to an app role and then sign-out and sign-in again.');
return false;
} else if (!this.authService.getAccount().idTokenClaims.roles.includes(expectedRole)) {
window.alert('You do not have access as expected role is missing. Please ensure that your account is assigned to an app role and then sign-out and sign-in again.');
return false;
}
return true;
}
}
We then enable RoleGuard in app-routing.module.ts
as follows:
const routes: Routes = [
{
path: 'todo-edit/:id',
component: TodoEditComponent,
canActivate: [
MsalGuard,
RoleGuardService
],
data: {
expectedRole: 'TenantUser'
}
},
{
path: 'todo-view',
component: TodoViewComponent,
canActivate: [
MsalGuard,
RoleGuardService
],
data: {
expectedRole: 'TenantUser'
}
},
{
path: 'dashboard',
component: DashboardComponent,
canActivate: [
MsalGuard,
RoleGuardService,
],
data: {
expectedRole: 'TaskAdmin'
}
},
{
path: '',
component: HomeComponent
}
];
However, it is important to be aware of that no content on the front-end application can be truly secure. That is, our RoleGuard component is primarily responsible for rendering the correct pages and other UI elements for a user in a particular role; in the example above, we allow only users in the TaskAdmin
role to see the Dashboard
component. In order to truly protect data and expose certain REST operations to a selected set of users, we enable RBAC on the back-end/web API as well in this sample. This is shown next.
As mentioned before, in order to truly implement RBAC and secure data, this sample allows only authorized calls to our web API. We do this by defining access policies and decorating our HTTP methods with them. To do so, we first add roles
claim as a validation parameter in Startup.cs
, and then we create authorization policies that depends on this claim:
// See https://docs.microsoft.com/aspnet/core/security/authorization/roles for more info.
services.Configure<JwtBearerOptions>(JwtBearerDefaults.AuthenticationScheme, options =>
{
// The claim in the Jwt token where App roles are available.
options.TokenValidationParameters.RoleClaimType = "roles";
});
// Adding authorization policies that enforce authorization using Azure AD roles.
services.AddAuthorization(options =>
{
options.AddPolicy(AuthorizationPolicies.AssignmentToTaskUserRoleRequired, policy => policy.RequireRole(AppRole.TaskUser));
options.AddPolicy(AuthorizationPolicies.AssignmentToTaskAdminRoleRequired, policy => policy.RequireRole(AppRole.TaskAdmin));
});
We defined these roles in AppRoles.cs
as follows:
public static class AppRole
{
public const string TaskUser = "TaskUser";
public const string TaskAdmin = "TaskAdmin";
}
public static class AuthorizationPolicies
{
public const string AssignmentToTaskUserRoleRequired = "AssignmentToTaskUserRoleRequired";
public const string AssignmentToTaskAdminRoleRequired = "AssignmentToTaskAdminRoleRequired";
}
Finally, in TodoListController.cs
, we decorate our routes with the appropriate policy:
// GET: api/todolist/getAll
[HttpGet]
[Route("getAll")]
[Authorize(Policy = AuthorizationPolicies.AssignmentToTaskAdminRoleRequired)]
public async Task<ActionResult<IEnumerable<TodoItem>>> GetAll()
{
HttpContext.VerifyUserHasAnyAcceptedScope(scopeRequiredByApi);
return await _context.TodoItems.ToListAsync();
}
// GET: api/todolist
[HttpGet]
[Authorize(Policy = AuthorizationPolicies.AssignmentToTaskUserRoleRequired)]
public async Task<ActionResult<IEnumerable<TodoItem>>> GetTodoItems()
{
HttpContext.VerifyUserHasAnyAcceptedScope(scopeRequiredByApi);
string owner = User.FindFirst("preferred_username")?.Value;
return await _context.TodoItems.Where(item => item.Owner == owner).ToListAsync();
}
ℹ️ Did the sample not work for you as expected? Then please reach out to us using the GitHub Issues page.
To debug the .NET Core Web API that comes with this sample, install the C# extension for Visual Studio Code.
Learn more about using .NET Core with Visual Studio Code.
Authorization in Azure AD can also be done with Security Groups as well, as we will now cover in second chapter. 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.
Now lets learn about how to use Security Groups for authorization in your app in the second chapter.
Use Stack Overflow to get support from the community.
Ask your questions on Stack Overflow first and browse existing issues to see if someone has asked your question before.
Make sure that your questions or comments are tagged with [azure-active-directory
azure-ad-b2c
ms-identity
msal
].
If you find a bug in the sample, raise the issue on GitHub Issues.
To provide feedback on or suggest features for Azure Active Directory, visit User Voice page.
If you'd like to contribute to this sample, see CONTRIBUTING.MD.
This project has adopted the Microsoft Open Source Code of Conduct. For more information, see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.