C#C
C#3y ago
Iron

Role based Authorize Blazor Server + Client

I am stuck trying to use the @attribute [Authorize(Roles = "Admin")] in my blazor component on client side.

I started by adding
.AddRoles<IdentityRole>() “

in program.cs

public class RoleConfiguration : IEntityTypeConfiguration<IdentityRole> { public void Configure(EntityTypeBuilder<IdentityRole> builder) { builder.HasData( new IdentityRole { Name = "Visitor", NormalizedName = "VISITOR" }, new IdentityRole { Name = "Admin", NormalizedName = "ADMIN" } ); } } “

I made the above class to create the roles


Then i added a override in my ApplicationDbContext

protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); builder.ApplyConfiguration(new RoleConfiguration()); } “
then i ran
INSERT INTO AspNetUserRoles VALUES ('UserId','Administrator RoleId') “

With the ids that i get from my tables,

however, the [Authorize(Roles = "Admin)]

on a page still tells me i dont have permissions to view this page. Any tips?
Was this page helpful?