From 815b63f7d6ff529880bf12441272850a35d2f293 Mon Sep 17 00:00:00 2001 From: Timothy Wamalwa Date: Wed, 10 Jul 2024 13:27:20 +0300 Subject: [PATCH] Resets to original graph environment only if the current environment is not the same as the original one --- .../Authentication/Cmdlets/InvokeMgGraphRequest.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Authentication/Authentication/Cmdlets/InvokeMgGraphRequest.cs b/src/Authentication/Authentication/Cmdlets/InvokeMgGraphRequest.cs index d211b0c2aa..9090fd957e 100644 --- a/src/Authentication/Authentication/Cmdlets/InvokeMgGraphRequest.cs +++ b/src/Authentication/Authentication/Cmdlets/InvokeMgGraphRequest.cs @@ -999,8 +999,11 @@ private string QualifyFilePath(string path) /// private void ResetGraphSessionEnvironment() { - _originalEnvironment = GraphSession.Instance.Environment; - GraphSession.Instance.Environment = _originalEnvironment; + var currentEnvironment = GraphSession.Instance.Environment; + if(currentEnvironment != null && !currentEnvironment.Equals(_originalEnvironment)) + { + GraphSession.Instance.Environment = _originalEnvironment; + } } #region CmdLet LifeCycle @@ -1039,6 +1042,8 @@ private async Task ProcessRecordAsync() if (ShouldCheckHttpStatus && !isSuccess) { var httpErrorRecord = await GenerateHttpErrorRecordAsync(httpResponseMessageFormatter, httpRequestMessage); + // A reset of the GraphSession Environment is required to avoid side effects + ResetGraphSessionEnvironment(); ThrowTerminatingError(httpErrorRecord); } await ProcessResponseAsync(httpResponseMessage);