/// <summary> /// Sets the application data based on hybrid connection details. /// </summary> /// <param name="hybridConnectionDetails">Hybrid connection details.</param> private void SetApplicationData(HybridConnectionDetails hybridConnectionDetails) { bool encryptionEnabled = true; this.Dispatcher.Invoke(() => { encryptionEnabled = this.chkEnableEncryption.IsChecked.GetValueOrDefault(); }); TunnelRelayStateManager.ApplicationData.EnableCredentialEncryption = encryptionEnabled; TunnelRelayStateManager.ApplicationData.HybridConnectionSharedKey = hybridConnectionDetails.HybridConnectionSharedKey; TunnelRelayStateManager.ApplicationData.HybridConnectionKeyName = hybridConnectionDetails.HybridConnectionKeyName; TunnelRelayStateManager.ApplicationData.HybridConnectionUrl = hybridConnectionDetails.RelayUrl; TunnelRelayStateManager.ApplicationData.HybridConnectionName = hybridConnectionDetails.HybridConnectionName; this.Dispatcher.Invoke(() => { MainWindow mainWindow = new MainWindow(); mainWindow.Show(); this.Close(); }); }
/// <summary> /// Handles the Click event of the Done 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 Done_Click(object sender, RoutedEventArgs e) { this.progressBar.Visibility = Visibility.Visible; this.btnDone.IsEnabled = false; RM.Models.SubscriptionInner selectedSubscription = (this.comboSubscriptionList as ComboBox).SelectedItem as RM.Models.SubscriptionInner; RelayNamespaceInner selectedAzureRelay = (this.comboAzureRelayList as ComboBox).SelectedItem as RelayNamespaceInner; string selectedLocation = this.listBoxSubscriptionLocations.SelectedItem.ToString(); string newRelayName = this.txtAzureRelayName.Text; // Case 1. When user used existing Relay. Thread existingAzureRelayThread = new Thread(new ThreadStart(() => { try { HybridConnectionDetails hybridConnectionDetails = this.relayResourceManager.GetHybridConnectionAsync(selectedSubscription, selectedAzureRelay, Environment.MachineName).ConfigureAwait(false).GetAwaiter().GetResult(); this.SetApplicationData(hybridConnectionDetails); } catch (Exception ex) { this.logger.LogError(ex, "Failed to connect to relay namespace"); this.Dispatcher.Invoke(() => MessageBox.Show("Failed to connect to Azure Relay namespace!!", "Azure Error", MessageBoxButton.OKCancel, MessageBoxImage.Error)); } })); // Case 2. When user created a new Relay. Thread newAzureRelayThread = new Thread(new ThreadStart(() => { try { if (string.IsNullOrEmpty(newRelayName)) { MessageBox.Show("Please enter the name for Azure Relay."); this.Dispatcher.Invoke(() => { this.progressBar.Visibility = Visibility.Hidden; this.btnDone.IsEnabled = true; }); return; } if (newRelayName.Length < 6) { MessageBox.Show("Name of Azure Relay must be at least 6 characters."); this.Dispatcher.Invoke(() => { this.progressBar.Visibility = Visibility.Hidden; this.btnDone.IsEnabled = true; }); return; } HybridConnectionDetails hybridConnectionDetails = this.relayResourceManager.CreateHybridConnectionAsync( selectedSubscription, newRelayName, Environment.MachineName, selectedLocation).ConfigureAwait(false).GetAwaiter().GetResult(); this.SetApplicationData(hybridConnectionDetails); } catch (CloudException cloudEx) { this.logger.LogError(cloudEx, "Cloud exception while creating Azure Relay namespace."); this.Dispatcher.Invoke(() => { MessageBox.Show(cloudEx.Message, "Azure Error", MessageBoxButton.OKCancel, MessageBoxImage.Error); this.progressBar.Visibility = Visibility.Hidden; this.btnDone.IsEnabled = true; }); } catch (Exception ex) { this.logger.LogError(ex, "Failed to create new Azure Relay namespace"); this.Dispatcher.Invoke(() => { MessageBox.Show("Failed to create new Azure Relay namespace!!", "Azure Error", MessageBoxButton.OKCancel, MessageBoxImage.Error); this.progressBar.Visibility = Visibility.Hidden; this.btnDone.IsEnabled = true; }); } })); // If the user selected to new Relay entry we added. if (selectedAzureRelay.Id == null) { newAzureRelayThread.Start(); } else { existingAzureRelayThread.Start(); } }
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, }); }