0
votes

I'm working on an application using Framework 4.6.2 and angularjs, We are using adal.js and adal-angular.js. The problem we are experiencing is that when you are logout of the application and click on a hyperlink that point to the aplication (not the root of the app) after login on xxxx.onmicrosoft.com it redirect you to the root.

For example you click on a link or put the following url in the browser

http://localhost:4434/#/ticket/search

The application takes you to the login page:

enter image description here

After that it redirect me to the root

http://localhost:4434/#/

instead of http://localhost:4434/#/ticket/search

Here is the code I have in place:

      adalAuthenticationServiceProvider.init({
            tenant: 'xxxxx.onmicrosoft.com',
            clientId: 'xxxx-xxxx-xxxx-xxxxx',
            cacheLocation: 'localStorage',
            popUp: true
        }, $httpProvider);

.when("/tickets/search", { controller: "TicketsSearchController", templateUrl: "app/tickets/templates/search.html", allowNavBack: true, requireADLogin: true }) we are using AdalJS and ADAL-angular v1.0.14

Microsoft.IdentityModel.Clients.ActiveDirectory 3.13.9

This is how the Startup class looks like

 public class Startup
     {
        public void Configuration(IAppBuilder app)
         {

       app.UseWindowsAzureActiveDirectoryBearerAuthentication(
               new WindowsAzureActiveDirectoryBearerAuthenticationOptions
                {
                Tenant = "xxxxxx.onmicrosoft.com",
                TokenValidationParameters = new TokenValidationParameters
                {
                 ValidAudience = "xxxxxx.onmicrosoft.com/"
                }

            });
        app.UseAeActiveDirectoryViaAzureAuthentication(WebConfigurationManager.AppSettings["ida:ClientId"],
            WebConfigurationManager.AppSettings["ida:RedirectUri"]);

        app.MapSignalR();
    }
}

The issue is that no matter what url you are trying to go, it always redirects you to the url I specified on Azure which is the same I use in web.config

But the expected result is that if you navigate to a url like http://localhost:4434/#/ticket/search after you login you should be redirected to that Url not to the root http://localhost:4434

Thanks in advance for any help you can provide

Jose

1

1 Answers

2
votes

This root cause is that the ADAL check the state whether is protected by Azure AD before the location is changed. So the ADAL will save the current location as loginStartPage. To make sure the page redirect to the path previews visiting, we can change the source code of ADAL.

For example, we can change the stateChangeHandler function to log the path and then modify the loginHandler to change the login page as we wanted.

var stateChangeHandler = function (e, toState, toParams, fromState, fromParams) {
                    if (toState) {
                        var states = getStates(toState);
                        var state = null;
                        for (var i = 0; i < states.length; i++) {
                            state = states[i];
                            if (isADLoginRequired(state, _adal.config)) {
                                if (!_oauthData.isAuthenticated) {
                                    if (!_adal._renewActive && !_adal.loginInProgress()) {
                                        _adal.info('State change event for:' + $location.$$url);
                                        //pass the url here
                                        loginHandler(toState.url);
                                    }
                                }
                            }
 ...

var loginHandler = function (path) {
    _adal.info('Login event for:' + $location.$$url);
    if (_adal.config && _adal.config.localLoginUrl) {
        $location.path(_adal.config.localLoginUrl);
    }
    else {
        // directly start login flow
        _adal.info('Start login at:' + $location.$$absUrl);
        $rootScope.$broadcast('adal:loginRedirect');
        //change the loginStatePage here
        _adal.login($location.$$absUrl+'#!'+path);
    }
};

This is a workaround for this issue, to support redirecting the specific route after ADAL authentication using the raw ADAL for JavaScript, I suggest that you reopen a new issue from here.