Open Closed

Client credentials client cannot create users with roles in ABP 10.x – FilterRolesByCurrentUserAsync throws "Nullable object must have a value" #10962


User avatar
0
ElifKaya created

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

  1. Create a confidential OpenIddict application with the client_credentials grant.
  2. Grant it AbpIdentity.Users.Create and AbpIdentity.Users.Update.ManageRoles.
  3. Get a token with client_credentials and call POST /api/identity/users with a non-empty roleNames.
  4. Result: 500 "Nullable object must have a value."

What we expect from ABP

  1. Confirmation whether this is a bug: the new logic doesn't seem to handle callers without a user (client_credentials).
  2. 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.
  3. The recommended way to assign roles from a client_credentials client in 10.x until a fix is released.
  4. 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:
Markdown supported.
Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)

1 Answer(s)
  • User Avatar
    0
    AI-Bot created
    Support Team ABP Agent
    • Yes — this is a bug/regression in ABP 10.x behavior for client_credentials callers.
      IdentityUserAppService.FilterRolesByCurrentUserAsync(...) uses CurrentUser.IsInRole(...) and then falls back to loading the current user via CurrentUser.GetId(). For client_credentials, there is no user id claim, so CurrentUser.Id is null and CurrentUser.GetId() throws InvalidOperationException: 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 provides ICurrentClient to 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 (so CurrentUser.Id is not null), using ICurrentPrincipalAccessor.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 FilterRolesByCurrentUserAsync and returning inputRoleNames when CurrentUser.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.IsAuthenticated and ICurrentClient.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.

    • Practical safest workaround right now

      1. Detect machine-to-machine calls via ICurrentClient (not ICurrentUser).
      2. For those calls, switch to a dedicated “system user” principal (preferred), or apply your override with strict client-id gating.
      3. Keep permissions restricted to only what that sync client needs.

    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)
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.8.0-preview. Updated on November 16, 2026, 06:26
1
ABP Assistant
🔐 You need to be logged in to use the chatbot. Please log in first.