Hi Team,
We are using ASP.NET Zero V 13.3.0 with .NET Core and Angular.
We created permission definitions and are using them in the applications. These permissions are currently dynamic and are not being stored in the AbpPermissions table.
How can we store these permissions in a SQL table so they can be shared across microservices?
3 Answer(s)
-
0
Hi @conorcorr
The permissions defined in the
AppAuthorizationProviderwithin the application are reflected in the database table when the application is run. Did you check the database table (AbpPermissions) after adding the permissions?Ensure that the Authorization provider is added in the
YourProjectNameApplicationModuleas shown below.public override void PreInitialize() { //Adding authorization providers Configuration.Authorization.Providers.Add<AppAuthorizationProvider>(); //... }Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image) -
0
Hi @oguzhanagir,
Thanks for the response. Here’s the behavior I'm observing:
* When a new user is created, there are no entries in the AbpPermissions table. * When I edit a permission for that user, an entry is then created in the table.The behavior differs based on the user’s role:
For the Admin role:
* An entry is created in AbpPermissions only when a permission is explicitly removed (IsGranted = false).For the User role:
* An entry is created in AbpPermissions when a permission is explicitly added (IsGranted = true).Is there a way to insert all permission entries for a user—at least with IsGranted = true—by default, regardless of the user’s role?
Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image) -
0
Hi @conorcorr
The behavior you're seeing is not a bug it's a result of the ASP.NET Boilerplate - Zero's intentional design, which is built for efficiency.
Why the
AbpPermissionsTable Works This WayThe core of ASP.NET Boilerplate is to only save overrides from the default state to the database. This approach avoids unnecessarily bloating the database and improves performance.
Permission Definitions: All permissions you define in your
AppAuthorizationProviderare managed in memory by theIPermissionManager, not in the database. They represent the universe of all possible permissions in the system.Default Behavior:
- Admin Role: The
Adminrole is typically configured to have all permissions granted by default. Therefore, when you revoke a permission from theAdminrole (IsGranted = false), this is an exception. A row is inserted into the database to record this specific "exception." As long as the permission is granted, no record is needed because it follows the default behavior. - User Role (or other standard roles): Standard roles usually have no permissions granted by default. Therefore, when you grant a permission to such a role (
IsGranted = true), this is also a deviation from the default state, and a record is added to the database to reflect this grant.
- Admin Role: The
The Purpose of the
AbpPermissionsTable: This table is not a list of all permissions. It is a table of overrides that supersede the in memory default settings for a specific user or role. When a permission check occurs (IsGrantedAsync), the system follows this structure:- First, it checks
AbpPermissionsfor a user specific setting. - If none exists, it checks
AbpPermissionsfor settings related to the user's roles. - If still no setting is found, it applies the default setting defined in memory (in the
Permissiondefinition).
- First, it checks
How to Store All Permissions in the Database?
Your request to save all
IsGranted = truepermissions for a user to the database goes against the framework's natural flow. While you could do this manually, it is not recommended for the following reasons.- Database Bloat: Creating hundreds of permission rows for every single user would cause the
AbpPermissionstable to grow massive very quickly. - Performance Degradation: This could lead to more database queries during permission checks.
- Maintenance: When you add a new permission to your application, you would need an additional mechanism to manually insert it for all existing users.
Sharing Permission Definitions Across Microservices
Since your ultimate goal is to share permission definitions across microservices, the correct approach is through code sharing, not by relying on the database for definitions.
Create a Shared Core Library
Create a New Project: Add a new library project to your solution, named something like
.SharedMove the
AuthorizationProvider: Move yourAppAuthorizationProvider.csfile and its related constant classes (likeAppPermissions) into this new shared project.Add a Reference: Add a reference to this new shared project from all of your microservices (including your main monolithic project, if you have one).
Register in the
Module: In each microservice's respective*ApplicationModuleor*CoreModulefile, ensure you register the sharedAuthorizationProvider:public override void PreInitialize() { // Adding authorization providers from the shared library Configuration.Authorization.Providers.Add<AppAuthorizationProvider>(); }
With this method, all your microservices will load the exact same permission definitions into their memory on startup. Consequently, a check for permission x in one service will be understood and recognized by all other services.
Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image)