/// <summary>
        /// Handles the Click event of the LoginToAzure control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void LoginToAzure_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.logger.LogInformation("Starting Azure login.");

                UserAuthenticator userAuthDetails = new UserAuthenticator(LoggingHelper.GetLogger <UserAuthenticator>());

                // Raise Authentication prompt and log the user in.
                userAuthDetails.AuthenticateUserAsync().ConfigureAwait(false).GetAwaiter().GetResult();
                this.logger.LogInformation("Token acquire complete.");

                SelectAzureRelay selectAzureRelay = new SelectAzureRelay(userAuthDetails);
                selectAzureRelay.Left = this.Left;
                selectAzureRelay.Top  = this.Top;

                selectAzureRelay.Show();
                this.Close();
            }
            catch (Exception ex)
            {
                this.logger.LogError(ex, "Failed to Login into Azure");
                MessageBox.Show("Failed to log you in!!", "Login failure", MessageBoxButton.OKCancel, MessageBoxImage.Error);
            }
        }
Пример #2
0
        private static async Task <ApplicationData> PerformInteractiveConfigurationAsync(
            IServiceProvider serviceProvider,
            string redirectionUrl = null)
        {
            Console.WriteLine("Please wait while we log you in...");

            UserAuthenticator userAuthenticator = serviceProvider.GetRequiredService <UserAuthenticator>();

            AzureRelayResourceManager relayResourceManager = serviceProvider.GetRequiredService <AzureRelayResourceManager>();

            await userAuthenticator.AuthenticateUserAsync().ConfigureAwait(false);

            Console.WriteLine("Please wait while we gather subscription information...");

            List <SubscriptionInner> userSubscriptions = await userAuthenticator.GetUserSubscriptionsAsync().ConfigureAwait(false);

            if (userSubscriptions.Count == 0)
            {
                Console.Error.WriteLine("No Azure subscriptions found");
                throw new InvalidOperationException("User has no associated subscriptions");
            }

            Console.WriteLine("Select the subscription you want to use");

            for (int i = 0; i < userSubscriptions.Count; i++)
            {
                Console.WriteLine($"{i + 1} - {userSubscriptions[i].DisplayName}({userSubscriptions[i].SubscriptionId})");
            }

            int selectedSubscriptionIndex = 0;

            while (true)
            {
                if (!int.TryParse(Console.ReadLine(), out selectedSubscriptionIndex))
                {
                    Console.Error.WriteLine("Invalid input. Please select the index.");
                    continue;
                }

                if (selectedSubscriptionIndex > userSubscriptions.Count || selectedSubscriptionIndex == 0)
                {
                    Console.Error.WriteLine("Invalid input. Select index out of allowed values");
                    continue;
                }

                break;
            }

            SubscriptionInner selectedSubscription = userSubscriptions[selectedSubscriptionIndex - 1];

            List <RelayNamespaceInner> relayNamespaces = await relayResourceManager.GetRelayNamespacesAsync(selectedSubscription).ConfigureAwait(false);

            int selectedRelayIndex = 0;

            if (relayNamespaces.Count != 0)
            {
                Console.WriteLine("Select the Azure Relay you want to use.");

                Console.WriteLine("0 - Create a new Azure Relay");
                for (int i = 0; i < relayNamespaces.Count; i++)
                {
                    Console.WriteLine($"{i + 1} - {relayNamespaces[i].Name}");
                }

                while (true)
                {
                    if (!int.TryParse(Console.ReadLine(), out selectedRelayIndex))
                    {
                        Console.Error.WriteLine("Invalid input. Please select the index.");
                        continue;
                    }

                    if (selectedRelayIndex > relayNamespaces.Count)
                    {
                        Console.Error.WriteLine("Invalid input. Select index out of allowed values");
                        continue;
                    }

                    break;
                }
            }

            HybridConnectionDetails hybridConnectionDetails = null;

            if (selectedRelayIndex == 0)
            {
                Console.Write("Enter the name for the new Azure Relay. This must be atleast 6 character long and globally unique. ");
                string relayName = Console.ReadLine();

                Console.WriteLine("Select the location for the new Relay from the list below");

                List <Location> subscriptionLocations = userAuthenticator.GetSubscriptionLocations(selectedSubscription).ToList();

                for (int i = 0; i < subscriptionLocations.Count; i++)
                {
                    Console.WriteLine($"{i + 1} - {subscriptionLocations[i].DisplayName}");
                }

                int selectedLocationIndex = 0;
                while (true)
                {
                    if (!int.TryParse(Console.ReadLine(), out selectedLocationIndex))
                    {
                        Console.Error.WriteLine("Invalid input. Please select the index.");
                        continue;
                    }

                    if (selectedRelayIndex > subscriptionLocations.Count || selectedLocationIndex == 0)
                    {
                        Console.Error.WriteLine("Invalid input. Select index out of allowed values");
                        continue;
                    }

                    break;
                }

                Console.WriteLine("Please wait while the new Relay is being created");
                hybridConnectionDetails = await relayResourceManager.CreateHybridConnectionAsync(
                    selectedSubscription,
                    relayName,
                    Environment.MachineName,
                    subscriptionLocations[selectedLocationIndex - 1].DisplayName).ConfigureAwait(false);
            }
            else
            {
                Console.WriteLine("Please wait while the details for Relay are fetched");
                hybridConnectionDetails = await relayResourceManager.GetHybridConnectionAsync(
                    selectedSubscription,
                    relayNamespaces[selectedRelayIndex - 1],
                    Environment.MachineName).ConfigureAwait(false);
            }

            if (string.IsNullOrEmpty(redirectionUrl))
            {
                Console.Write("Enter the endpoint to route requests to. Example http://localhost:4200 ");
                redirectionUrl = Console.ReadLine();
            }

            return(new ApplicationData
            {
                // DPAPI APIs used for encryption are only present on Windows.
                EnableCredentialEncryption = RuntimeInformation.IsOSPlatform(OSPlatform.Windows),
                EnabledPlugins = new HashSet <string>(),
                HybridConnectionKeyName = hybridConnectionDetails.HybridConnectionKeyName,
                HybridConnectionName = hybridConnectionDetails.HybridConnectionName,
                HybridConnectionSharedKey = hybridConnectionDetails.HybridConnectionSharedKey,
                HybridConnectionUrl = hybridConnectionDetails.RelayUrl,
                PluginSettingsMap = new Dictionary <string, Dictionary <string, string> >(),
                RedirectionUrl = redirectionUrl,
            });
        }