From 77e83c4139e220928e937997ef37f4de819a8232 Mon Sep 17 00:00:00 2001 From: koskila Date: Mon, 28 Oct 2024 09:50:51 +0200 Subject: [PATCH] Don't throw in ClientContext.IsAppOnly() if AccessToken extraction is successful --- .../Extensions/ClientContextExtensions.cs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/lib/PnP.Framework/Extensions/ClientContextExtensions.cs b/src/lib/PnP.Framework/Extensions/ClientContextExtensions.cs index 0734c529a..bb6e5df5d 100644 --- a/src/lib/PnP.Framework/Extensions/ClientContextExtensions.cs +++ b/src/lib/PnP.Framework/Extensions/ClientContextExtensions.cs @@ -576,7 +576,6 @@ public static ClientContext GetSiteCollectionContext(this ClientRuntimeContext c /// True if app-only, false otherwise public static bool IsAppOnly(this ClientRuntimeContext clientContext) { - // Set initial result to false var result = false; @@ -690,7 +689,18 @@ public static string GetAccessToken(this ClientRuntimeContext clientContext) }; // Issue a dummy request to get it from the Authorization header clientContext.ExecutingWebRequest += handler; - clientContext.ExecuteQuery(); + try + { + clientContext.ExecuteQuery(); + } + catch (Exception ex) + { + // This can fail for whatever reason, but if we already have the AccessToken, it doesn't matter + if (String.IsNullOrEmpty(accessToken)) + { + throw new AggregateException("Fetching the AccessToken to inspect whether the ClientContext is AppOnly or not failed. There might be more information in the InnerExceptions.", ex); + } + } clientContext.ExecutingWebRequest -= handler; } }