Auth0 Home Blog Docs

Can't get Azure AD working with SPA application

azure-ad
application-failure
not-working

#1

Hi
Guys, Auth0 is the most complex thing I have ever used. So many things needs to be discovered… Anyway, am trying to setup an Azure AD multi-tenant app and I can’t get it working. I am sure, I am missing something obvious. I have got everything working when using “Try” button from the Auth0 portal.

                var webAuth = new Auth0.WebAuth({
                  domain:  AUTH_CONFIG.domain,
                  clientID: AUTH_CONFIG.clientID
                });

                webAuth.authorize({
                    connection: 'digital-dealers-waad',
                    responseType: 'code',
                    redirectUri :AUTH_CONFIG.callbackURL,
                    prompt:"consent",
                });

Where call back URL - http://localhost:4200/callback - my Angular2 App
I am getting a code:

http://localhost:4200/callback?code=ReJnUpqetKd3HR9S

Btu what to do after with that code, I have no idea. I was trying to call Azure AD with that code, but it doesn’t work

            // Get the user's profile from Azure AD.
            var credential = new ClientCredential("AppKey",
                "AppSecret");
            var authContext = new AuthenticationContext("https://login.windows.net/common/");

            try
            {
                var result = await authContext.AcquireTokenByAuthorizationCodeAsync("ReJnUpqetKd3HR9S", new Uri("https://digital-dealers.au.auth0.com/login/callback"), credential);
            }catch(Exception ex)
            {
                string s = ex.Message;
            }

            return Ok();

But it returns BadRequest saying that Code is invalid.
Please HELP!!!


#2

The responseType parameter determines whether Auth0 redirects to your callback/application with an authorization_code, or an id_token. Because your frontend is a Single Page Application, we recommend using the responseType: 'token', which will redirect to your application with the id_token in the URL hash. The parseHash method will parse the hash fragment and extract the authentication result.


#3

Thank you so much for reply prashant.
Now I get something like this:
http://localhost:4200/callback#access_token=YyJ1pv_dAAFHdcPL&expires_in=86400&token_type=Bearer&state=Ts~w2OtXPHG-txRx9bXlt3k_Q5NpsIvF

What I should do with this token to get user id? Call Azure AD? Auth0 api?

Thanks
Nick


#4

Thank you so much for reply prashant.
Now I get something like this:
http://localhost:4200/callback#access_token=YyJ1pv_dAAFHdcPL&expires_in=86400&token_type=Bearer&state=Ts~w2OtXPHG-txRx9bXlt3k_Q5NpsIvF

What I should do with this token to get user id? Call Azure AD? Auth0 api?

Thanks
Nick


#5