Adal.js does not get tokens for external api endpoint resource

前端 未结 4 1678
时光取名叫无心
时光取名叫无心 2020-12-28 08:00

I\'m trying out adal.js with an Angular SPA (Single Page Application) web site that gets data from an external Web API site (different domain). Authentication against the SP

4条回答
  •  北海茫月
    2020-12-28 08:49

    ADAL.js does get the access_token apart from id_token for calling Azure AD protected API running on different domain. Initially, during login, it only takes id_token. This token has the access for accessing resource of the same domain. But, on calling the API running in different domain, adal interceptor checks if the API URL is configured in as endpoint in adal.init().

    It is only then that the access token is called for the requested resource. It also necessitates that the SPA is configured in the AAD to access API APP.

    The key to achieve this is following: 1. Add endpoints in the adal.init()

    var endpoints = {
    
        // Map the location of a request to an API to a the identifier of the associated resource
        //"Enter the root location of your API app here, e.g. https://contosotogo.azurewebsites.net/":
        //    "Enter the App ID URI of your API app here, e.g. https://contoso.onmicrosoft.com/TestAPI",
        "https://api.powerbi.com": "https://analysis.windows.net/powerbi/api",
        "https://localhost:44300/": "https://testpowerbirm.onmicrosoft.com/PowerBICustomServiceAPIApp"
    };
    
    adalProvider.init(
        {
            instance: 'https://login.microsoftonline.com/',
            tenant: 'common',
            clientId: '2313d50b-7ce9-4c0e-a142-ce751a295175',
            extraQueryParameter: 'nux=1',
            endpoints: endpoints,
            requireADLogin: true,
    
            //cacheLocation: 'localStorage', // enable this for IE, as sessionStorage does not work for localhost.  
            // Also, token acquisition for the To Go API will fail in IE when running on localhost, due to IE security restrictions.
        },
        $httpProvider
        );
    
    1. Give permission to the SPA application in Azure AD to access the API application:

    You may refer this link for details : ADAL.js deep dive

提交回复
热议问题