This class contains the credentials for a Windows VM.
        private static string CreateRdpFile(Instance instance, WindowsInstanceCredentials credentials)
        {
            var instanceRootPath = WindowsCredentialsStore.Default.GetStoragePathForInstance(instance);
            var rdpPath = Path.Combine(instanceRootPath, GetRdpFileName(credentials));

            WriteRdpFile(rdpPath, instance, credentials);
            return rdpPath;
        }
        /// <summary>
        /// Creates an .rdp file with the minimal set of settings required to connect to the VM.
        /// </summary>
        public static void WriteRdpFile(string path, Instance instance, WindowsInstanceCredentials credentials)
        {
            using (var writer = new StreamWriter(path))
            {
                // The IP (or name) of the VM to connect to.
                writer.WriteLine($"full address:s:{instance.GetPublicIpAddress()}");

                // The user name to use.
                writer.WriteLine($"username:s:{credentials.User}");

                // The encrypted password to use.
                writer.WriteLine($"password 51:b:{EncryptPassword(credentials.Password)}");
            }
        }
        /// <summary>
        /// Publishes an ASP.NET 4.x project to the given GCE <seealso cref="Instance"/>.
        /// </summary>
        /// <param name="projectPath">The full path to the project file.</param>
        /// <param name="targetInstance">The instance to deploy.</param>
        /// <param name="credentials">The Windows credentials to use to deploy to the <paramref name="targetInstance"/>.</param>
        /// <param name="progress">The progress indicator.</param>
        /// <param name="outputAction">The action to call with lines of output.</param>
        /// <returns></returns>
        public static async Task<bool> PublishProjectAsync(
            string projectPath,
            Instance targetInstance,
            WindowsInstanceCredentials credentials,
            IProgress<double> progress,
            Action<string> outputAction)
        {
            var stagingDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
            Directory.CreateDirectory(stagingDirectory);
            progress.Report(0.1);

            var publishSettingsPath = Path.GetTempFileName();
            var publishSettingsContent = targetInstance.GeneratePublishSettings(credentials.User, credentials.Password);
            File.WriteAllText(publishSettingsPath, publishSettingsContent);

            using (var cleanup = new Disposable(() => Cleanup(publishSettingsPath, stagingDirectory)))
            {
                // Wait for the bundle operation to finish and update the progress in the mean time to show progress.
                if (!await ProgressHelper.UpdateProgress(
                        CreateAppBundleAsync(projectPath, stagingDirectory, outputAction),
                        progress,
                        from: 0.1, to: 0.5))
                {
                    return false;
                }
                progress.Report(0.6);

                // Update for the deploy operation to finish and update the progress as it goes.
                if (!await ProgressHelper.UpdateProgress(
                        DeployAppAsync(stagingDirectory, publishSettingsPath, outputAction),
                        progress,
                        from: 0.6, to: 0.9))
                {
                    return false;
                }
                progress.Report(1);
            }

            return true;
        }
 private void OnActionCommand()
 {
     Result = CurrentCredentials;
     _owner.Close();
 }
 private static string GetRdpFileName(WindowsInstanceCredentials credentials) => $"{credentials.User}.rdp";
 /// <summary>
 /// Opens a new Terminal Server session against the given <paramref name="instance"/> with the
 /// given set of <paramref name="credentials"/>.
 /// </summary>
 /// <param name="instance">The Windows VM</param>
 /// <param name="credentials">The credentials to use to connect.</param>
 public static void OpenSession(Instance instance, WindowsInstanceCredentials credentials)
 {
     var rdpPath = CreateRdpFile(instance, credentials);
     Debug.WriteLine($"Saved .rdp file at {rdpPath}");
     Process.Start("mstsc", $"\"{rdpPath}\"");
 }
        /// <summary>
        /// Adds a Windows credential to the store for the given <paramref name="instance"/>.
        /// </summary>
        /// <param name="instance">The GCE VM.</param>
        /// <param name="credentials">The credentials to store.</param>
        public void AddCredentialsToInstance(Instance instance, WindowsInstanceCredentials credentials)
        {
            var instancePath = GetInstancePath(instance);
            var instanceStoragePath = GetStoragePathForInstance(instance);

            SaveEncryptedCredentials(instanceStoragePath, credentials);
            _credentialsForInstance.Remove(instancePath);
        }
 private static string GetFileName(WindowsInstanceCredentials credentials) => $"{credentials.User}{PasswordFileExtension}";
        private void SaveEncryptedCredentials(string path, WindowsInstanceCredentials credentials)
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            var filePath = Path.Combine(path, GetFileName(credentials));
            var passwordBytes = Encoding.UTF8.GetBytes(credentials.Password);
            var encrypted = ProtectedData.Protect(passwordBytes, null, DataProtectionScope.CurrentUser);
            File.WriteAllBytes(filePath, encrypted);
        }
        /// <summary>
        /// Deletes the given credentials from the list of associated credenials for <paramref name="instance"/>.
        /// </summary>
        /// <param name="instance">The GCE VM.</param>
        /// <param name="credentials">The credentials.</param>
        public void DeleteCredentialsForInstance(Instance instance, WindowsInstanceCredentials credentials)
        {
            var instancePath = GetInstancePath(instance);
            var instanceStoragePath = GetStoragePathForInstance(instance);
            var credentialsPath = Path.Combine(instanceStoragePath, GetFileName(credentials));

            if (File.Exists(credentialsPath))
            {
                File.Delete(credentialsPath);
                _credentialsForInstance.Remove(instancePath);
            }
        }
        private async void OnAddCredentialsCommand()
        {
            var request = AddWindowsCredentialWindow.PromptUser(_instance);
            if (request == null)
            {
                return;
            }

            WindowsInstanceCredentials credentials;
            if (request.GeneratePassword)
            {
                var resetCredentialsTask = CreateOrResetCredentials(request.User);
                credentials = await ProgressDialogWindow.PromptUser(
                    resetCredentialsTask,
                    new ProgressDialogWindow.Options
                    {
                        Title = Resources.ResetPasswordProgressTitle,
                        Message = String.Format(Resources.ResetPasswordProgressMessage, request.User),
                        IsCancellable = false
                    });
                if (credentials != null)
                {
                    ShowPasswordWindow.PromptUser(
                        new ShowPasswordWindow.Options
                        {
                            Title = String.Format(Resources.ShowPasswordWindowTitle, _instance.Name),
                            Message = String.Format(Resources.ShowPasswordNewPasswordMessage, credentials.User),
                            Password = credentials.Password,
                        });
                }
            }
            else
            {
                credentials = new WindowsInstanceCredentials
                {
                    User = request.User,
                    Password = request.Password
                };
            }

            if (credentials != null)
            {
                WindowsCredentialsStore.Default.AddCredentialsToInstance(_instance, credentials);
                CredentialsList = WindowsCredentialsStore.Default.GetCredentialsForInstance(_instance);

                EventsReporterWrapper.ReportEvent(AddWindowsCredentialEvent.Create());
            }
        }