Ejemplo n.º 1
0
        private static void ImportPrivateCert(string sPrivateCertPath, string sFilePassword, string m_sThumb, bool m_bIsExportable)
        {
            Hashtable hs = new Hashtable();

            if (m_sIdentityUser == null)
            {
                ManagementObjectSearcher m_mgMISearcher = new ManagementObjectSearcher(new WqlObjectQuery("SELECT * FROM MSBTS_HostSetting"));
                m_mgMISearcher.Scope = new ManagementScope("root\\MicrosoftBizTalkServer");
                m_mgMISearcher.Options.ReturnImmediately = true;

                ManagementBaseObject[] m_moHostInstances = new ManagementBaseObject[m_mgMISearcher.Get().Count];

                m_mgMISearcher.Get().CopyTo(m_moHostInstances, 0);

                foreach (ManagementObject oHost in m_moHostInstances)
                {
                    try
                    {
                        hs.Add(oHost.GetPropertyValue("LastUsedLogon").ToString().ToUpper(CultureInfo.InvariantCulture), null);
                    }
                    catch { }
                }
            }
            else
            {
                hs.Add(m_sIdentityUser, m_sIdentityPassword);
            }

            foreach (string sUser in hs.Keys)
            {
                string sPassword = null;

                if (hs[sUser] == null)
                {
                    Console.Write("\r\nEnter password for identity " + sUser + ": ");
                    sPassword = PwdConsole.ReadLine(); //Console.ReadLine();
                }
                else
                {
                    sPassword = hs[sUser].ToString();
                }
                try
                {
                    PrivateKeyManager.ImpersonateUser(sUser, sPassword);
                    PrivateKeyManager.Import(sPrivateCertPath,
                                             sFilePassword,
                                             m_sThumb,
                                             PrivateKeyManager.CertificateStores.CERT_SYSTEM_STORE_CURRENT_USER, "MY", m_bIsExportable);
                    Console.WriteLine("Completed importing private certificate to user identity " + sUser + ".");

                    PrivateKeyManager.UndoImpersonation();
                }
                catch
                {
                    Console.WriteLine("Failed importing private key to user [" + sUser + "].\n\rCheck that you have entered a valid password.");
                }
            }
        }