private void UpdateServiceConfigurations(CloudServiceProject service, string forwarderName, Certificate certElement, string encryptedPassword)
        {
            foreach (ServiceConfiguration config in new[] { service.Components.LocalConfig, service.Components.CloudConfig })
            {
                foreach (RoleSettings role in config.Role)
                {
                    if (role.Certificates == null)
                    {
                        role.Certificates = new Certificate[0];
                    }

                    Certificate existingCert = role.Certificates.FirstOrDefault(c => c.name == certElement.name);
                    if (existingCert != null)
                    {
                        // ensure we're referencing the right cert
                        existingCert.thumbprint = certElement.thumbprint;
                    }
                    else
                    {
                        role.Certificates = role.Certificates.Concat(new[] { certElement }).ToArray();
                    }

                    Dictionary <string, string> settings = new Dictionary <string, string>();
                    foreach (ConfigurationSetting setting in role.ConfigurationSettings)
                    {
                        settings[setting.name] = setting.value;
                    }
                    settings["Microsoft.WindowsAzure.Plugins.RemoteAccess.Enabled"]                  = "true";
                    settings["Microsoft.WindowsAzure.Plugins.RemoteAccess.AccountUsername"]          = Username;
                    settings["Microsoft.WindowsAzure.Plugins.RemoteAccess.AccountEncryptedPassword"] = encryptedPassword;
                    settings["Microsoft.WindowsAzure.Plugins.RemoteAccess.AccountExpiration"]        = (DateTime.Now + TimeSpan.FromDays(365)).ToString("o");

                    if (role.name == forwarderName)
                    {
                        settings["Microsoft.WindowsAzure.Plugins.RemoteForwarder.Enabled"] = "true";
                    }

                    role.ConfigurationSettings = settings.Select(pair => new ConfigurationSetting {
                        name = pair.Key, value = pair.Value
                    }).ToArray();
                }
            }
        }
        public void EnableRemoteDesktop()
        {
            Validate.ValidateStringIsNullOrEmpty(Username, "Username");
            if (Password == null)
            {
                throw new ArgumentNullException("Password");
            }

            string plainPassword = GetPlainPassword();

            if (!IsPasswordComplex(plainPassword))
            {
                throw new ArgumentException(Resources.EnableAzureRemoteDesktopCommand_Enable_NeedComplexPassword);
            }

            CloudServiceProject service = new CloudServiceProject(General.GetServiceRootPath(CurrentPath()), null);

            WebRole[]    webRoles    = service.Components.Definition.WebRole ?? new WebRole[0];
            WorkerRole[] workerRoles = service.Components.Definition.WorkerRole ?? new WorkerRole[0];

            string forwarderName = GetForwarderName(webRoles, workerRoles);

            RemoveOtherRemoteForwarders(webRoles, workerRoles, forwarderName);
            AddRemoteAccess(webRoles, workerRoles);

            X509Certificate2 cert        = ChooseCertificate();
            Certificate      certElement = new Certificate
            {
                name = "Microsoft.WindowsAzure.Plugins.RemoteAccess.PasswordEncryption",
                thumbprintAlgorithm = ThumbprintAlgorithmTypes.sha1,
                thumbprint          = cert.Thumbprint
            };
            string encryptedPassword = Encrypt(plainPassword, cert);

            UpdateServiceConfigurations(service, forwarderName, certElement, encryptedPassword);
            service.Components.Save(service.Paths);

            if (PassThru)
            {
                WriteObject(true);
            }
        }
        public void EnableRemoteDesktop()
        {
            Validate.ValidateStringIsNullOrEmpty(Username, "Username");
            if (Password == null)
            {
                throw new ArgumentNullException("Password");
            }
            
            string plainPassword = GetPlainPassword();
            if (!IsPasswordComplex(plainPassword))
            {
                throw new ArgumentException(Resources.EnableAzureRemoteDesktopCommand_Enable_NeedComplexPassword);
            }

            CloudServiceProject service = new CloudServiceProject(CommonUtilities.GetServiceRootPath(CurrentPath()), null);
            WebRole[] webRoles = service.Components.Definition.WebRole ?? new WebRole[0];
            WorkerRole[] workerRoles = service.Components.Definition.WorkerRole ?? new WorkerRole[0];

            string forwarderName = GetForwarderName(webRoles, workerRoles);
            RemoveOtherRemoteForwarders(webRoles, workerRoles, forwarderName);
            AddRemoteAccess(webRoles, workerRoles);

            X509Certificate2 cert = ChooseCertificate();
            Certificate certElement = new Certificate
            {
                name = "Microsoft.WindowsAzure.Plugins.RemoteAccess.PasswordEncryption",
                thumbprintAlgorithm = ThumbprintAlgorithmTypes.sha1,
                thumbprint = cert.Thumbprint
            };
            string encryptedPassword = Encrypt(plainPassword, cert);
            
            UpdateServiceConfigurations(service, forwarderName, certElement, encryptedPassword);
            service.Components.Save(service.Paths);

            if (PassThru)
            {
                WriteObject(true);
            }
        }
        private void UpdateServiceConfigurations(CloudServiceProject service, string forwarderName, Certificate certElement, string encryptedPassword)
        {
            foreach (ServiceConfiguration config in new[] { service.Components.LocalConfig, service.Components.CloudConfig })
            {
                foreach (RoleSettings role in config.Role)
                {
                    if (role.Certificates == null)
                    {
                        role.Certificates = new Certificate[0];
                    }

                    Certificate existingCert = role.Certificates.FirstOrDefault(c => c.name == certElement.name);
                    if (existingCert != null)
                    {
                        // ensure we're referencing the right cert
                        existingCert.thumbprint = certElement.thumbprint;
                    }
                    else
                    {
                        role.Certificates = role.Certificates.Concat(new[] { certElement }).ToArray();
                    }

                    Dictionary<string, string> settings = new Dictionary<string, string>();
                    foreach (ConfigurationSetting setting in role.ConfigurationSettings)
                    {
                        settings[setting.name] = setting.value;
                    }
                    settings["Microsoft.WindowsAzure.Plugins.RemoteAccess.Enabled"] = "true";
                    settings["Microsoft.WindowsAzure.Plugins.RemoteAccess.AccountUsername"] = Username;
                    settings["Microsoft.WindowsAzure.Plugins.RemoteAccess.AccountEncryptedPassword"] = encryptedPassword;
                    settings["Microsoft.WindowsAzure.Plugins.RemoteAccess.AccountExpiration"] = (DateTime.Now + TimeSpan.FromDays(365)).ToString("o");

                    if (role.name == forwarderName)
                    {
                        settings["Microsoft.WindowsAzure.Plugins.RemoteForwarder.Enabled"] = "true";
                    }

                    role.ConfigurationSettings = settings.Select(pair => new ConfigurationSetting { name = pair.Key, value = pair.Value }).ToArray();
                }
            }
        }