private void btnExport_Click(object sender, EventArgs e)
        {
            // get container to export
            string container = ChooseKeyProviderForm.GetKeyProvider(this, "Choose an RSA Key Container to Export", CryptoAPI.GetKeyContainerNames());

            if (String.IsNullOrEmpty(container))
            {
                return;
            }

            // get file name
            SaveFileDialog save = new SaveFileDialog();

            save.Title           = "Choose a File for RSA Key Container Export";
            save.Filter          = "XML File (.xml)|*.xml";
            save.OverwritePrompt = true;
            save.ShowHelp        = false;
            if (save.ShowDialog(this) != DialogResult.OK)
            {
                return;
            }
            string fileName = save.FileName;

            if (String.IsNullOrEmpty(fileName))
            {
                return;
            }

            // export
            string output = Program.RunTask("-px \"{0}\" \"{1}\" -pri", container, fileName);

            Program.ShowMessageBox(this, output);
        }
Esempio n. 2
0
        string GetKeyProvider(IWin32Window window)
        {
            // get key providers from config file
            XmlDocument xmld = new XmlDocument();

            xmld.Load(ConfigFilePath);
            XmlNodeList providers = xmld.SelectNodes("configuration/configProtectedData/providers/add[@name]");

            // if none found, show error
            if (providers == null || providers.Count == 0)
            {
                MessageBox.Show(window, "No providers found in configProtectedData section of " + ConfigFilePath, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(null);
            }
            // if one found, return it
            if (providers.Count == 1)
            {
                return(providers[0].Attributes["name"].Value);
            }
            // handle multiple key providers
            List <string> providerKeys = new List <string>();

            foreach (XmlNode item in providers)
            {
                providerKeys.Add(item.Attributes["name"].Value);
            }
            return(ChooseKeyProviderForm.GetKeyProvider(window, ConfigFilePath, SectionName, providerKeys));
        }
        private void btnPermission_Click(object sender, EventArgs e)
        {
            // get container to grant on
            string container = ChooseKeyProviderForm.GetKeyProvider(this, "Choose an RSA Key Container to Grant Permissions For", CryptoAPI.GetKeyContainerNames());

            if (String.IsNullOrEmpty(container))
            {
                return;
            }

            // get user name to grant to
            string user = GetStringForm.Prompt(this, "Enter User Name", "Enter the Windows user to grant access to. (Usually 'NT AUTHORITY\\NETWORK SERVICE' in IIS versions before 7.5 and 'APPPOOL\\YourAppPoolName' in IIS versions 7.5 and later.)");

            if (String.IsNullOrEmpty(user))
            {
                return;
            }

            // grant
            string output = Program.RunTask("-pa \"{0}\" \"{1}\"", container, user);

            Program.ShowMessageBox(this, output);
        }
        private void btnConfig_Click(object sender, EventArgs e)
        {
            // get file name
            OpenFileDialog open = new OpenFileDialog();

            open.Title    = "Choose a web.config File to add RSA Key Container to";
            open.Filter   = "config File (.config)|*.config";
            open.ShowHelp = false;
            if (open.ShowDialog(this) != DialogResult.OK)
            {
                return;
            }
            string fileName = open.FileName;

            if (String.IsNullOrEmpty(fileName))
            {
                return;
            }

            // get container to grant on
            string container = ChooseKeyProviderForm.GetKeyProvider(this, "Choose an RSA Key Container to Add", CryptoAPI.GetKeyContainerNames());

            if (String.IsNullOrEmpty(container))
            {
                return;
            }

            // open config file
            try
            {
                // load file
                XmlDocument xmld = new XmlDocument();
                xmld.Load(fileName);

                // ensure not already present
                string  xpath = String.Format("/configuration/configProtectedData/providers/add[@keyContainerName='{0}']", container);
                XmlNode node  = xmld.SelectSingleNode(xpath);
                if (node != null)
                {
                    MessageBox.Show(this, String.Format("RSA Key Container {0} was already configured in {1}.", container, fileName),
                                    "Already Configured", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                // create node
                node = xmld.CreateNode(XmlNodeType.Element, "add", "");
                AppendAttribute(node, "name", container + "Provider");
                AppendAttribute(node, "type", String.Format(
                                    "System.Configuration.RsaProtectedConfigurationProvider, System.Configuration, Version={0}, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL",
                                    ConfigurationManager.AppSettings["SystemConfigurationVersion"] ?? "4.0.0.0"));
                AppendAttribute(node, "keyContainerName", container);
                AppendAttribute(node, "useMachineContainer", "true");
                GetKeyProvidersNode(xmld).AppendChild(node);
                // save updated XML
                xmld.Save(fileName);
                // update user
                MessageBox.Show(this, String.Format("RSA Key Container {0} configured in {1}.", container, fileName),
                                "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                // show error
                MessageBox.Show(this, ex.Message, "An Error Occurred", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }