private static X509SecurityToken RetrieveTokenFromStore(X509CertificateStore store, string keyIdentifier)
        {
            if (store == null)
            {
                throw new ArgumentNullException("store");
            }

            X509SecurityToken token = null;

            try
            {
                if (store.OpenRead())
                {
                    // Place the key ID of the certificate in a byte array
                    // This KeyID represents the Wse2Quickstart certificate included with the WSE 2.0 Quickstarts
                    // ClientBase64KeyId is defined in the ClientBase.AppBase class
                    X509CertificateCollection certs = store.FindCertificateByKeyIdentifier(Convert.FromBase64String(keyIdentifier));

                    if (certs.Count > 0)
                    {
                        // Get the first certificate in the collection
                        token = new X509SecurityToken(((X509Certificate)certs[0]));
                    }
                }
            }
            finally
            {
                if (store != null)
                {
                    store.Close();
                }
            }

            return(token);
        }
        public bool IsContactCertificateInStore(string strContactID)
        {
            bool bRetVal = false;

            X509CertificateStore certStore = X509CertificateStore.LocalMachineStore(X509CertificateStore.MyStore);

            if (certStore == null)
            {
                throw new Exception("Error opening Local Machine Store");
            }

            if (certStore.OpenRead())
            {
                X509CertificateCollection certColl = certStore.FindCertificateBySubjectName(strContactID);
                if (certColl.Count == 0)
                {
                    bRetVal = false;
                }
                else
                {
                    bRetVal = true;
                }
            }

            // Close the certificate store
            certStore.Close();

            return(bRetVal);
        }
Exemple #3
0
        /// <summary>
        /// Returns the X.509 SecurityToken that will be used to encrypt the
        /// messages.
        /// </summary>
        /// <returns>Returns </returns>
        public X509SecurityToken GetEncryptionToken()
        {
            X509SecurityToken token = null;
            //
            // The certificate for the target receiver should have been imported
            // into the "My" certificate store. This store is listed as "Personal"
            // in the Certificate Manager
            //
            X509CertificateStore store = X509CertificateStore.CurrentUserStore(X509CertificateStore.MyStore);
            bool open = store.OpenRead();

            try
            {
                //
                // Open a dialog to allow user to select the certificate to use
                //
                StoreDialog     dialog = new StoreDialog(store);
                X509Certificate cert   = dialog.SelectCertificate(IntPtr.Zero, "Select Certificate", "Choose a Certificate below for encrypting.");
                if (cert == null)
                {
                    throw new ApplicationException("You chose not to select an X509 certificate for encrypting your messages.");
                }
                else if (!cert.SupportsDataEncryption)
                {
                    throw new ApplicationException("The certificate must support key encipherment.");
                }
                else
                {
                    token = new X509SecurityToken(cert);
                }
            }
            finally
            {
                if (store != null)
                {
                    store.Close();
                }
            }

            return(token);
        }
