I noticed that if i don't add delegated api permissions to my app registration and request explicit scopes during auth like 'File.ReadWrite' instead of the './default', if the user consents the app still works. I am confused because to some extent i expected this to not work, as the permissions were not allowed by admin in the app registration. Why does it work?
if i do use './default' and a new scope is added later by the admin, if the old scopes were granted and a user oauth is retriggered, it will not ask the user for consent again and instead generate a token with the old scopes only -- that is why i explicitly used option 1 but ran into the above caveat
I noticed that if i don't add delegated api permissions to my app registration and request explicit scopes during auth like 'File.ReadWrite' instead of the './default', if the user consents the app still works. I am confused because to some extent i expected this to not work, as the permissions were not allowed by admin in the app registration. Why does it work?
if i do use './default' and a new scope is added later by the admin, if the old scopes were granted and a user oauth is retriggered, it will not ask the user for consent again and instead generate a token with the old scopes only -- that is why i explicitly used option 1 but ran into the above caveat
1 Answer
Reset to default 1Note that: If you explicitly request
File.ReadWrite
but haven’t configured this permission in your app registration, Azure AD will throw an error.
- When you use a scope like
Files.ReadWrite
, your app registration needs to include the corresponding permission.
I created a Microsoft Entra ID application, dint not grant any API permission except User.Read
:
Now, I tried to use the below endpoint to authorize users:
https://login.microsoftonline.com/TenantID/oauth2/v2.0/authorize?
&client_id=ClientID
&response_type=code
&redirect_uri=https://jwt.ms
&response_mode=query
&scope=Files.ReadWrite
&state=12345
When I logged in as a user, I got the error:
When I tried to request explicit scopes which is not added to application that is Files.ReadWrite
I got the error.
But a when I signed with Global Admin, I got the consent screen as Global admin can grant the permission on behalf of users:
If the Global admin clicks on `Consent on behalf of your organization':
The Files.ReadWrite
API permission will be added under Other permissions granted for TenantName
Make sure to set user and consent settings as "Do not allow user consent"
And if the API permission is granted under other permissions granted, then the user will be able to explicitly call the permissions.
- The Global Admin consent on behalf of the organization is the key mechanism that makes your app work with permissions that were not initially configured in the app registration.
- If you use the
./default
scope and new permissions are added later, users won't be asked for consent again once those permissions are granted by the Global Admin, as long as the user has an active token.
The issue you are facing is because, you have set the user and consent settings as "Allow user consent for apps":
And when I tried I got the user consent screen for the API permission which are not added to the app:
UPDATE PASTING PICTURES:
User consent screen:
API permissions:
Enterprise application:
Got consent screen as you:
To resolve the issue, you need to set user and consent settings as "Do not allow user consent" like this and After setting the consent settings as "Do not allow user consent" wait for 10-15 mins and then try again you will get error "Need admin approval"
For low impact of security you can add permissions in Permission classifications balde which permissions the user can consent:
Select "Allow user consent for apps from verified publishers, for selected permissions (Recommended)"
And select permissions for which the user can consent.
File.ReadWrite
) during authentication, Azure AD allows dynamic consent based on what the user grants, even if those permissions aren't pre-registered in the app. However, when using./default
, it uses the permissions defined in the app registration, and new permissions won’t take effect unless the user reconsents in a new OAuth flow after the app registration is updated. – Rukmini Commented Feb 7 at 3:31