Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

How to actually implement Roles in Blazor WebApp and Identity?

$
0
0

So, here's the thing: If you create a Blazor WebApp (.Net8) with individual accounts enabled, it will give you a working solution. For development, I'm using Sqlite. Here's what that looks like (in an otherwise completely functional app):

builder.Services.AddDbContext<ApplicationDbContext>(options =>    options.UseSqlite("Data Source=Application.db"));builder.Services.AddDatabaseDeveloperPageExceptionFilter();builder.Services.AddIdentityCore<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)    .AddEntityFrameworkStores<ApplicationDbContext>()    .AddSignInManager()    .AddDefaultTokenProviders();

Please take note of the Core in AddIdentityCore. Now, the helptext for this method states that you simply should use AddRole<IdentityRole> if you want roles. An inspection of the database also yields that there are automatically created tables for roles (and claims).

However, when you add the AddRoles<IdentityRole> it then bombs out because the RoleManager service is missing. Easy fix: Just do AddRoleManager<RoleManager<IdentityRole>>

Which in turn bombs out because the RoleStore service is missing. Okay. So I do AddRoleStore<IdentityRole> and everything is peachy, right?

Nope: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContext' while attempting to activate 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.RoleStore'

Which is weird because every other service finds the DbContext just fine. So, where do I go from here?

Oh, and using other things like AddIdentity lets the app bomb out at other locations. I'm a bit irritated because all the documentation available makes no mention of AddIdentityCore and also makes no mention of the necessity to add the manager and the store, instead an AddRole suffices in those examples.


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>