Enterprise Application User and Role Management via API

Roy, Sudip 0 Reputation points
2026-11-16T03:38:46.6+00:00

We have several custom Enterprise Applications hosted in Microsoft Entra ID (Azure AD). We require API-based automation to manage application access and authorization.

The solution should support the following operations:

1. User Management

  • Add users to an Enterprise Application.
  • Remove users from an Enterprise Application.
  • Retrieve users assigned to an Enterprise Application.

2. Application Role Management

  • Create new application roles for the Enterprise Application.
  • Update or remove existing application roles.
  • Retrieve available application roles.

3. Role Assignment

  • Assign application roles to users.
  • Remove application role assignments from users.

Technical Requirements

  • All operations should be performed programmatically using Microsoft Graph APIs or any other Microsoft provided API.
  • Support delegate permission only.
  • The solution should follow the principle of least privilege and Microsoft Entra ID security best practices.

Expected Outcome

Provide a centralized API-based mechanism to automate user access management for Enterprise Applications, including user assignments and role-based access control (RBAC), eliminating manual administration through the Microsoft Entra portal.

 

Microsoft Security | Microsoft Entra | Microsoft Entra ID
0 comments No comments

2 answers

Sort by: Most helpful
  1. Vasileios Dionysopoulos 826 Reputation points
    2026-11-17T06:15:52+00:00

    Hi Sudip,

    The existing answer covers the Graph endpoints well. I'd change one thing for least privilege: the operator's role, not the scopes.

    With delegated permissions, the effective access is the intersection of the token's scopes and what the signed-in user is allowed to do. AppRoleAssignment.ReadWrite.All and Application.ReadWrite.All sound tenant-wide, but if the operator is only an owner of your custom apps, the same token can only change those apps. That avoids giving anyone Privileged Role Administrator, which can also grant Microsoft Graph permissions to any app.

    Setup per application

    1. Add the operator (or a security group of operators) as Owner of both the App registration and the Enterprise application. Alternatively, assign Cloud Application Administrator scoped to that single app registration (Roles and administrators → Add assignment → scope: the application).
    2. Grant admin consent once for the client app's delegated scopes: Application.ReadWrite.All, AppRoleAssignment.ReadWrite.All.

    Define roles on the app registration, not the service principal

    Because these are your own apps, manage the roles on the application object. They then propagate to the enterprise app, and they survive if the service principal is ever recreated:

    PATCH https://graph.microsoft.com/v1.0/applications/{app-object-id}
    { "appRoles": [ ...full existing array plus the new or updated role... ] }
    

    User assignment and removal stay as described in the other answer: POST / GET / DELETE on /servicePrincipals/{sp-id}/appRoleAssignedTo.

    Quick test: sign in to Graph Explorer as a user who is only an owner of app A. Assigning a user to app A should succeed, and assigning to app B should return 403. That confirms the scoping before you build the automation on it.

    Was this answer helpful?

    0 comments No comments

  2. AI answer

    2026-11-16T03:40:53.9466667+00:00

    Microsoft Graph v1.0 can provide the centralized mechanism, using the Enterprise Application’s service principal object ID and its appRoleAssignedTo relationship for assignments, plus appRoles for tenant-local role definitions.

    Delegated-only automation must run with a signed-in work or school user—through interactive authentication or an on-behalf-of flow. It is not suitable for a fully unattended daemon with no user context because delegated permissions act on behalf of a signed-in user.^1^

    1. Permissions and operator roles

    Request only the delegated scopes required by each function:

    Operation Delegated permission
    Find/read service principals and roles Application.Read.All
    List assignments for an Enterprise Application Application.Read.All
    Create/delete assignments AppRoleAssignment.ReadWrite.All
    Create/update/delete service-principal app roles Application.ReadWrite.All

    Reading a service principal uses Application.Read.All.^2^ Assignment writes use AppRoleAssignment.ReadWrite.All; the signed-in user must also hold a supported Entra role. For assignment operations, Privileged Role Administrator is the least-privileged role identified specifically for Microsoft Graph app-role administration, although several other administrative roles are supported.^3^

    For tenant-local Enterprise Application role creation, Microsoft’s procedure specifically requires a Privileged Role Administrator. Its Graph Explorer instructions request both Application.ReadWrite.All and Directory.ReadWrite.All, while the underlying update-servicePrincipal API identifies Application.ReadWrite.All as the least-privileged delegated permission. For a custom client, start with the API’s least-privileged permission and add broader permission only if another operation requires it.^4^^5^

    2. Resolve the Enterprise Application

    An Enterprise Application is represented by a service principal. Resolve it by application/client ID:

    GET https://graph.microsoft.com/v1.0/servicePrincipals(appId='{application-client-id}')?$select=id,appId,displayName,appRoles,appRoleAssignmentRequired
    Authorization: Bearer {delegated-token}
    

    Keep the returned id; it is the service principal object ID, not the application/client ID.

    3. List, add, and remove assignments

    List all direct assignments to the application:

    GET https://graph.microsoft.com/v1.0/servicePrincipals/{resource-sp-id}/appRoleAssignedTo
    Authorization: Bearer {delegated-token}
    

    This returns assignments whose principals can be users, groups, or service principals.^6^ Follow every @odata.nextLink until absent. Group entries represent group assignments; they are not expanded into every user who receives effective access through membership.

    Assign a user:

    POST https://graph.microsoft.com/v1.0/servicePrincipals/{resource-sp-id}/appRoleAssignedTo
    Authorization: Bearer {delegated-token}
    Content-Type: application/json
    
    {
      "principalId": "{user-object-id}",
      "resourceId": "{resource-sp-id}",
      "appRoleId": "{app-role-id}"
    }
    

    The payload requires principalId, resourceId, and appRoleId.^7^ If the application exposes no explicit roles and supports assignment without a role, use the default-access role ID:

    00000000-0000-0000-0000-000000000000
    

    Remove the assignment using the assignment object’s id:

    DELETE https://graph.microsoft.com/v1.0/servicePrincipals/{resource-sp-id}/appRoleAssignedTo/{assignment-id}
    Authorization: Bearer {delegated-token}
    

    Success returns 204 No Content. Microsoft recommends deleting through the resource service principal’s appRoleAssignedTo relationship.^8^

    Deleting an assignment removes that assignment. It blocks application sign-in only when the Enterprise Application has appRoleAssignmentRequired=true, or when another authorization layer enforces equivalent restrictions. Changing this property affects sign-in behavior, so enable it only when assigned-user-only access is intended:

    PATCH https://graph.microsoft.com/v1.0/servicePrincipals/{resource-sp-id}
    Authorization: Bearer {delegated-token}
    Content-Type: application/json
    
    { "appRoleAssignmentRequired": true }
    

    4. Create or update tenant-local app roles

    First GET the complete appRoles collection. Append a new role or modify the required existing object, then PATCH the entire resulting collection:

    PATCH https://graph.microsoft.com/v1.0/servicePrincipals/{resource-sp-id}
    Authorization: Bearer {delegated-token}
    Content-Type: application/json
    
    {
      "appRoles": [{
        "allowedMemberTypes": ["User"],
        "description": "Approves business requests",
        "displayName": "Approver",
        "id": "11111111-2222-3333-4444-555555555555",
        "isEnabled": true,
        "origin": "ServicePrincipal",
        "value": "Approver"
      }]
    }
    

    The example shows one role for brevity; the actual PATCH must include every existing role that must remain. Omitting existing entries removes them from the collection.^5^

    To delete a role safely:

    1. GET and retain the complete appRoles array.
    2. PATCH the complete array with the target role’s isEnabled set to false.
    3. PATCH again with only that disabled role removed.
    4. Preserve every other role unchanged.

    For custom applications where roles should be part of the application’s durable definition rather than tenant-local customization, manage them on the backing app registration instead; the service-principal procedure above specifically manages Enterprise Application-local roles.


    References

    1. Overview of Microsoft Graph permissions
    2. Get servicePrincipal
    3. Grant an appRoleAssignment to a user
    4. Update servicePrincipal
    5. Configure the role claim
    6. List appRoleAssignments granted for a service principal
    7. Grant an appRoleAssignment for a service principal
    8. Delete appRoleAssignedTo
    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.