protected override void ConnectCore()

in sources/Google.Solutions.IapDesktop.Extensions.Session/ToolWindows/Session/RdpView.cs [146:317]


        protected override void ConnectCore()
        {
            var viewModel = this.viewModel.Value;

            using (ApplicationTraceSource.Log.TraceMethod().WithParameters(
                viewModel.Server,
                viewModel.Port,
                viewModel.Parameters!.ConnectionTimeout))
            {
                this.Client!.MainWindow = (Form)this.MainWindow;
                this.Client.ServerAuthenticationWarningDisplayed += (_, args)
                    => this.AuthenticationWarningDisplayed?.Invoke(this, args);

                //
                // Basic connection settings.
                //
                this.Client.Server = viewModel.Server;
                this.Client.Domain = viewModel.Credential!.Domain;
                this.Client.Username = viewModel.Credential.User;
                this.Client.ServerPort = viewModel.Port!.Value;
                this.Client.ConnectionTimeout = viewModel.Parameters.ConnectionTimeout;
                this.Client.EnableAdminMode = viewModel.Parameters.SessionType == RdpSessionType.Admin;

                //
                // Connection security settings.
                //
                switch (viewModel.Parameters.AuthenticationLevel)
                {
                    case RdpAuthenticationLevel.NoServerAuthentication:
                        this.Client.ServerAuthenticationLevel = 0;
                        break;

                    case RdpAuthenticationLevel.RequireServerAuthentication:
                        this.Client.ServerAuthenticationLevel = 1;
                        break;

                    case RdpAuthenticationLevel.AttemptServerAuthentication:
                        this.Client.ServerAuthenticationLevel = 2;
                        break;
                }

                switch (viewModel.Parameters.UserAuthenticationBehavior)
                {
                    case RdpAutomaticLogon.Enabled:
                        //
                        // Use stored credentials, but allow prompting in case
                        // they're incomplete or wrong.
                        //
                        this.Client.EnableCredentialPrompt = true;
                        this.Client.Password = 
                            viewModel.Credential.Password?.ToClearText() ?? string.Empty;
                        break;

                    case RdpAutomaticLogon.Disabled:
                        //
                        // Allow (and expect) a prompt.
                        //
                        // We "shouldn't" have a stored password -- but we might 
                        // have one anyway:
                        //
                        // - Automatic logons might be auto-disabled by group policy,
                        //   but the user might have stored a credential before that 
                        //   group policy took effect (or before IAP Desktop started 
                        //   considering that group policy).
                        // - Automatic logons might be auto-disabled by group policy 
                        //   (causing prompts to be suppressed), but the user might 
                        //   have stored credentialsmanually.
                        //
                        // So if there is a password, use it.
                        //
                        this.Client.EnableCredentialPrompt = true;
                        this.Client.Password = 
                            viewModel.Credential.Password?.ToClearText() ?? string.Empty;
                        break;

                    case RdpAutomaticLogon.LegacyAbortOnFailure:
                        this.Client.EnableCredentialPrompt = false;
                        this.Client.Password = 
                            viewModel.Credential.Password?.ToClearText() ?? string.Empty;
                        break;
                }

                this.Client.EnableNetworkLevelAuthentication =
                    viewModel.Parameters.NetworkLevelAuthentication != RdpNetworkLevelAuthentication.Disabled;
                this.Client.EnableRestrictedAdminMode =
                    viewModel.Parameters.RestrictedAdminMode == RdpRestrictedAdminMode.Enabled;

                //
                // Connection bar settings.
                //
                this.Client.EnableConnectionBar =
                    viewModel.Parameters.ConnectionBar != RdpConnectionBarState.Off;
                this.Client.EnableConnectionBarMinimizeButton = true;
                this.Client.EnableConnectionBarPin =
                    viewModel.Parameters.ConnectionBar == RdpConnectionBarState.Pinned;
                this.Client.ConnectionBarText = this.Instance.Name;

                //
                // Local resources settings.
                //
                this.Client.EnableClipboardRedirection =
                    viewModel.Parameters.RedirectClipboard == RdpRedirectClipboard.Enabled;
                this.Client.EnablePrinterRedirection =
                    viewModel.Parameters.RedirectPrinter == RdpRedirectPrinter.Enabled;
                this.Client.EnableSmartCardRedirection =
                    viewModel.Parameters.RedirectSmartCard == RdpRedirectSmartCard.Enabled;
                this.Client.EnablePortRedirection =
                    viewModel.Parameters.RedirectPort == RdpRedirectPort.Enabled;
                this.Client.EnableDriveRedirection =
                    viewModel.Parameters.RedirectDrive == RdpRedirectDrive.Enabled;
                this.Client.EnableDeviceRedirection =
                    viewModel.Parameters.RedirectDevice == RdpRedirectDevice.Enabled;
                this.Client.EnableAudioCaptureRedirection = 
                    viewModel.Parameters.AudioInput == RdpAudioInput.Enabled;

                switch (viewModel.Parameters.AudioPlayback)
                {
                    case RdpAudioPlayback.PlayLocally:
                        this.Client.AudioRedirectionMode = 0;
                        break;
                    case RdpAudioPlayback.PlayOnServer:
                        this.Client.AudioRedirectionMode = 1;
                        break;
                    case RdpAudioPlayback.DoNotPlay:
                        this.Client.AudioRedirectionMode = 2;
                        break;
                }

                //
                // Display settings.
                //
                this.Client.EnableDpiScaling =
                    viewModel.Parameters.DpiScaling == RdpDpiScaling.Enabled;
                this.Client.EnableAutoResize =
                    viewModel.Parameters.DesktopSize == RdpDesktopSize.AutoAdjust;

                switch (viewModel.Parameters.ColorDepth)
                {
                    case RdpColorDepth.HighColor:
                        this.Client.ColorDepth = 16;
                        break;
                    case RdpColorDepth.TrueColor:
                        this.Client.ColorDepth = 24;
                        break;
                    case RdpColorDepth.DeepColor:
                        this.Client.ColorDepth = 32;
                        break;
                }

                //
                // Keyboard settings.
                //
                this.Client.KeyboardHookMode =
                    (int)viewModel.Parameters.HookWindowsKeys;

                //
                // Set hotkey to trigger OnFocusReleasedEvent. This should be
                // the same as the main window uses to move the focus to the
                // control.
                //
                this.Client.FocusHotKey = ToggleFocusHotKey;
                this.Client.FullScreenHotKey = ToggleFullScreenHotKey;

                this.Client.EnableWebAuthnRedirection =
                    viewModel.Parameters.RedirectWebAuthn == RdpRedirectWebAuthn.Enabled;

                //
                // Start establishing a connection and react to events.
                //
                this.Client.Connect();
            }
        }