web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to implement single sign on both dynamic crm tenant with adal.js?

(0) ShareShare
ReportReport
Posted on by

I have implemented adal.js in my crm custom solution to call power bi rest api. When the token expires it is asking for login again with active directory login page. But I don't want this scenario. Since user already logged in into my dynamic crm online tenant so user should also be logged in into ADAL. It is really a cumbersome that user need to login again with anther login prompt to see the power bi report. 

Is it possible to implement single sign with adal.js into my dynamic crm online tenant? Below is my current code using adal.js:

var adalConfig = {
    clientId: POWER_BI_APP_ID,
    popUp: true,
    cacheLocation: "localStorage",
    redirectUri: 'test.crm6.dynamics.com'
    };
var adalAuthContext = new window['AuthenticationContext'](adalConfig);

var user = adalAuthContext.getCachedUser();
if (!user) {
    adalAuthContext.login();
}

if (adalAuthContext.isCallback(window.location.hash)) {
     adalAuthContext.handleWindowCallback();
}

adalAuthContext.acquireToken(resourceUrl, function (errorDesc, token, error) {
      if (error) {
          //acquire token failure
          if (adalConfig.popUp) {
             // If using popup flows
             adalAuthContext.acquireTokenPopup(resourceUrl, null, null, function (errorDesc, token, error) { });
                }
          else {
                // In this case the callback passed in the Authentication request constructor will be called.
                adalAuthContext.acquireTokenRedirect(resourceUrl, null, null);
          }
      }
      else {// success}
});


*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Shaminderpal Singh Profile Picture
    1,565 on at

    Hi,

    You need to implement adal.js to get token silently without issuing popup with prompt=none.

    Refer the article for complete setup-docs.microsoft.com/.../auth-silent-aad

    -Shaminder

  • Community Member Profile Picture
    on at

    if you have seen my code above, could you please tell me where I need to set prompt=none ?

  • Suggested answer
    Shaminderpal Singh Profile Picture
    1,565 on at

    Hi,

    You are using AcquireTokenPopup Method instead of that use below and add your logic accordingly

    function tokenSetup() {
    var endpoints = {
    orgUri: "https://*********.crm8.dynamics.com/"
    };
    window.config = {
    clientId: "cff1e855-c6e5-4e57-b16f-6b6427acf19c",
    tenant: '922d254f-cf35-49f3-8190-e5d67d388aa8',
    // redirectUri must be in the list of redirect URLs for the AAD app
    redirectUri: window.location.origin + "//WebResources/new_authentication",
    cacheLocation: "localStorage",
    navigateToLoginRequestUrl: false,
    endpoints: endpoints, callback: function (a) { console.log(a); }
    };

    authContext = new AuthenticationContext(config);
    var user = authContext.getCachedUser();

    // Check For & Handle Redirect From AAD After Login
    var isCallback = authContext.isCallback(window.location.hash);
    if (isCallback) {
    authContext.handleWindowCallback();
    }
    var loginError = authContext.getLoginError();

    if (isCallback && !loginError) {
    window.location = authContext._getItem(authContext.CONSTANTS.STORAGE.LOGIN_REQUEST);
    }
    else {
    console.log('Error');
    }

    var token = authContext.getCachedToken(config.clientId);
    if (token) {
    console.log(token);
    } else {
    // No token, or token is expired
    authContext._renewIdToken(function (err, idToken) {
    if (err) {
    console.log("Renewal failed: " + err);
    // Failed to get the token silently; show the login button

    // You could attempt to launch the login popup here, but in browsers this could be blocked by
    // a popup blocker, in which case the login attempt will fail with the reason FailedToOpenWindow.
    } else {
    console.log('Refreshed');
    // document.getElementById('loginToken').innerHTML = document.getElementById('loginToken').innerHTML + 'Refreshed ' + idToken;
    }
    });
    }

    setTimeout(tokenSetup, 900000);
    }
    $(function () {
    tokenSetup();
    });

    -This is the working version which refreshes token silently.

    CaptureAdal.PNG

  • Community Member Profile Picture
    on at

    How to work with login button in this case? Just need to call adalAuthContext.login() method for login button?

  • Suggested answer
    Shaminderpal Singh Profile Picture
    1,565 on at

    Yes, if the renewal fails then do authContext.login() to send a new request to authorization endpoint.

    -Shaminder

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans