Search code examples
dynamics-crmadaladal.js

issue with single sign on Azure active directory javascript library


We have single sign on enabled for our MS Dynamics 365 CRM instance to make a calls to an API hosted in Azure. On launch of CRM we have the following JavaScript that executes. This works most of the time, but on occasion we get "Invalid argument" popup. I am relatively new to using Adal.js and have no idea what is causing this. Any trouble shooting tips appreciated. Thanks in advance.

config = {
    ApiUrl: configData["ApiUrl"],
    SubscriptionKey: configData["SubscriptionKey"],
    trace: configData["trace"],
    AcceptHeader: configData["AcceptHeader"],
    ContentTypeHeader: configData["ContentTypeHeader"],
    tenant: configData["tenant"],
    clientId: configData["clientId"],
    tokenStoreUrl: configData["tokenStoreUrl"],
    cacheLocation: configData["cacheLocation"],
    GraphApi: configData["GraphApi"]
};
// Check For & Handle Redirect From AAD After Login
authContext = new window.AuthenticationContext(config);
var isCallback = authContext.isCallback(window.location.hash);

if (isCallback) {
    authContext.handleWindowCallback();
}

var loginError = authContext.getLoginError();
if (loginError) {
    console.log('ERROR:\n\n' + loginError);
}
authContext.popUp = true;
if (isCallback && !loginError) {
    window.location = authContext._getItem(authContext.CONSTANTS.STORAGE.LOGIN_REQUEST);
}
var user = authContext.getCachedUser();
if (!user) {                            
    authContext.clearCache(); 
    sessionStorage["adal.login.request"] = "";                           
    authContext.login();
}

window.parent.authContext = authContext;

enter image description here


Solution

  • It has been a while since I last looked at this, however I managed to get it resolved at the time. I implemented a locking mechanism, to ensure the login completes before trying to obtain a token.

    Here is the updated code:

    config = {
                                ApiUrl: configData["ApiUrl"],
                                SubscriptionKey: configData["SubscriptionKey"],
                                trace: configData["trace"],
                                AcceptHeader: configData["AcceptHeader"],
                                ContentTypeHeader: configData["ContentTypeHeader"],
                                tenant: configData["tenant"],
                                clientId: configData["clientId"],
                                tokenStoreUrl: configData["tokenStoreUrl"],
                                cacheLocation: configData["cacheLocation"],
                                GraphApi: configData["GraphApi"],
                                loadFrameTimeout: 10000
                            };
    
    
                        // Check For & Handle Redirect From AAD After Login
                        authContext = new window.AuthenticationContext(config);
                        var isCallback = authContext.isCallback(window.location.hash);
    
                        if (isCallback) {
                            authContext.handleWindowCallback();
                        }
    
                        var loginError = authContext.getLoginError();
                        if (loginError) {
                            // TODO: Handle errors signing in and getting tokens
                            console.log('ERROR:\n\n' + loginError);
                        }
                        authContext.popUp = true;
                        if (isCallback && !loginError) {
                            window.location = authContext._getItem(authContext.CONSTANTS.STORAGE.LOGIN_REQUEST);
                        }
                        var user = authContext.getCachedUser();
                        if (!user) {
                            authContext.clearCache();
                            sessionStorage["adal.login.request"] = "";
                            authContext.callback = function (error, token, msg) {
                                // remove lock
                                window.top.loginLock = null;
                                if (!!token) {
                                    getGraphApiTokenAndUpdateUser(authContext);
                                }
                                else {
                                    console.log('ERROR:\n\n' + error);
                                }
                            };
    
                            if (typeof (window.top.loginLock) == "undefined" || window.top.loginLock  == null) {
                                // Create lock
                                window.top.loginLock  = true;
                                authContext.login();
                            }
                        }
    
                        window.parent.authContext = authContext;