Exemple #4
0
        /// <summary>
        /// Gets the security token for signing messages.
        /// </summary>
        /// <returns>Returns </returns>
        public X509SecurityToken GetSecurityToken()
        {
            X509SecurityToken securityToken;
            //
            // open the current user's certificate store
            //
            X509CertificateStore store = X509CertificateStore.CurrentUserStore(X509CertificateStore.MyStore);
            bool open = store.OpenRead();

            try
            {
                //
                // Open a dialog to allow user to select the certificate to use
                //
                StoreDialog     dialog = new StoreDialog(store);
                X509Certificate cert   = dialog.SelectCertificate(IntPtr.Zero, "Select Certificate", "Choose a Certificate below for signing.");
                if (cert == null)
                {
                    throw new ApplicationException("You chose not to select an X509 certificate for signing your messages.");
                }
                else if (!cert.SupportsDigitalSignature)
                {
                    throw new ApplicationException("The certificate must support digital signatures and have a private key available.");
                }
                else
                {
                    securityToken = new X509SecurityToken(cert);
                }
            }
            finally
            {
                if (store != null)
                {
                    store.Close();
                }
            }
            return(securityToken);
        }
        public X509Certificate GetContactCertificate(string strContactID)
        {
            X509CertificateStore certStore = X509CertificateStore.LocalMachineStore(X509CertificateStore.MyStore);

            if (certStore == null)
            {
                throw new Exception("Error opening Local Machine Store");
            }

            X509Certificate cert = null;

            if (certStore.OpenRead())
            {
                X509CertificateCollection certColl = certStore.FindCertificateBySubjectName(strContactID);
                if (certColl.Count == 1)
                {
                    cert = certColl[0];
                }
            }

            // Close the certificate store
            certStore.Close();
            return(cert);
        }
      private static X509SecurityToken RetrieveTokenFromStore (X509CertificateStore store, string keyIdentifier) 
	{
	  if (store == null)
	    throw new ArgumentNullException ("store");
	  X509SecurityToken token = null;
	  try 
	    {
	      if (store.OpenRead ())
		{
		  // Place the key ID of the certificate in a byte array
		  // This KeyID represents the Wse2Quickstart certificate included with the WSE 2.0 Quickstarts
		  // ClientBase64KeyId is defined in the ClientBase.AppBase class
		  X509CertificateCollection certs =
		      store.FindCertificateByKeyIdentifier (Convert.FromBase64String (keyIdentifier));
		  if (certs.Count > 0)

		    {
		      // Get the first certificate in the collection
		      token = new X509SecurityToken (((X509Certificate) certs[0]));
		    }
		}
	    }
	  finally 
	    {
	      if (store != null)
		store.Close ();
	    }
	  return token;
	}
        private static Microsoft.Web.Services2.Security.X509.X509Certificate X509CertificateByThumbprint(string Thumbprint)
        {
            X509Certificate x509 = null;

            if (string.IsNullOrEmpty(Thumbprint))
            {
                throw new ArgumentNullException("Thumbprint is null or empty", new Exception("Thumbprint is mandatory"));
            }

            Thumbprint = Thumbprint.Replace("\u200e", string.Empty).Replace("\u200f", string.Empty).Replace(" ", string.Empty).Replace(":", string.Empty);

            X509CertificateStore store = new X509CertificateStore(X509CertificateStore.StoreProvider.System, X509CertificateStore.StoreLocation.LocalMachine, X509CertificateStore.RootStore);

            store.OpenRead();
            foreach (X509Certificate cert in store.Certificates)
            {
                if (cert.GetCertHashString().Trim().ToUpper() == Thumbprint.Trim().ToUpper())
                {
                    x509 = cert;
                    break;
                }
            }

            store.Close();

            if (x509 == null)
            {
                store = new X509CertificateStore(X509CertificateStore.StoreProvider.System, X509CertificateStore.StoreLocation.LocalMachine, X509CertificateStore.MyStore);
                store.OpenRead();
                foreach (X509Certificate cert in store.Certificates)
                {
                    if (cert.GetCertHashString().Trim().ToUpper() == Thumbprint.Trim().ToUpper())
                    {
                        x509 = cert;
                        break;
                    }
                }
                store.Close();
            }

            if (x509 == null)
            {
                store = new X509CertificateStore(X509CertificateStore.StoreProvider.System, X509CertificateStore.StoreLocation.CurrentUser, X509CertificateStore.RootStore);
                store.OpenRead();
                foreach (X509Certificate cert in store.Certificates)
                {
                    if (cert.GetCertHashString().Trim().ToUpper() == Thumbprint.Trim().ToUpper())
                    {
                        x509 = cert;
                        break;
                    }
                }
                store.Close();
            }

            if (x509 == null)
            {
                store = new X509CertificateStore(X509CertificateStore.StoreProvider.System, X509CertificateStore.StoreLocation.CurrentUser, X509CertificateStore.MyStore);
                store.OpenRead();
                foreach (X509Certificate cert in store.Certificates)
                {
                    if (cert.GetCertHashString().Trim().ToUpper() == Thumbprint.Trim().ToUpper())
                    {
                        x509 = cert;
                        break;
                    }
                }
                store.Close();
            }

            if (x509 == null)
            {
                if (!string.IsNullOrEmpty(Thumbprint))
                {
                    throw new CryptographicException("A x509 certificate for " + Thumbprint + " was not found");
                }
                else
                {
                    throw new CryptographicException("A x509 certificate was not found");
                }
            }
            return(x509);
        }