コード例 #1
0
        public bool AddContactCertificate(string strContactID, X509Certificate cert)
        {
            if (strContactID == null || strContactID.Length == 0)
            {
                throw new ArgumentException("Invalid contact ID", "strContactID");
            }
            if (cert == null)
            {
                throw new ArgumentNullException("cert", "Invalid Contact X509 Certificate");
            }

            // Prevent any attempt to add multiple certificates for a contact
            if (IsContactCertificateInStore(strContactID))
            {
                return(true);
            }

            bool bRetVal = true;

            // Use CAPICOM (v2.0) support to add certificate
            StoreClass store = new StoreClass();

            store.Open(CAPICOM_STORE_LOCATION.CAPICOM_LOCAL_MACHINE_STORE,
                       X509CertificateStore.MyStore,
                       CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_EXISTING_ONLY |
                       CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_MAXIMUM_ALLOWED
                       );

            // Store certificate
            // Convert cert to base64 string
            string strBase64Cert = cert.ToBase64String();
            // Save to temp file
            string       strTempFile = Guid.NewGuid().ToString();
            StreamWriter sw          = new StreamWriter(File.Create(strTempFile));

            sw.Write(strBase64Cert);
            sw.Flush();
            sw.Close();
            // Load cert from temp file
            store.Load(strTempFile, Constants.DEFAULT_CERT_PSWD, CAPICOM_KEY_STORAGE_FLAG.CAPICOM_KEY_STORAGE_DEFAULT | CAPICOM_KEY_STORAGE_FLAG.CAPICOM_KEY_STORAGE_EXPORTABLE);
            // Delete temp file
            File.Delete(strTempFile);
            // Close store
            store.CloseHandle(store.StoreHandle);
            // Return true
            return(bRetVal);
        }
コード例 #2
0
        public bool RemoveContactCertificate(string strContactID)
        {
            if (strContactID == null || strContactID.Length == 0)
            {
                throw new ArgumentException("Invalid contact ID", "strContactID");
            }

            // Prevent any attempt to add multiple certificates for a contact
            if (!IsContactCertificateInStore(strContactID))
            {
                return(true);
            }

            bool bRetVal = true;

            // Use CAPICOM (v2.0) support to remove certificate
            StoreClass store = new StoreClass();

            store.Open(CAPICOM_STORE_LOCATION.CAPICOM_LOCAL_MACHINE_STORE,
                       X509CertificateStore.MyStore,
                       CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_EXISTING_ONLY |
                       CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_MAXIMUM_ALLOWED
                       );

            // Remove "CN=" prefix from contact ID
            // Hack because CAPICOM Find by Subject name will not ignore the "CN="
            // unlike Microsoft.Web.Services.Security.X509.X509CertificateStore
            int nStart = strContactID.LastIndexOf("=");

            strContactID = strContactID.Substring(nStart + 1);
            // Find the cert to remove
            Certificates certCol = ((Certificates)store.Certificates).Find(CAPICOM_CERTIFICATE_FIND_TYPE.CAPICOM_CERTIFICATE_FIND_SUBJECT_NAME, strContactID, false);
            IEnumerator  it      = certCol.GetEnumerator();

            while (it.MoveNext())
            {
                store.Remove((Certificate)it.Current);
            }

            // Close store
            store.CloseHandle(store.StoreHandle);
            return(bRetVal);
        }