"Do not allow user consent" + "IMAP.AccessAsUser.All" = admin conset loop on OAuth2

Krystian Bigaj 6 Reputation points
2022-11-23T15:45:24.547+00:00

Hi.

I have a windows service application (it irrelevant) which access user email box (for sending and receiving mails - IMAP/POP3 + SMTP).
To make it work with MS email account (Exchange, Outlook) I have created "App registrations" with "API permissions" from "Microsoft Graph":

  • IMAP.AccessAsUser.All
  • POP.AccessAsUser.All
  • SMTP.Send
    263398-api.png

Authentication is done by "OAuth2 authorization code flow" https://learn.microsoft.com/en-us/exchange/client-developer/legacy-protocols/how-to-authenticate-an-imap-pop-smtp-application-by-using-oauth
and work fine unless end-user tenant have change option to "Do not allow user consent":
263516-consent.png

Even when administrator approves consent:
263521-admin-consent-done.png
user every time backs to that admin consent window:
263465-admin-consent.png

Scope for authorization is: offline_access https://outlook.office365.com/IMAP.AccessAsUser.All

If the scope is: offline_access https://graph.microsoft.com/IMAP.AccessAsUser.All the there is no admin-consent-loop (once tenant admin approves application, then authorization doesn't ask for admin approve), however then IMAP/POP/SMTP connection fails with authentication errors (like NO AUTHENTICATE failed.)

Any hint or ideas? Thanks.

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,589 questions
Exchange Server Development
Exchange Server Development
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Development: The process of researching, productizing, and refining new or existing technologies.
508 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Andy David - MVP 141.6K Reputation points MVP
    2022-11-23T16:08:54.87+00:00

    Per that doc, perms are application, not delegated as in your picture:

    263524-image.png

    Also make sure you register it as service principal in Exo:

    https://learn.microsoft.com/en-us/exchange/client-developer/legacy-protocols/how-to-authenticate-an-imap-pop-smtp-application-by-using-oauth#register-service-principals-in-exchange