/// <summary> /// Initializes a new instance of the <see cref="SelectAzureRelay"/> class. /// </summary> /// <param name="authenticationDetails">The authentication details for the user.</param> internal SelectAzureRelay(UserAuthenticator authenticationDetails) { this.ContentRendered += this.Window_ContentRendered; this.InitializeComponent(); this.userAuthenticator = authenticationDetails; this.relayResourceManager = new AzureRelayResourceManager(this.userAuthenticator); this.comboSubscriptionList.ItemsSource = this.subscriptions; this.comboAzureRelayList.ItemsSource = this.relays; // Disable controls to begin with. this.comboSubscriptionList.IsEnabled = false; this.comboAzureRelayList.IsEnabled = false; }
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, }); }