I am writing a web app using the Security API within Microsoft Graph and I'm authenticating with Azure AD, but the permissions needed to access the API require admin consent for every tenant that uses my app.
How can I check if the tenant admin has given consent to my app? That way I know whether to send the user to the regular login flow or to the admin consent flow.
Is it possible to make a REST call to Azure AD for this information? I’ve tried using oAuth2PermissionGrant
but that only seems to work with the Object ID and I only have my app/client ID.
Edit:
I was confused about which scopes were needed in my app, but it turns out that when I add the admin only delegated permissions like SecurityEvents.Read.All
and SecurityEvents.ReadWrite.All
to just the Application Registraion Portal and only use the User.Read
permission in my app to request the users basic info. There are two different outcomes when requesting an access token;
- If the admin hasn't given my app consent, then the access token will only contain
User.Read
scope. - If the admin has given consent, then the access token will contain
User.Read
,SecurityEvents.Read.All
, andSecurityEvents.ReadWrite.All
delegated scopes. Even though my app only requestsUser.Read
.
I found that I can use this to determine if the admin has given consent to my app by checking the access token scopes. If it contains SecurityEvents.Read.All
, or SecurityEvents.ReadWrite.All
then the user can continue in the app. But if those delegated permissions are not in the scope then I can prompt the https://login.microsoftonline.com/common/adminconsent?client_id=<APP ID>&state=12345&redirect_uri=http://localhost/myapp/permissions
admin consent flow to request the permissions to be added to the tenant.
Thank you @marc-lafleur, I was using the admin account to play with the oAuth2PermissionGrant
in Graph Explorer and I didn't catch the chicken vs. egg scenario until you pointed it out.