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..81e1405e4 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://aka.ms/pnp/powershell"); } } - 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://aka.ms/pnp/powershell", ex); } } else if (OperatingSystem.IsMacOS())