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

SPFX app calling AadHttpClient returns idToken is null or empty

Open manlamcheng opened this issue 6 years ago • 13 comments

starting last week, we are getting an error similar to this bug https://github.com/SharePoint/sp-dev-docs/issues/4840

we have a SPFX app that calls API which is secured with AD authentication. The app has been working for months. There were no changes to the code. This errors just suddenly happens on both production and development sites on the same tenant.

The app calls the API in two places, one on the entry screen and one on the detail screen. It seems that it works only the first call, then on the second call it returns “ClientAuthError: The idToken is null or empty. Please review the trace to determine the root cause. Raw ID Token Value: null”.

Here is the code On the webpart file public onInit(): Promise { return super.onInit().then((_) => { let appID =

  this.context.aadHttpClientFactory
    .getClient(appID)
    .then((client: AadHttpClient): void => {
      this.apiClient = client;
    });
  sp.setup({
    spfxContext: this.context,
  });
});

}

Then the apiClient is passed down via props to the components and calls this way

this.props.apiClient .fetch(“api endpoint”, AadHttpClient.configurations.v1, { method: "GET", headers: { Accept: "application/json", "Content-Type": "application/json", }, }) .then((response) => response.json()) .then((data) => {} .catch((error) => { alert("Error loading data: " + error); });

The SPFX version used is 1.9.1

Does anyone know what happening here?

thank you !

manlamcheng avatar Oct 18 '20 12:10 manlamcheng

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

ghost avatar Oct 18 '20 12:10 ghost

I tried to upgrade the framework to 1.11 as instructed here https://docs.microsoft.com/en-us/sharepoint/dev/spfx/release-1.11.0 still getting the same error

manlamcheng avatar Oct 18 '20 15:10 manlamcheng

@manlamcheng said:

let appID =
  this.context.aadHttpClientFactory
    .getClient(appID)
    .then((client: AadHttpClient): void => {
      this.apiClient = client;
    });
  sp.setup({
    spfxContext: this.context,
  });
});

What's appID? Is that a real GUID or endpoint? If so, that's not supported. At some point in the previews, you could use a GUID, but that wasn't the documented way of doing it once it went GA.

While it worked for a while, that's not what was supported so if it worked until just recently, very well could be that support was dropped. See this for a reference: https://docs.microsoft.com/en-us/sharepoint/dev/spfx/use-aadhttpclient#connect-to-azure-ad-applications-using-the-aadhttpclient

andrewconnell avatar Oct 19 '20 13:10 andrewconnell

BTW, didn't notice this before, but in the future, please don't clear out the issue template. It includes important context information we need to help. In the future, new issues will be automatically rejected if they omit this information.

andrewconnell avatar Oct 19 '20 13:10 andrewconnell

Hello Andrew, thanks for the reply,

sorry, I did see the template that was used on other issues, but when I open one by clicking on the "..." on other issue and chose "Reference in new issue", I didn't see the template, just a blank text box. I will make sure to include the template next time

as for the appID, it's the Application ID of the app that's registered in Azure AD. It's used in the Azure function app for authentication

manlamcheng avatar Oct 19 '20 13:10 manlamcheng

@manlamcheng said:

as for the appID, it's the Application ID of the app that's registered in Azure AD. It's used in the Azure function app for authentication

As I said in my reply, that's not supported and won't always work. In your case, you should use the URL of your Azure Function App as the referenced doc I provided shows.

andrewconnell avatar Oct 19 '20 13:10 andrewconnell

yes, I just tested with the url instead of the appID as indicated on the link you provided, but still getting the same error

.getClient("https://{appname}.azurewebsites.net")

manlamcheng avatar Oct 19 '20 13:10 manlamcheng

The AadHttpClient object is created on the web part file and passed to main component

main component then passed to 2 other components (A, B) via props

if A open first, it will work, then from A switch to B, it gives error on B

if B open first, then it works.

is this approach supported? or should I create a separate AadHttpClient object on each component ?

manlamcheng avatar Oct 19 '20 14:10 manlamcheng

still struggling here

I tried to create a service class for aadHttpClient service, and imported to components. still facing the same problem "The idToken is null or empty."

also tried to turn off the AD authentications on the function app, no help

copy and paste the function url into browser works

I am running out of options, please please let me know if you have any hints/directions that I can follow

thank you very much

manlamcheng avatar Oct 20 '20 15:10 manlamcheng

@manlamcheng said:

still facing the same problem "The idToken is null or empty."

That sounds more like an issue with your custom AAD app than your code or the config. There's a setting within the AAD app where you can select if an ID token is returned.

andrewconnell avatar Oct 21 '20 13:10 andrewconnell

thanks, I think I may have a fix, it's related to the session being cleared when switching components, however, it's not an issue for past months, perhaps there was a recent change with SharePoint online that affected how session is handled

manlamcheng avatar Oct 22 '20 12:10 manlamcheng

There's active work going on to migrate the internals of how tokens are obtained, but the current state of that work isn't well known. They are moving from ADAL.js => MSAL.js v1 & also working to fix numerous known issues with the process. That may be an impact to what you're seeing, but maybe not... can't tell.

andrewconnell avatar Oct 22 '20 13:10 andrewconnell

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

ghost avatar Oct 29 '20 18:10 ghost