Blazor Role Authorization not working

ICodeInTx 56 Reputation points
2021-01-24T08:48:59.483+00:00

I have a Blazor server application with Identity setup to utilize a SQL Server. Additionally I'm using IdentityManager2 to assist with ensuring the account/roles are setup and everything works properly everywhere except for AuthorizeView and Authorize.

First of all I'm signed in and the index lists my claims. Shown Here:
59918-image.png

On my navmenu I have this section:

        <AuthorizeView Policy="@Policies.IsAdmin">  
            <Authorized>  
                <li class="nav-item px-3">  
                    <NavLink class="nav-link" href="admin/user/list">  
                        <span class="oi oi-list-rich" aria-hidden="true"></span> Users  
                    </NavLink>  
                </li>  
                <li class="nav-item px-3">  
                    <NavLink class="nav-link" href="admin/company/list">  
                        <span class="oi oi-list-rich" aria-hidden="true"></span> Companies  
                    </NavLink>  
                </li>  
            </Authorized>  
            <NotAuthorized>  
                <div class="alert alert-danger">not auth</div>  
            </NotAuthorized>  
        </AuthorizeView>  

Additionally I have a page called WaveRiders.razor that contains this line:

@attribute [Authorize(Policy = Policies.IsAdmin)]  

Here is the code in my startup.cs

            services.AddDbContext<SqlContext>(options =>  
                   options.UseSqlServer(  
                       Configuration.GetConnectionString("SqlContextConnection")));  
  
            services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)  
                .AddRoles<IdentityRole>()  
                .AddEntityFrameworkStores<SqlContext>();  
  
            services.AddAuthorizationCore(config =>  
            {  
                config.AddPolicy(Policies.IsAdmin, Policies.IsAdminPolicy());  
                config.AddPolicy(Policies.IsCompanyOwner, Policies.IsCompanyOwnerPolicy());  
            });  
  
            services.AddRazorPages();  
  
            services.AddServerSideBlazor();  
            services.AddScoped<AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider<IdentityUser>>();  

So even though I am authenticated and I do belong to the Administrator group it appears I'm always unauthorized and my screen appears as:
59848-image.png

I've worked on this issues for at least 10 hours and I'm at the end of trying. Does anyone see what is wrong with the code I provided?

Here is proof from IdentityManager that I belong to the roles:
59883-image.png

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,404 questions
{count} votes

1 answer

Sort by: Most helpful
  1. ICodeInTx 56 Reputation points
    2021-01-24T15:11:28.617+00:00

    I'm starting to make progress. I finally decided the code had to be correct since there isn't much code anyway so I looked into the SQL database that I'm using for Identity. I'm leaning to the opinion that IdentityManager isn't doing what it needs to do.

    The AspNetRoles contains the roles:
    59906-image.png

    AspNetUserClaim reflects the checkboxes from the IdentityManager application
    59873-image.png

    However the AspNetUserRoles is empty.

    If I add the entry manually to the AspNetUserRoles table then the Blazor site works as expected.
    59961-image.png

    Now I have to figure out why the AspNetUserRoles table is not being populate and only the AspNetUserClaim is getting populated when checking the role for the user in IdentityManager.

    Maybe IdentityManager isn't the right tool to use??

    0 comments No comments