public override Task StartDefaultOsBrowserAsync(string url)
        {
            if (DesktopOsHelper.IsWindows())
            {
                try
                {
                    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 msedge {url}")
                    {
                        CreateNoWindow = true
                    });
                }
            }
            else if (DesktopOsHelper.IsLinux())
            {
                try
                {
                    ProcessStartInfo psi = null;
                    foreach (string openTool in new[] { "xdg-open", "gnome-open", "kfmclient" })
                    {
                        if (TryGetExecutablePath(openTool, out string openToolPath))
                        {
                            psi = new ProcessStartInfo(openToolPath, url)
                            {
                                RedirectStandardOutput = true,
                                RedirectStandardError  = true
                            };

                            Process.Start(psi);

                            break;
                        }
                    }

                    if (psi == null)
                    {
                        throw new Exception("Failed to locate a utility to launch the default web browser.");
                    }
                }
                catch (Exception ex)
                {
                    throw new MsalClientException(
                              MsalError.LinuxXdgOpen,
                              MsalErrorMessage.LinuxOpenToolFailed,
                              ex);
                }
            }
            else if (DesktopOsHelper.IsMac())
            {
                Process.Start("/usr/bin/open", url);
            }
            else
            {
                throw new PlatformNotSupportedException(RuntimeInformation.OSDescription);
            }
            return(Task.FromResult(0));
        }
Пример #2
0
        public override Task StartDefaultOsBrowserAsync(string url, bool isBrokerConfigured)
        {
            if (DesktopOsHelper.IsWindows())
            {
                try
                {
                    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 msedge {url}")
                    {
                        CreateNoWindow = true
                    });
                }
            }
            else if (DesktopOsHelper.IsLinux())
            {
                string sudoUser = Environment.GetEnvironmentVariable("SUDO_USER");
                if (!string.IsNullOrWhiteSpace(sudoUser))
                {
                    throw new MsalClientException(
                              MsalError.LinuxXdgOpen,
                              MsalErrorMessage.LinuxOpenAsSudoNotSupported);
                }

                try
                {
                    bool opened = false;

                    foreach (string openTool in GetOpenToolsLinux(isBrokerConfigured))
                    {
                        if (TryGetExecutablePath(openTool, out string openToolPath))
                        {
                            OpenLinuxBrowser(openToolPath, url);
                            opened = true;
                            break;
                        }
                    }

                    if (!opened)

                    {
                        throw new MsalClientException(
                                  MsalError.LinuxXdgOpen,
                                  MsalErrorMessage.LinuxOpenToolFailed);
                    }
                }
                catch (Exception ex)
                {
                    throw new MsalClientException(
                              MsalError.LinuxXdgOpen,
                              MsalErrorMessage.LinuxOpenToolFailed,
                              ex);
                }
            }
            else if (DesktopOsHelper.IsMac())
            {
                Process.Start("/usr/bin/open", url);
            }
            else
            {
                throw new PlatformNotSupportedException(RuntimeInformation.OSDescription);
            }
            return(Task.FromResult(0));
        }