I am trying to learn more about Identity Server. I am currently struggling to get Role Based Authorisation working. Here are the steps I have followed:
1) Download the sample solution: https://github.com/IdentityServer/IdentityServer4.Samples/tree/release/Quickstarts/6_AspNetIdentity
2) Run the solution, which starts:
a) Identity Server project
b) MVC project
c) API project
3) Browse to the MVC project and apply migrations. 4) Register a new user: [email protected] 5) Browse to: CallApiUsingUserAccessToken in the MVC project. The API is reached as expected because the user is authorised.
Say I now wanted to change IdentityContoller from this:
[Authorize]
public class IdentityController : ControllerBase
to this:
[Authorize(Roles="Admin")]
public class IdentityController : ControllerBase
and Home Controller (https://github.com/IdentityServer/IdentityServer4.Samples/blob/release/Quickstarts/6_AspNetIdentity/src/MvcClient/Controllers/HomeController.cs) from this:
public async Task<IActionResult> CallApiUsingUserAccessToken()
to this:
[Authorize(Roles="Admin")]
public async Task<IActionResult> CallApiUsingUserAccessToken()
What changes would I have to make to the configuration?
I have tried a few suggestions this afternoon. For example, in the startup of the MVCClient I tried adding:
options.ClaimActions.MapJsonKey("role", "role", "role");
options.TokenValidationParameters.NameClaimType = "name";
options.TokenValidationParameters.RoleClaimType = "role";
Please assume that I have correctly added the roles to the identity database (and associated the roles with the users).