Skip to content

Fix #4983: open browser in linux #4984

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 1 commit into from
Jun 26, 2025
Merged
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
114 changes: 94 additions & 20 deletions src/Commands/Utilities/BrowserHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using Microsoft.Identity.Client;
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
Expand Down Expand Up @@ -165,42 +167,70 @@ internal enum UrlMatchType
}
}

// Using code from MSAL
// https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/blob/main/src/client/Microsoft.Identity.Client/Platforms/netstandard/NetCorePlatformProxy.cs

internal static void OpenBrowserForInteractiveLogin(string url, int port, CancellationTokenSource cancellationTokenSource)
{
// Fixes encoding of scopes and redirect_uri issue on MacOS. It has no negative effects on Windows.
url = WebUtility.UrlDecode(url);

try
if (OperatingSystem.IsWindows())
{

ProcessStartInfo psi = new ProcessStartInfo
try
{
FileName = url,
UseShellExecute = true
};
Process.Start(psi);

}
catch
{
// hack because of this: https://github.com/dotnet/corefx/issues/10361
if (OperatingSystem.IsWindows())
var psi = new ProcessStartInfo
{
FileName = url,
UseShellExecute = true
};
Process.Start(psi);
}
catch
{
// hack because of this: https://github.com/dotnet/corefx/issues/10361
url = url.Replace("&", "^&");
Process.Start(new ProcessStartInfo("cmd", $"/c start {url}") { CreateNoWindow = true });
}
else if (OperatingSystem.IsLinux())
}
else if (OperatingSystem.IsLinux())
{
string sudoUser = Environment.GetEnvironmentVariable("SUDO_USER");
if (!string.IsNullOrWhiteSpace(sudoUser))
{
Process.Start("xdg-open", url);
throw new MsalClientException(MsalError.LinuxXdgOpen);
}
else if (OperatingSystem.IsMacOS())
try
{
Process.Start("open", url);
bool opened = false;
foreach (string openTool in GetOpenToolsLinux())
{
if (TryGetExecutablePath(openTool, out string openToolPath))
{
OpenLinuxBrowser(openToolPath, url);
opened = true;
break;
}
}

if (!opened)
{
throw new MsalClientException(MsalError.LinuxXdgOpen);
}
}
else
catch
{
throw new PlatformNotSupportedException(RuntimeInformation.OSDescription);
throw new MsalClientException(MsalError.LinuxXdgOpen);
}
}
else if (OperatingSystem.IsMacOS())
{
Process.Start("/usr/bin/open", url);
}
else
{
throw new PlatformNotSupportedException(RuntimeInformation.OSDescription);
}
}

internal static int FindFreeLocalhostRedirectUri()
Expand All @@ -216,5 +246,49 @@ internal static int FindFreeLocalhostRedirectUri()
listener?.Stop();
}
}

internal static void OpenLinuxBrowser(string openToolPath, string url)
{
ProcessStartInfo psi = new ProcessStartInfo(openToolPath, url)
{
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false
};

Process.Start(psi);

}

internal static string[] GetOpenToolsLinux()
{
return ["xdg-open", "gnome-open", "kfmclient", "microsoft-edge", "wslview"];
}

/// <summary>
/// Searches through PATH variable to find the path to the specified executable.
/// </summary>
/// <param name="executable">Executable to find the path for.</param>
/// <param name="path">Location of the specified executable.</param>
/// <returns></returns>
internal static bool TryGetExecutablePath(string executable, out string path)
{
string pathEnvVar = Environment.GetEnvironmentVariable("PATH");
if (pathEnvVar != null)
{
var paths = pathEnvVar.Split(':');
foreach (var basePath in paths)
{
path = Path.Combine(basePath, executable);
if (File.Exists(path))
{
return true;
}
}
}

path = null;
return false;
}
}
}
Loading