Skip to content

Blazor WASM, IdentityServer4 with Multiple Roles, Additional User Claim(s) & gRPC Roles Authorization

License

Notifications You must be signed in to change notification settings

JeepNL/Blazor-WASM-Identity-gRPC

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

52 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Blazor WASM, IdentityServer4 with Multiple Roles, Additional User Claim(s) & gRPC Roles Authorization

Blazor Template Screenshot

.NET Core (Whoa Nelly, this thing does work!) 😉

This .NET 6 Preview repo combines two repo's by @javiercn:

  1. https://github.com/javiercn/BlazorAuthRoles
  2. https://github.com/javiercn/BlazorGrpcAuth

(To run this repo you'll need .NET 6.0 SDK and Visual Studio Preview)

I've added Role Authorization to the -Greeter -gRPC -Service:

Server/Startup.cs

endpoints
    .MapGrpcService<GreeterService>()
    .RequireAuthorization(new AuthorizeAttribute { Roles = "Administrator"})
    .EnableGrpcWeb();

I've also added a Client/Claims.razor page with a list of the current user's claims.

It uses Kestrel as the default webserver, a SQLite database and is "CTRL-F5'able" without any further configuration.

You can delete de SQLite database and migrations folder if you want and use the following commands in Visual Studio's Package Manager Console to re-create the db.

  1. Add-Migration InitialCreate
  2. Update-Database

At first run the app will create 2 users (if they don't exist, see: Server/SeedData.cs)

  1. admin@example.com / Qwerty1234#
  2. user@example.com / Qwerty1234#

and 2 roles:

  1. Users
  2. Administrators

The 'Administrators' & 'Users' roles will be assigned to: admin@example.com

The 'Users' role will be assigned to: user@example.com

Additional Claim(s)

(By George, I think I've got it)

  1. I've extended ASP.NET Identity AspNetUsers table with an extra 'CustomClaim' field (see: Server/Models/ApplicationUser.cs).
  2. Seeded a value to that CustomClaim field in: Server/Data/SeedData.cs
  3. Added: Server/AppClaimsPrincipalFactory.cs
  4. Modified: Server/Startup.cs to use AppClaimsPrincipalFactory.cs
  5. When you run the app you'll see the custom_claim in the Client/Pages/Claims.razor page

About

Blazor WASM, IdentityServer4 with Multiple Roles, Additional User Claim(s) & gRPC Roles Authorization

Topics

Resources

License

Stars

Watchers

Forks

Contributors 3

  •  
  •  
  •