sp-dev-docs icon indicating copy to clipboard operation
sp-dev-docs copied to clipboard

SPFx page is loaded twice during adal login

Open vasanthtt opened this issue 7 years ago • 10 comments

Category

  • [x] Question
  • [ ] Typo
  • [ ] Bug
  • [ ] Additional article idea

We have built SPFx component using react that uses adal.js for calling external API. I’ve noticed a bad experience in my spfx page, the page reloaded twice during login. When I debugged, the first reload happens when login happened in adal. This returns the id_refresh token in URL fragment in the reply URL. The next reload is called when this token is handled by handleWindowCallback method. I can suppress the second reload by configuring adal with navigateToLoginRequestUrl: false. But I am afraid to use this because the original URL contains the query string which is lost due to this setting.

As due to some reason we have to build spfx component using adal.js, not the AADHttpClient. So need help to fix this multiple refreshes which actually gives a bad user experience.

ADAL Config

import * as AuthenticationContext from "adal-angular";
const adalConfig: AuthenticationContext.Options = {
  clientId: "rfdsfsd-dsfsdf-sdf-sdfsd",
  tenant: "microsoft.onmicrosoft.com",
  extraQueryParameter: "scope=openid+profile", 
  endpoints: {
    "https://graph.microsoft.com": "https://graph.microsoft.com",
    WebApiUri: "https://webapi.azurewebsites.net"
  },
  cacheLocation: "localStorage",
  navigateToLoginRequestUrl: false,
  redirectUri: window.location.href,
};

Handle id_refresh token

this.authCtx = new AuthenticationContext(this.config);
    var isCallback = this.authCtx.isCallback(window.location.hash);
    if (isCallback && !this.authCtx.getLoginError()) {
      this.authCtx.handleWindowCallback(window.location.hash);
    } else {
      var user = this.authCtx.getCachedUser();
      if (!user) {
        this.authCtx.login();
      }
      else {
        if (user.userName.toLowerCase() !== this.userName.toLowerCase()) {
          this.authCtx.clearCache();
        }
        this.userName = user.userName;
      }
    }

@andrewconnell @VesaJuvonen Request you both also to check and let me know. Thanks.

vasanthtt avatar Aug 16 '19 13:08 vasanthtt

Thank you for reporting this issue. We will be triaging your incoming issue as soon as possible.

msft-github-bot avatar Aug 16 '19 13:08 msft-github-bot

Thanks for the question. This is a limitation of ADALjs.

ADALjs and OAuth Implicit Flow is based on redirects between Azure AD and your client side web part. The app redirects to Azure AD, and then redirects back to your client side web part with the token in the URL.

This is outlined in the SPFx ADALjs docuementation - https://docs.microsoft.com/en-us/sharepoint/dev/spfx/web-parts/guidance/connect-to-api-secured-with-aad#processing-authorization-callbacks

There has been other methods online using popups instead of redirects - still not a great user experience.

Question - Is the reason you are using ADALjs directly instead of AADHttpClient because you are using version before SPFx 1.4.1?


One a side note, tagging people in your post doesn't improve the chances of being answered. We try to address each question as it comes in.

bcameron1231 avatar Aug 16 '19 13:08 bcameron1231

Thanks for the question. This is a limitation of ADALjs. .... Question - Is the reason you are using ADALjs directly instead of AADHttpClient because you are using version before SPFx 1.4.1?

I am having a similar issue. and I am on the latest version of SPFx 1.8.x

In my scenario, I started using ADAL, as AADHttpClient was not enabled/allowed by our IT until recently, and I have a bunch of SPFx components that use the ADAL that will need to be updated. So, I have to continue using ADAL for a while before changing.

Further, I need to submit my components to IT for publishing to catalog - I guess it is a common thing, but must make it Isolated also, and the permissions need to be approved by IT, even before I can complete testing.

So, I am following a method to use both, ADAL during development cycle until user testing is done and change to AADHttpClient for the final version published thru IT.

Question: When considering using AADHttpClient, Is there guidance on how to manage the development cycle for my scenario?

AniruddhaBapat avatar Aug 19 '19 00:08 AniruddhaBapat

I'm curious why AADHttpClient is not allowed, but ADALjs is? AADHttpClient uses ADALjs... so it's just a lot more work for you to develop solutions using the same technology... because without AADHttpClient you have to handle all the bootstrapping and the AD Registration yourself.

I would suggest reaching out to your IT Team because they are doing nothing except making it harder for you to develop your solutions. You are using ADALjs either way :)

bcameron1231 avatar Aug 19 '19 01:08 bcameron1231

I would suggest reaching out to your IT Team because they are doing nothing except making it harder for you to develop your solutions. You are using ADALjs either way :)

AADHttpClient is allowed now, however web parts need to be isolated and permissions approved by IT (a lengthy process of review and approval). This is not so much of problem if I am required to do it only once.

The problem I see moving to AADHttpClient in my scenario is making the development cycle potentially requiring multiple IT submission cycles. To avoid that, I am continuing to use ADAL directly, that allows completing testing and changing to AADHttpClient before submitting the final version to IT. - I agree it is a lot more work, is there a better way?

AniruddhaBapat avatar Aug 19 '19 01:08 AniruddhaBapat

I personally develop my solutions on a dev environment where I have Admin credentials to add/approve requests for API Management.

bcameron1231 avatar Aug 19 '19 13:08 bcameron1231

I personally develop my solutions on a dev environment where I have Admin credentials to add/approve requests for API Management.

Having a completely isolated environment for SPO and Azure is an option but I guess it will not be very cost-effective in addition to being administrative and configuration management overhead compared to our current setup, where we use Dev site collections and Prod site collections in the same tenant and respective Azure environment. Question: Is having a completely isolated SPO and Azure environment, where Dev team can manage API permissions during the development phase - is a prerequisite for moving to AADHttpClient?

AniruddhaBapat avatar Aug 20 '19 03:08 AniruddhaBapat

@AniruddhaBapat said:

Question: Is having a completely isolated SPO and Azure environment, where Dev team can manage API permissions during the development phase - is a prerequisite for moving to AADHttpClient?

Absolutely not... it's just a development preference.

It's a separate issue, but if you wanted to go that route, the cost is zero to minimal. An Office 365 developer tenant is free... and you can access the Azure AD portal as well... for free. If you created your secured service in Azure (with an Azure Function or Web App for instance), the consumption cost would be minimal for development and testing.

The SPFx has provided a solution for calling secured endpoints with the AADHttpClient API & usage of ADAL.js is no longer the recommended option due to the issue you ran into. I think the question has been answered and this is more of a challenge with your IT group & processes.

andrewconnell avatar Aug 20 '19 13:08 andrewconnell

@andrewconnell @bcameron1231 - thanks for your guidance.

and while I am considering an isolated environment that Dev can manage without requiring IT involvement, I also feel the IT processes or related challenges are not unique only to my organization any large enterprise will have similar.

AniruddhaBapat avatar Aug 21 '19 08:08 AniruddhaBapat

Issues that have been closed & had no follow-up activity for at least 7 days are automatically locked. Please refer to our wiki for more details, including how to remediate this action if you feel this was done prematurely or in error: Issue List: Our approach to locked issues

msft-github-bot avatar Jan 23 '20 18:01 msft-github-bot