简体   繁体   中英

How to use microsoft graph api for assigning role to the user in azure ad

I have created below role in the app registration manifest :

"appRoles": [
   {
      "allowedMemberTypes": [
        "User"
      ],
      "displayName": "Student",
      "id": "d1c2ade8-98f8-45fd-aa4a-6d06b947c66f",
      "isEnabled": true,
      "description": "Student",
      "value": "Student"
    }
  ],

Now I am using appRoleAssignment api to assign a role to the user. I am following this documentation . In this page, it says that we need to use below api with the json body:

POST https://graph.microsoft.com/v1.0/servicePrincipals/{id}/appRoleAssignments
Content-Type: application/json
Content-Length: 110

{
  "principalId": "principalId-value",
  "resourceId": "resourceId-value",
  "appRoleId": "appRoleId-value"
}

I am unable to understand what I should use in principalId , resourceId and appRoleId . As per that page, it says that:

principalId: The id of the client service principal to which you are assigning the app role.
resourceId: The id of the resource servicePrincipal (the API) which has defined the app role (the application permission).
appRoleId: The id of the appRole (defined on the resource service principal) to assign to the client service principal.

But what I could understand is that principalId is the ID of the user I have in the active directory for which I want to assign the role.

which in my case is the ObjectId in below photo:

在此处输入图片说明

is this correct.?

resourceId is the tennant id and appRoleId is the id I used while creating the app role above which is d1c2ade8-98f8-45fd-aa4a-6d06b947c66f

Putting it all together if I make a request in python

token = get_token()
headers = {'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json'}

user_data = {
    "principalId": "1bc79085-12qw-4fad-8da8-647f4b4b2927",  
    "resourceId": "c01b6482-3ccd-4533-8c98-a7c5e8067cc8",   
    "appRoleId": "d1c2ade8-98f8-45fd-aa4a-6d06b947c66f"  
}
j_data = json.dumps(user_data)
conn = http.client.HTTPSConnection('graph.microsoft.com')
conn.request("POST", "/v1.0/servicePrincipals/1bc79085-12qw-4fad-8da8-647f4b4b2927/appRoleAssignments", j_data, headers)
response = conn.getresponse()
rdata = response.read()

I am getting below response:

{
  "error": {
    "code": "Request_ResourceNotFound",
    "message": "Resource '1bc79085-12qw-4fad-8da8-647f4b4b2927' does not exist or one of its queried reference-property objects are not present.",
    "innerError": {
      "date": "2020-10-26T05:16:35",
      "request-id": "1c87a140-7bc9-499d-82dd-bc1dcb54e075",
      "client-request-id": "1c87a140-7bc9-499d-82dd-bc1dcb54e075"
    }
  }
}

Can anyone please help me debug this. Please help. Thanks

EDIT:

Error:

{
    "error": {
        "code": "Request_ResourceNotFound",
        "message": "Resource '261eda4b-6eee-45ba-a176-259960603409' does not exist or one of its queried reference-property objects are not present.",
        "innerError": {
            "date": "2020-10-26T07:09:38",
            "request-id": "8dc2ea73-63e5-45b5-8127-445df777c1e1",
            "client-request-id": "8dc2ea73-63e5-45b5-8127-445df777c1e1"
        }
    }
}

Json:

{
    "principalId": "f923e078-ca9d-4611-a80e-bebb712ad7d1",  
    "resourceId": "261eda4b-6eee-45ba-a176-259960603409",   
    "appRoleId": "d1c2ade8-98f8-45fd-aa4a-6d06b947c66f"  
}

Post URL: https://graph.microsoft.com/v1.0/servicePrincipals/261eda4b-6eee-45ba-a176-259960603409/appRoleAssignments

GET Url to get the object id: https://graph.microsoft.com/v1.0/serviceprincipals?$select=id&$filter=displayName eq '{useracces}'

在此处输入图片说明

POST https://graph.microsoft.com/v1.0/servicePrincipals/{id}/appRoleAssignedTo
Content-Type: application/json
Content-Length: 110

{
  "principalId": "principalId-value",
  "resourceId": "resourceId-value",
  "appRoleId": "appRoleId-value"
}

In this example, {id} and {resourceId-value} would both be the object id of the resource service principal, which is the enterprise app associated with the Azure AD app you have created appRoles in.

And {principalId-value} would be the object id of the user.

{appRoleId-value} is the id of the app role you created in manifest.

UPDATE:

The steps you get the object id of service principal are correct.

If you want to get it using Graph API, you can do it like this:

GET https://graph.microsoft.com/v1.0/serviceprincipals?$select=id&$filter=displayName eq '{app name}'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM