azure-gov

Is there a way to know which aad to use for authenticating in advance?


I have a UWP app that needs to authenticate, and I would like to avoid asking the user to choose which national cloud to authenticate with. I could just try them all, but I hope there is a better way to tell which Azure Active Directory the user belongs to (.us or .com)


Solution

  • Native apps can discover the Azure AD endpoint for a national cloud by passing an instance_aware parameter in the authorization request to the global Azure AD endpoint. This is done in the acquireToken call, where you need to pass in instance_aware = true as an extra query parameter when initializing the Authentication context.

    From the Authentication result, you can read and store the cloud_instance_host_name attribute to learn the correct Azure AD endpoint. You must pass this value as the authority to re-initialize the Authentication context for the subsequent acquireTokenSilent calls to succeed.

    An example ADAL.Net code snippet is below:

    var authenticationContext= new AuthenticationContext(Authority, false, new TokenCache());
    
    var authenticationResult = await authenticationContext.AcquireTokenAsync(resource,  
                                            clientId,  
                                            redirectUri,  
                                            platformParameters,  
                                            userIdentifier,  
                                            "instance_aware=true"
                                            );
    

    Also, here are example OAuth request and responses using the instance_aware parameter:

    Request:

    https://login.microsoftonline.com/common/oauth2/authorize?
    response_type=code&
    client_id=f5d01c1c-abe6-4207-ae2d-5bc9af251724&
    instance_aware=true&
    redirect_uri=http://localhost/appcheck&
    resource=00000002-0000-0000-c000-000000000000
    

    Response:

    http://localhost/appcheck?
    code=AQABAAIAAQDnLpu3ikefR73l_aNlxt5x0ulCIcjaTlOoWp412SJ2Oxlih65_h_Ju3OdOqpEy-mz0giFzZtU2_MbIgSG12e6RjwxpcaXaVPene_lMtmR2DPexUZZ3QhFRl8Vgl76SidX_nJ1CN-hJAejCi139FG_YZit4ePbiNySC3zR9GcP3B3St7HDsdEhMh1Vi1XHSSKfpgVqzLnOiBSO_jXrm1WJVqXSlt4_M_KO92Gdpbpy8H7zpsRg0O6blbuSw_83YUcj0w1gEfByHZP2Hk5AToDy_DWepPqJ0GWOJYeKcfIiEFleNYaeyEJDDuMyFhV16IOT28mq1oNOWL0dnhjwr-OV0JnyajQCT_LZzapxp7Y-8jSPDgW6SR878sgrq6CS2z3Zos8_T31n4DucQaPqv2Ae_jxlGHHSENBFy2RhHy397B7BBohXGqhDj_OdIroimDOJGVewn612gQOA6-9p0llv-PNd7vj9VZL-9Q8kEuYuhTqaBsH3yKm6y9FfgxMWovVkYtDt4YgxbqCV2Wb_lzImtyTHKxazn6YhH6R2pCvFdVSAA&
    cloud_instance_name=microsoftonline.us&
    cloud_instance_host_name=login.microsoftonline.us&
    cloud_graph_host_name=graph.windows.net&
    msgraph_host=graph.microsoft.com&
    session_state=899b8a55-034f-4dcd-8b4b-888b7874b041
    

    One way to "spot check" which cloud/AAD environment the user belongs to is by making a call to the Azure AD OpenID Connect discovery endpoint:

    https://login.microsoftonline.com/place_tenantname_or_tenantid_here/.well-known/openid-configuration

    For Azure Government, "USG" or "USGov" as the value in the tenant_region_scope field will indicate tenants that should be using login.microsoftonline.us.

    I hope this helps. Let me know if not.

    Bernie