We are developing an Office Add-in that authenticates with an organisational account to Azure AD. The Add-in needs administrative consent. So if an administrator is logged on, he should be guided to express his administrativ consent.
We are using OAuth to authenticate:
https://login.microsoftonline.com/common/oauth2/authorize?response_type=id_token&client_id=<clientId>&redirect_uri=<redirectUri>
and we request admin consent by appending &prompt=admin_consent
to that URL
Question 1. How can we test if that admin consent has already been successfully given, so we only need to ask the administrator to give consent if he didn't previously?
Question 2. How can we check if an updated version of the Add-in possibly now needs more permissions and inform users and administrator about that new requirements?
tl;dr
Yes, you can do this. You'll want to call this MS Graph endpoint, and inspect the oAuth2PermissionGrant object for the
consentType
field being set toAllPrincipals
.Some Background
Using the Microsoft Graph, you can identify if admin consent was granted. When Admin Consent is granted, there are OAuth2.0 permission grants written on the app.
Inside each permission grant, there's a field that indicates the permission level of the grant. For Admin Consent, you would be looking for
AllPrincipals
.Detailed Steps
App Role:
Directory.Read.All
&Directory.ReadWrite.All
Delegated Permission:
Diretory.Read.All
,Directory.ReadWrite.All
, orDirectory.AccessAsUser.All
in order of least to most privileged.This returns back an oAuth2PermissionGrant object with the details you're looking for.
consentType
field. You may need to enumerate all the grants looking for the valueAllPrincipals
.IMHO, the custom implementation would be a better choice for your usecase
The steps could be the following
After the user approves of his admin access, we typically get the status in the response back from Azure AD like the one below,
GET http://localhost/myapp/permissions?tenant=a8990e1f-ff32-408a-9f8e-78d3b9139b95&state=state=12345&admin_consent=True
The App now stores the admin consent grant status in the DB.
In case of reading more about the steps, please click here