protected internal override async Task CreateContextAsync()

in sources/Google.Solutions.IapDesktop.Extensions.Session/ToolWindows/App/ConnectAppProtocolWithClientCommand.cs [106:187]


        protected internal override async Task<AppProtocolContext> CreateContextAsync(
            IProjectModelInstanceNode instance,
            CancellationToken cancellationToken)
        {
            var context = (AppProtocolContext)await this.contextFactory
                .CreateContextAsync(
                    instance,
                    (uint)AppProtocolContextFlags.TryUseRdpNetworkCredentials,
                    cancellationToken)
                .ConfigureAwait(true);

            var client = this.contextFactory.Protocol
                .Client
                .ExpectNotNull("Client is null");

            if (!client.IsNetworkLevelAuthenticationSupported ||
                context.Parameters.NetworkLevelAuthentication == AppNetworkLevelAuthenticationState.Disabled)
            {
                //
                // Reset network credentials, we're not supposed to
                // use these.
                //
                context.NetworkCredential = null;

                if (client.IsUsernameRequired &&
                    (this.forceCredentialPrompt || string.IsNullOrEmpty(context.Parameters.PreferredUsername)))
                {
                    //
                    // Prompt for a username.
                    //
                    if (this.credentialDialog.PromptForUsername(
                        this.ownerWindow,
                        this.contextFactory.Protocol.Name,
                        $"Enter username for {instance.DisplayName}",
                        out var username) != DialogResult.OK)
                    {
                        //
                        // Cancelled.
                        //
                        throw new TaskCanceledException();
                    }

                    Debug.Assert(username != null);
                    context.Parameters.PreferredUsername = username;
                }
            }
            else
            {
                //
                // NLA enabled, make sure we actually have credentials.
                //
                if (this.forceCredentialPrompt || context.NetworkCredential == null)
                {
                    //
                    // Prompt for Windows credentials.
                    //
                    if (this.credentialDialog.PromptForWindowsCredentials(
                        this.ownerWindow,
                        new CredentialDialogParameters()
                        {
                            Caption = this.contextFactory.Protocol.Name,
                            Message = $"Enter credentials for {instance.DisplayName}",
                            Package = AuthenticationPackage.Any
                        },
                        out var _,
                        out var credential) != DialogResult.OK)
                    {
                        //
                        // Cancelled.
                        //
                        throw new TaskCanceledException();
                    }

                    Debug.Assert(credential != null);
                    context.NetworkCredential = credential;
                }

                Debug.Assert(context.NetworkCredential != null);
            }

            return context;
        }