In this post I’ll try to explain how we can build an MVC application that uses Azure Active Directory Application Roles for authorization. This kind of authorization is implemented using role based access control – RBAC. When using this kind of authorization we can grant permissions to roles, not to individual users or groups. The administrator can then assign roles to different users and groups. Thanks to that he can control who has access to what content and functionality.
Custom application roles can be defined in Azure Management Portal in our application manifest file. All you need to do is to download your application manifest file by clicking Download Manifest:
In this file we can specify roles specific to our application. These application roles can be assigned to a user or group. Then when a user signs in to the application, the user token will contain a roles claim with any of the application roles the user was assigned to - either directly or indirectly through group assignment. This then allows your application to make authorization decisions purely based on the roles claim.
Each role have following fields:
- id – unique quid which will identify your role
- displayName – this will be displayed in the users and groups assignment experience as well as in the consent experience
- description – we can here describe what application privileges role grants
- value – Azure AD will emit this value in the roles claim in the tokens that users and client applications get for the resource application
- allowedMemberTypes – this property dictates when the role can be assigned to the user and group or to a client application or both. To enable the role be assigned to users or groups, specify “User”, and to specify the role be assigned to client applications, specify “Application”.
- isEnabled – if the role is enabled or not
- origin – this property of role will be deprecated, set its value to “Application”
Here is how you can define your custom application roles:
When you will upload your updated manifest file you will be able to assign users to custom roles. Navigate to the users tab under the application to which you would like to assign users and groups. Select a user and click on the Assign action on the bottom bar. Here you can assign the desired role to the user. Only the global administrator or the user administrator of the directory can assign application roles.
Here is how we can configure the application security options in MVC 6 Startup.cs file:
And example usage. Here is how we can authorize access to specific actions:
And use roles in code: