public override async Task AuthenticateAsync()

in src/PowerShell/Authenticators/InteractiveUserAuthenticator.cs [35:93]


        public override async Task<AuthenticationResult> AuthenticateAsync(AuthenticationParameters parameters, CancellationToken cancellationToken = default)
        {
            AuthenticationResult authResult;
            IClientApplicationBase app;
            InteractiveParameters interactiveParameters = parameters as InteractiveParameters;
            TcpListener listener = null;
            string redirectUri = null;
            int port = 8399;

            while (++port < 9000)
            {
                try
                {
                    listener = new TcpListener(IPAddress.Loopback, port);
                    listener.Start();
                    redirectUri = $"http://localhost:{port}/";
                    listener.Stop();
                    break;
                }
                catch (Exception ex)
                {
                    WriteWarning($"Port {port} is taken with exception '{ex.Message}'; trying to connect to the next port.");
                    listener?.Stop();
                }
            }

            app = GetClient(parameters.Account, parameters.Environment, redirectUri);

            if (app is IConfidentialClientApplication)
            {
                ICustomWebUi customWebUi = new DefaultOsBrowserWebUi(interactiveParameters.Message);

                ServiceClientTracing.Information($"[InteractiveUserAuthenticator] Calling AcquireAuthorizationCodeAsync - Scopes: '{string.Join(",", parameters.Scopes)}'");

                Uri authCodeUrl = await customWebUi.AcquireAuthorizationCodeAsync(
                    await app.AsConfidentialClient().GetAuthorizationRequestUrl(parameters.Scopes).ExecuteAsync(cancellationToken).ConfigureAwait(false),
                    new Uri(redirectUri),
                    cancellationToken).ConfigureAwait(false);

                NameValueCollection queryStringParameters = HttpUtility.ParseQueryString(authCodeUrl.Query);

                ServiceClientTracing.Information($"[InteractiveUserAuthenticator] Calling AcquireTokenByAuthorizationCode - Scopes: '{string.Join(",", parameters.Scopes)}'");

                authResult = await app.AsConfidentialClient().AcquireTokenByAuthorizationCode(
                    parameters.Scopes,
                    queryStringParameters["code"]).ExecuteAsync(cancellationToken).ConfigureAwait(false);
            }
            else
            {
                ServiceClientTracing.Information(string.Format(CultureInfo.InvariantCulture, "[InteractiveUserAuthenticator] Calling AcquireTokenInteractive - Scopes: '{0}'", string.Join(",", parameters.Scopes)));

                authResult = await app.AsPublicClient().AcquireTokenInteractive(parameters.Scopes)
                    .WithCustomWebUi(new DefaultOsBrowserWebUi(interactiveParameters.Message))
                    .WithPrompt(Prompt.ForceLogin)
                    .ExecuteAsync(cancellationToken).ConfigureAwait(false);
            }

            return authResult;
        }