• Stars
    star
    101
  • Rank 336,841 (Top 7 %)
  • Language
    C#
  • License
    MIT License
  • Created over 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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. [email protected] / Qwerty1234#
  2. [email protected] / Qwerty1234#

and 2 roles:

  1. Users
  2. Administrators

The 'Administrators' & 'Users' roles will be assigned to: [email protected]

The 'Users' role will be assigned to: [email protected]

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