IdentityServer4 Claims-Based Policy WebAPI Security | Tutorial 6
In Tutorial 4, you learned how to secure WebAPI with valid JWT using the “Authorize” attribute. In this tutorial 6, you will learn how to implement policy to provide granularity access control.
Prerequisites
- Visual Studio 2019 (community edition is ok)
- Git client
Tutorial Content
Use case: Let’s say you want to do role-based access in your application. For example, only admin users can DELETE data in the sample EmployeeProfile WebAPI (from tutorial 4). See Figure 1 for the Swagger of the Employee Profile API.

This tutorial contains three parts
- Setup admin role and assign users to the admin role using IdentityServer4 Admin UI
- Configure the oAuth2 scope to include “role” in Access Token using IdentityServer4 Admin UI
- Implement a policy to limit Delete action to only users in the admin role
Part 1 — Setup admin role and assign users to the admin role using IdentityServer4 Admin UI
Task 1 — Add admin role. See Figure 2.

Task 2 — Assign the user to the admin role. See Figure 3.

Part 2 — Configure the oAuth2 scope to include “role” in Access Token using IdentityServer4 Admin UI
To add a role to the access token, navigate to the Api Resource, and add “role” to User Claims. See Figure 4.

Part 3 — Implement a policy to limit Delete action to only users in the admin role
Task 1 — Update appsettings.json with a configuration setting of the admin role name. See Figure 5 (line 22)

Task 2 — Add code to setup Policy. See figure 6.

Task 3 — Add the policy to the Authorize attribute at the action level in the controller. See Figure 7.

Test Drive
Login account: (janedoe, Pa$$word123)
Angular SPA: https://devkit-cli-angular-oidc-oauth2.azurewebsites.net
WebAPI: https://devkit-api-employeeprofile.azurewebsites.net/swagger/index.html
Source Code
Git Repo: https://github.com/workcontrolgit/devkit-apiresources-employeeprofileapi
Related Tutorials
Rapid Prototype Asp.Net Core REST API using KissApi Template — use Visual Studio template to generate Clean Architecture solution based on Repository Pattern, Unit of Work, Dapper, SQLKata, and Swagger.
Summary
This tutorial provides step by step instructions to set up and configure a policy to secure the REST API. The source code for this tutTo view all other related tutorials, visit DevKit WebAPI Security.






