Adal.js logging out without a redirect

In our current SPA implementation we authenticate against AzureAD using adal.js and upon successful authentication hit our web api to get the authorization data. There are a couple of edge case scenarios where the get authorization data call could fail. In this case we would like to clear out the state/cache created by adal.js. I have tried a few things but I have not not been able to create a clean slate. Here is some code that I have tried.

localStorage.clear();
 var authContext = AuthenticationContext.prototype._singletonInstance;
 authContext.clearCache();
 authContext._user = null; 

I don't want to use the built in logout function. Calling logout redirects the user to the Azure signout page. The UX is pretty wierd so trying to avoid it.

Answers:

Answer

If you want to clear all the cache entries created by adal, clearCache() is the method that should be used, and if you want to clear the cache only for a specific resource entry, then use clearCacheForResource.

But one more thing to note is, these two methods only clear the cache/storage though, it won't clear any session/cookie hold on azure ad, if you want to clear that, then the built in logout should be the one to use.

You could probably try to implement the silent logout(probably using iframe, this will prevent the ux from displaying), and then call clearCache to clear the localstorage/sessionstorage

Answer

You can set postLogoutRedirectUri to your aplication setup:

  adalProvider.init(
        {
            instance: 'https://login.microsoftonline.com/',
            tenant: 'www.contoso.com',
            clientId: '0101001010101',
            extraQueryParameter: 'nux=1',
            cacheLocation: 'localStorage', // enable this for IE, as sessionStorage does not work for localhost.
            endpoints: endpoints,
            postLogoutRedirectUri: 'https://www.yourapp.com'
        },
        $httpProvider
        );

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.