From e396f40c3781608786ac727765847861e3df19bc Mon Sep 17 00:00:00 2001 From: Gautam Sheth Date: Sat, 28 Jun 2025 17:31:01 +0300 Subject: [PATCH 1/2] Enhance error handling for clipboard operations and improve error messages for browser opening failures --- src/Commands/AzureAD/RegisterAzureADApp.cs | 8 +++++++- .../AzureAD/RegisterEntraIDAppForInteractiveLogin.cs | 10 +++++++--- src/Commands/Base/PnPConnection.cs | 8 +++++++- src/Commands/Utilities/BrowserHelper.cs | 8 ++++---- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/Commands/AzureAD/RegisterAzureADApp.cs b/src/Commands/AzureAD/RegisterAzureADApp.cs index a1c357e39..e73a8173e 100644 --- a/src/Commands/AzureAD/RegisterAzureADApp.cs +++ b/src/Commands/AzureAD/RegisterAzureADApp.cs @@ -659,7 +659,13 @@ private void StartConsentFlow(string loginEndPoint, AzureADApp azureApp, string { using (var authManager = AuthenticationManager.CreateWithDeviceLogin(azureApp.AppId, Tenant, (deviceCodeResult) => { - ClipboardService.SetText(deviceCodeResult.UserCode); + try + { + ClipboardService.SetText(deviceCodeResult.UserCode); + } + catch + { + } Host.UI.WriteWarningLine($"\n\nPlease login.\n\nWe opened a browser and navigated to {deviceCodeResult.VerificationUrl}\n\nEnter code: {deviceCodeResult.UserCode} (we copied this code to your clipboard)\n\nNOTICE: close the browser tab after you authenticated successfully to continue the process."); BrowserHelper.OpenBrowserForInteractiveLogin(deviceCodeResult.VerificationUrl, BrowserHelper.FindFreeLocalhostRedirectUri(), cancellationTokenSource); return Task.FromResult(0); diff --git a/src/Commands/AzureAD/RegisterEntraIDAppForInteractiveLogin.cs b/src/Commands/AzureAD/RegisterEntraIDAppForInteractiveLogin.cs index 4991e00c0..af6cc47be 100644 --- a/src/Commands/AzureAD/RegisterEntraIDAppForInteractiveLogin.cs +++ b/src/Commands/AzureAD/RegisterEntraIDAppForInteractiveLogin.cs @@ -496,12 +496,17 @@ private void StartConsentFlow(string loginEndPoint, AzureADApp azureApp, string // { if (!Stopping) { - if (ParameterSpecified(nameof(DeviceLogin))) { using (var authManager = AuthenticationManager.CreateWithDeviceLogin(azureApp.AppId, Tenant, (deviceCodeResult) => { - ClipboardService.SetText(deviceCodeResult.UserCode); + try + { + ClipboardService.SetText(deviceCodeResult.UserCode); + } + catch + { + } Host.UI.WriteWarningLine($"\n\nPlease login.\n\nWe opened a browser and navigated to {deviceCodeResult.VerificationUrl}\n\nEnter code: {deviceCodeResult.UserCode} (we copied this code to your clipboard)\n\nNOTICE: close the browser tab after you authenticated successfully to continue the process."); BrowserHelper.OpenBrowserForInteractiveLogin(deviceCodeResult.VerificationUrl, BrowserHelper.FindFreeLocalhostRedirectUri(), cancellationTokenSource); return Task.FromResult(0); @@ -523,7 +528,6 @@ private void StartConsentFlow(string loginEndPoint, AzureADApp azureApp, string authManager.GetAccessToken(resource, Microsoft.Identity.Client.Prompt.Consent); } } - // Write results WriteObject($"App created. You can now connect to your tenant using Connect-PnPOnline -Url -ClientId {azureApp.AppId}"); diff --git a/src/Commands/Base/PnPConnection.cs b/src/Commands/Base/PnPConnection.cs index 66889316e..d2ecbaf3c 100644 --- a/src/Commands/Base/PnPConnection.cs +++ b/src/Commands/Base/PnPConnection.cs @@ -280,7 +280,13 @@ internal static PnPConnection CreateWithDeviceLogin(string clientId, string url, { authManager = Framework.AuthenticationManager.CreateWithDeviceLogin(clientId, tenantId, (deviceCodeResult) => { - ClipboardService.SetText(deviceCodeResult.UserCode); + try + { + ClipboardService.SetText(deviceCodeResult.UserCode); + } + catch + { + } messageWriter.LogWarning($"\n\nCode {deviceCodeResult.UserCode} has been copied to your clipboard and a new tab in the browser has been opened. Please paste this code in there and proceed.\n\n"); BrowserHelper.OpenBrowserForInteractiveLogin(deviceCodeResult.VerificationUrl, BrowserHelper.FindFreeLocalhostRedirectUri(), cancellationTokenSource); diff --git a/src/Commands/Utilities/BrowserHelper.cs b/src/Commands/Utilities/BrowserHelper.cs index 0f9b96b9b..35f402281 100644 --- a/src/Commands/Utilities/BrowserHelper.cs +++ b/src/Commands/Utilities/BrowserHelper.cs @@ -198,7 +198,7 @@ internal static void OpenBrowserForInteractiveLogin(string url, int port, Cancel string sudoUser = Environment.GetEnvironmentVariable("SUDO_USER"); if (!string.IsNullOrWhiteSpace(sudoUser)) { - throw new MsalClientException(MsalError.LinuxXdgOpen); + throw new MsalClientException(MsalError.LinuxXdgOpen, "Unable to open a web page using xdg-open, gnome-open, kfmclient or wslview tools in sudo mode. Please run the process as non-sudo user."); } try { @@ -215,12 +215,12 @@ internal static void OpenBrowserForInteractiveLogin(string url, int port, Cancel if (!opened) { - throw new MsalClientException(MsalError.LinuxXdgOpen); + throw new MsalClientException(MsalError.LinuxXdgOpen, "Unable to open a web page using xdg-open, gnome-open, kfmclient or wslview tools. See inner exception for details. Possible causes for this error are: tools are not installed or they cannot open a URL. Make sure you can open a web page by invoking from a terminal: xdg-open https://www.bing.com"); } } - catch + catch (Exception ex) { - throw new MsalClientException(MsalError.LinuxXdgOpen); + throw new MsalClientException(MsalError.LinuxXdgOpen, "Unable to open a web page using xdg-open, gnome-open, kfmclient or wslview tools. See inner exception for details. Possible causes for this error are: tools are not installed or they cannot open a URL. Make sure you can open a web page by invoking from a terminal: xdg-open https://www.bing.com", ex); } } else if (OperatingSystem.IsMacOS()) From fdbc5349759651f043f620875625003ed41308f6 Mon Sep 17 00:00:00 2001 From: Gautam Sheth Date: Sat, 28 Jun 2025 17:37:40 +0300 Subject: [PATCH 2/2] Update error messages for Linux web page opening failures to provide a more relevant URL --- src/Commands/Utilities/BrowserHelper.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Commands/Utilities/BrowserHelper.cs b/src/Commands/Utilities/BrowserHelper.cs index 35f402281..81e1405e4 100644 --- a/src/Commands/Utilities/BrowserHelper.cs +++ b/src/Commands/Utilities/BrowserHelper.cs @@ -215,12 +215,12 @@ internal static void OpenBrowserForInteractiveLogin(string url, int port, Cancel if (!opened) { - throw new MsalClientException(MsalError.LinuxXdgOpen, "Unable to open a web page using xdg-open, gnome-open, kfmclient or wslview tools. See inner exception for details. Possible causes for this error are: tools are not installed or they cannot open a URL. Make sure you can open a web page by invoking from a terminal: xdg-open https://www.bing.com"); + throw new MsalClientException(MsalError.LinuxXdgOpen, "Unable to open a web page using xdg-open, gnome-open, kfmclient or wslview tools. See inner exception for details. Possible causes for this error are: tools are not installed or they cannot open a URL. Make sure you can open a web page by invoking from a terminal: xdg-open https://aka.ms/pnp/powershell"); } } catch (Exception ex) { - throw new MsalClientException(MsalError.LinuxXdgOpen, "Unable to open a web page using xdg-open, gnome-open, kfmclient or wslview tools. See inner exception for details. Possible causes for this error are: tools are not installed or they cannot open a URL. Make sure you can open a web page by invoking from a terminal: xdg-open https://www.bing.com", ex); + throw new MsalClientException(MsalError.LinuxXdgOpen, "Unable to open a web page using xdg-open, gnome-open, kfmclient or wslview tools. See inner exception for details. Possible causes for this error are: tools are not installed or they cannot open a URL. Make sure you can open a web page by invoking from a terminal: xdg-open https://aka.ms/pnp/powershell", ex); } } else if (OperatingSystem.IsMacOS())