liveBook · Manning

Role

As explained in Building Web APIs with ASP.NET Core

A role is a named identity-group designation assigned to a user for evaluating the permissions and access the user has.

A role is a named group assigned to users so authorization rules can evaluate their permissions. Role-based access control separates basic users, moderators, and administrators instead of treating every authenticated user as an administrator. section 9.4

Configure roles through a practical authorization flow:

  • Create users representing the supported user types, such as TestModerator and TestAdministrator. section 9.4.1
  • Define role names as constants, then create missing IdentityRole records through RoleManager.CreateAsync. section 9.4.2
  • Assign users with UserManager and verify that each assignment does not already exist. section 9.4.3
  • Copy each user’s roles into JWT claims so authorization middleware can inspect them on later requests. section 9.4.4
  • Restrict endpoints with role-aware authorization, using Moderator for updates and Administrator for deletes and database seeding. section 9.4.5
Listing 9.12 /Controllers/SeedController.cs file: AuthData method
[HttpPost]
[ResponseCache(NoStore = true)]
public async Task<IActionResult> AuthData()
{
    int rolesCreated = 0;
    int usersAddedToRoles = 0;
 
    if (!await _roleManager.RoleExistsAsync(RoleNames.Moderator))
    {
        await _roleManager.CreateAsync( 
            new IdentityRole(RoleNames.Moderator));       #1
        rolesCreated++;
    }
    if (!await _roleManager.RoleExistsAsync(RoleNames.Administrator))
    {
        await _roleManager.CreateAsync(
            new IdentityRole(RoleNames.Administrator));   #1
        rolesCreated++;
    }
 
    var testModerator = await _userManager
        .FindByNameAsync("TestModerator");
    if (testModerator != null
        && !await _userManager.IsInRoleAsync(
            testModerator, RoleNames.Moderator))
    {
        await _userManager.AddToRoleAsync(testModerator,
 RoleNames.Moderator);                                  #2
        usersAddedToRoles++;
    }
 
    var testAdministrator = await _userManager
        .FindByNameAsync("TestAdministrator");
    if (testAdministrator != null
        && !await _userManager.IsInRoleAsync(
            testAdministrator, RoleNames.Administrator))
    {
        await _userManager.AddToRoleAsync(
            testAdministrator, RoleNames.Moderator);      #2
        await _userManager.AddToRoleAsync(
            testAdministrator, RoleNames.Administrator);  #2
        usersAddedToRoles++;
    }
 
    return new JsonResult(new
    {
        RolesCreated = rolesCreated,
        UsersAddedToRoles = usersAddedToRoles
    });
}

Use the AuthData seeding method as a compact implementation pattern: create missing roles, find users, and add users to roles. section 9.4.2

Verify the authorization matrix

Login as TestUser, TestModerator, and TestAdministrator through SwaggerUI, place each JWT in the Authorize dialog, and call the moderator and administrator test endpoints. TestUser receives 401 for both; TestModerator receives 200 for the moderator endpoint and 401 for the administrator endpoint; TestAdministrator receives 200 for both. section 9.4.6

Keep role creation and assignment in a dedicated authentication-seeding endpoint rather than mixing them with board-game data seeding. Add future roles, such as SuperAdmin, to the constants and extend the seeding logic. section 9.4.2 section 9.5.1

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Explains role creation, assignment, JWT claims, authorization attributes, seeding, and verification, stopping short of broader policy design.
Examples
Uses Moderator, Administrator, TestUser, TestModerator, TestAdministrator, SwaggerUI, and role-restricted test endpoints.
What it assumes
Assumes working user registration, login, JWT authentication, controllers, and the parameterless Authorize attribute.
What you can do after
Configure and verify role-restricted ASP.NET Core API endpoints with seeded users, JWT role claims, and authorization attributes.
How it connects
Connects roles with role-based access control, users, JWT claims, authentication, authorization middleware, and Identity APIs.

Also explained in

Compare how 11 books explain role →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.