Skip to content

Enhance error handling for clipboard operations and improve error messages for browser opening failures #4992

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Commands/AzureAD/RegisterAzureADApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 7 additions & 3 deletions src/Commands/AzureAD/RegisterEntraIDAppForInteractiveLogin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 <yourtenanturl> -ClientId {azureApp.AppId}");
Expand Down
8 changes: 7 additions & 1 deletion src/Commands/Base/PnPConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
8 changes: 4 additions & 4 deletions src/Commands/Utilities/BrowserHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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())
Expand Down
Loading