Jeff Saremi Jeff Saremi - 1 month ago 54x
C# Question

How can I force the AdminConsent using AuthenticationContext.AcquireTokenAsync?

I have a native app which i'm using in a multi-tenant scenario.
To authenticate the user -- and to get their consent on allowing this application to access Azure on their behalf -- I simply instantiate an AuthenticationContext and call AcquireTokenAsync. However I don't know how if this by default uses the AdminConsent or not? If not how can i achieve that?

Below is the sample code that i use:

AuthenticationContext commonAuthContext = new AuthenticationContext("");
AuthenticationResult result = await commonAuthContext.AcquireTokenAsync(resource,
clientId, replyUrl,
new PlatformParameters(PromptBehavior.Always));


No, this does not automatically invoke admin consent (even if an admin consents, they're just consenting for themselves, not for the whole tenant).

To invoke admin consent, you have to add prompt=admin_consent to the authentication request:

AuthenticationResult result = await commonAuthContext.AcquireTokenAsync(
    new PlatformParameters(PromptBehavior.Auto), // <-- Important: use PromptBehavior.Auto
    "prompt=admin_consent"); // <-- This is the magic

Of course, you should not send all users to sign in with this, as it will fail if the user is not an admin.

See "Triggering the Azure AD consent framework at runtime":