We have a .NET console application that synchronizes users and roles from an external system into ABP. It talks to the HTTP API only through ABP's dynamic HTTP client proxies (AbpIdentityHttpApiClientModule, AbpHttpClientIdentityModelModule).
- It authenticates with the client_credentials grant, using a confidential OpenIddict application with a client id and client secret. No user is involved.
- The application is created by our data seeder with the client_credentials grant type only.
- The client is granted the required permissions directly (permission provider "C"), e.g.: AbpIdentity.Users, AbpIdentity.Users.Create, AbpIdentity.Users.Update, AbpIdentity.Users.Update.ManageRoles, AbpIdentity.Roles, AbpIdentity.Roles.Create, ...
- Since it is a client and not a user, it has no roles. ABP doesn't support assigning roles to OpenIddict applications.
appsettings of the console app: "IdentityClients": { "Default": { "GrantType": "client_credentials", "ClientId": "MOD_Synch_App", "ClientSecret": "***", "Authority": "https://<auth-server>", "Scope": "PSSX" } }
The app calls IIdentityUserAppService.CreateAsync(new IdentityUserCreateDto { ..., RoleNames = new[] { "SomeRole" } }).
Problem
This worked with 9.1.0. After upgrading to 10.6.0, CreateAsync fails with HTTP 500 whenever RoleNames is set:
System.InvalidOperationException: Nullable object must have a value. at Volo.Abp.Identity.IdentityUserAppService.FilterRolesByCurrentUserAsync(IdentityUser user, String[] inputRoleNames) at Volo.Abp.Identity.IdentityUserAppService.UpdateUserByInput(IdentityUser user, IdentityUserCreateOrUpdateDtoBase input) at Volo.Abp.Identity.IdentityUserAppService.CreateAsync(IdentityUserCreateDto input)
The permission check passes, otherwise we would get a 403. The failure happens in the new role filtering logic. Our understanding of it:
- If the current user is in the admin role, the requested roles are assigned as is.
- Otherwise, it loads the current user via CurrentUser.GetId() to restrict the roles to the ones the caller has.
For a client_credentials caller:
- CurrentUser.IsInRole("admin") is always false, because a client has no roles.
- CurrentUser.Id is null, so CurrentUser.GetId() throws.
So no permission configuration can make this work for a client. UpdateRolesAsync fails the same way.
Steps to reproduce
- Create a confidential OpenIddict application with the client_credentials grant.
- Grant it AbpIdentity.Users.Create and AbpIdentity.Users.Update.ManageRoles.
- Get a token with client_credentials and call POST /api/identity/users with a non-empty roleNames.
- Result: 500 "Nullable object must have a value."
What we expect from ABP
- Confirmation whether this is a bug: the new logic doesn't seem to handle callers without a user (client_credentials).
- A fix where machine-to-machine clients with the required permissions (e.g. ManageRoles) can assign roles as in 9.x, or at least get a proper authorization error instead of an InvalidOperationException.
- The recommended way to assign roles from a client_credentials client in 10.x until a fix is released.
- Feedback on our workaround. In our custom IdentityUserAppService (which already replaces the default one), we override:
protected override async Task<string[]> FilterRolesByCurrentUserAsync(IdentityUser user, string[] inputRoleNames) { if (CurrentUser.Id == null && <current client is our sync client>) { return inputRoleNames; }
return await base.FilterRolesByCurrentUserAsync(user, inputRoleNames);
}
Is this safe and supported?
Check the docs before asking a question: https://abp.io/docs/latest Check the samples to see the basic tasks: https://abp.io/docs/latest/samples The exact solution to your question may have been answered before, and please first use the search on the homepage.
Provide us with the following info:
đ§ Hint: If you are using the ABP Studio, you can see all the information about your solution from the configuration window, which opens when you right-click on the solution and click on the Solution Configuration button.
- Exception message and full stack trace:
- Steps to reproduce the issue:
1 Answer(s)
-
0
Yes â this is a bug/regression in ABP 10.x behavior for
client_credentialscallers.
IdentityUserAppService.FilterRolesByCurrentUserAsync(...)usesCurrentUser.IsInRole(...)and then falls back to loading the current user viaCurrentUser.GetId(). Forclient_credentials, there is no user id claim, soCurrentUser.IdisnullandCurrentUser.GetId()throwsInvalidOperationException: Nullable object must have a value, producing HTTP 500 instead of a 403/validation error.Recommended way (until ABP fixes it): perform role assignment under a user context (impersonation/synthetic principal)
For machine-to-machine scenarios, ABP providesICurrentClientto represent the client identity, but Identityâs user-role assignment logic is still written around user context (ICurrentUser).
A robust workaround is to run the role-assignment part by temporarily switching the current principal to a âsystem/adminâ user (soCurrentUser.Idis not null), usingICurrentPrincipalAccessor.Change(...), then call the base implementation.Example pattern:
using System.Security.Claims; using Volo.Abp.Security.Claims; using Volo.Abp.Users; // build a principal that includes AbpClaimTypes.UserId for a real user (e.g., admin/system user) var principal = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(AbpClaimTypes.UserId, adminUserId.ToString()), new Claim(AbpClaimTypes.UserName, "system") // optionally add role claims if needed by your logic })); using (_currentPrincipalAccessor.Change(principal)) { // call the identity user app service create/update that assigns roles }About your workaround (overriding
FilterRolesByCurrentUserAsyncand returninginputRoleNameswhenCurrentUser.Id == null)
It will avoid the exception, but it effectively says: âany client_credentials caller that passes your check can assign any roles it wants, regardless of ABPâs filtering modelâ.
If you keep it, you should at least gate it using the client identity, not heuristics:- check
ICurrentClient.IsAuthenticatedandICurrentClient.Id(client id claim-based identity) - ensure the client is exactly the expected synchronization client id
- and still keep permission checks (e.g.,
AbpIdentity.Users.Update.ManageRoles) as you already do.
In other words, the idea is workable, but itâs application-specific and not something I can call âframework-supportedâ; itâs a targeted bypass for a framework bug.
- check
Practical safest workaround right now
- Detect machine-to-machine calls via
ICurrentClient(notICurrentUser). - For those calls, switch to a dedicated âsystem userâ principal (preferred), or apply your override with strict client-id gating.
- Keep permissions restricted to only what that sync client needs.
- Detect machine-to-machine calls via
Sources (verified):
- https://abp.io/docs/10.6/framework/infrastructure/current-user
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.
Markdown supported.Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
