Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Authorization issue when use [Authorize] attribute in page in client blazor app

When i use

@attribute [Authorize]

on a razor page in blazor Client app it give me this error

System.InvalidOperationException: Cannot provide a value for property 'AuthorizationPolicyProvider' on type 'Microsoft.AspNetCore.Components.PageDisplay+AuthorizeViewWithSuppliedData'. There is no registered service of type 'Microsoft.AspNetCore.Authorization.IAuthorizationPolicyProvider'

I set the authentication and use custom AuthenticationStateProvider for the client side as following

 public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddScoped<AuthenticationStateProvider, ApiAuthenticationStateProvider>();
            services.AddSingleton<AuthService, AuthService>();
            services.AddBlazoredLocalStorage();

        }

        public void Configure(IComponentsApplicationBuilder app)
        {
            app.AddComponent<App>("app");
        }
    }

any help with this issue

like image 848
Shinichi Kudo Avatar asked Sep 03 '19 16:09

Shinichi Kudo


People also ask

How do I Authorize a page on Blazor?

[Authorize] attribute Only use [Authorize] on @page components reached via the Blazor Router. Authorization is only performed as an aspect of routing and not for child components rendered within a page. To authorize the display of specific parts within a page, use AuthorizeView instead.

How do I add authentication to Blazor app?

There is an option available to enable authentication for the Blazor app when you create the application. To enable authentication for Blazor server app, click on “Change” under “Authentication section and select “Individual User Accounts” option and then click on “Ok” button when you create a new Blazor server app.

Where can the Authorize attribute can be applied?

You can place the Authorize attribute on a controller or on individual actions inside the controller. When we place the Authorize attribute on the controller itself, the authorize attribute applies to all of the actions inside.


2 Answers

i just needed to add services.AddAuthorizationCore();

thanks for KodiakMx

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddBlazoredLocalStorage();
        services.AddAuthorizationCore();
        services.AddScoped<AuthenticationStateProvider, ApiAuthenticationStateProvider>();
    }
like image 77
Shinichi Kudo Avatar answered Oct 17 '22 00:10

Shinichi Kudo


You may need to add app.UseAuthentication(); to your Configure method in Startup class.

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
        app.UseHsts();
    }

    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseCookiePolicy();

    app.UseAuthentication();
    app.UseSession();
    app.UseMvc(routes =>
    {
        routes.MapRoute(
           name: "default",
           template: "{controller=Home}/{action=Index}/{id?}");
    });
}
like image 34
KodiakMx Avatar answered Oct 17 '22 00:10

KodiakMx