Open Closed

Blazor authorization #9474


User avatar
0
carl.hallqvist created

exceptionWe have problem to get https://abp.io/docs/latest/framework/ui/blazor/authorization to work in our Blazor project. This is what we have:

@page "/"
@using Microsoft.AspNetCore.Authorization
@attribute [Authorize("CustomerService.Customers")]
@inherits MyProjectComponentBase

@* https://abp.io/docs/latest/framework/ui/blazor/authorization *@
#AuthorizeView Policy="CustomerService.Customers.Create">
    <p>You can only see this if you satisfy the "CustomerService.Customers.Create" policy.</p>
#/AuthorizeView>

The #  is instead of < since CB does render it? A bug in the Write component maybe?

This is the MyProject_Administration database, AbpPermissions table:

And this is the exception in the browser:


5 Answer(s)
  • User Avatar
    0
    AI-Bot created
    Support Team ABP Agent
    • The error The AuthorizationPolicy named: 'CustomerService.Customers' was not found. indicates that you are using ASP.NET Core's policy-based authorization, but you have not registered a policy named CustomerService.Customers in your application's authorization configuration.

    • In ABP Blazor applications, permissions are typically checked using the ABP permission system, not by defining custom ASP.NET Core authorization policies for each permission. The correct way to check for a permission in a Blazor component is to use the @attribute [Authorize] without a policy name, or to use the AbpAuthorizeView component for permission checks.

    • To check for a specific permission, use the ABP permission name in the AbpAuthorizeView component, like this:

    @page "/"
    @using Volo.Abp.AspNetCore.Components.Authorization
    
    @attribute [Authorize] <!-- This just requires authentication -->
    
    <AbpAuthorizeView Permission="CustomerService.Customers.Create">
        <p>You can only see this if you have the 'CustomerService.Customers.Create' permission.</p>
    </AbpAuthorizeView>
    
    • If you want to use the standard AuthorizeView, you must define the policy in your Startup.cs or Program.cs:
    services.AddAuthorization(options =>
    {
        options.AddPolicy("CustomerService.Customers", policy =>
            policy.RequireClaim("Permission", "CustomerService.Customers"));
    });
    

    But this is not the recommended ABP way.

    • Make sure you are using the correct ABP permission system and components for permission checks in Blazor.

    Sources:

    • https://abp.io/docs/latest/framework/ui/blazor/authorization

    This response was generated by AI. If it didn’t solve your problem, feel free to post a new answer — our support team will jump in to help you soon.

  • User Avatar
    0
    carl.hallqvist created

    Thanks AI,

    I tried your suggestion but it did not worked, I think that @using Volo.Abp.AspNetCore.Components.Authorization is not valid.

    Please let the humans take a look at it :-)

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    What is your project type? Microservice?

    Does your blazor project reference the Contracts project that defines the PermissionDefinitionProvider?

    Thanks.

  • User Avatar
    0
    carl.hallqvist created

    Thanks mailing, that was the problem! You need to copy the contracts into the blazor project.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    : )

Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.0.0-preview. Updated on July 11, 2025, 11:35