Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

Certificate Store C# code works on some computers but not all

$
0
0

oh my code need to always fail the 1st i run it on a computer but the next time i run it it works. but on some computer it does not work at all.

ok here is the code:

  public static void CheckApplicationInstanceCertificate(ApplicationConfiguration configuration)
        {
            // create a default certificate id none specified.
            CertificateIdentifier id = configuration.SecurityConfiguration.ApplicationCertificate;

            if (id == null)
            {
                id = new CertificateIdentifier();
                id.StoreType = CertificateStoreType.Windows;
                id.StorePath = "LocalMachine\\My";
                id.SubjectName = configuration.ApplicationName;
            }

            IList<string> serverDomainNames = configuration.GetServerDomainNames();

            // check for private key.
            X509Certificate2 certificate = id.Find(true);

            if (certificate != null)
            {
                return;
            }

            certificate = id.Find(false);

            if (certificate != null)
            {
                Utils.Trace(Utils.TraceMasks.Error, "Certificate found. But private ket is not accessible: '{0}' {1}", certificate.Subject, certificate.Thumbprint);
                certificate = null;
            }

            // add the host.
            if (serverDomainNames.Count == 0)
            {
                serverDomainNames.Add(System.Net.Dns.GetHostName());
            }

            string commonName = Utils.Format("CN={0}", configuration.ApplicationName);
            string domainName = Utils.Format("DC={0}", serverDomainNames[0]);
            string subjectName = Utils.Format("{0}, {1}", commonName, domainName);

            Utils.Trace(Utils.TraceMasks.Error, "No certificate found. Creating a new certificate: {0}", subjectName);

            // create a new certificate with a new public key pair.
            certificate = CertificateFactory.CreateCertificate(
                id.StoreType,
                id.StorePath,
                configuration.ApplicationUri,
                configuration.ApplicationName,
                subjectName,
                serverDomainNames,
                1024,
                120);

            // update and save the configuration file.
            id.Certificate = certificate;
            configuration.SaveToFile(configuration.SourceFilePath);

            Utils.Trace(Utils.TraceMasks.Error, "Certificate created. '{0}' {1}", certificate.Subject, certificate.Thumbprint);

            // add certificate to the trusted peer store so other applications will trust it.
            ICertificateStore store = configuration.SecurityConfiguration.TrustedPeerCertificates.OpenStore();

            try
            {
                X509Certificate2 certificate2 = store.FindByThumbprint(certificate.Thumbprint);

                if (certificate2 == null)
                {
                    store.Add(certificate);
                }
            }
            finally
            {
                store.Close();
            }

            // tell the certificate validator about the new certificate.
            configuration.CertificateValidator.Update(configuration.SecurityConfiguration);

ok i know why it run the 2 time after it fails the 1st time on a new computer 

it is because of  this line:

 if (certificate != null)
            {
                return;
            }

There is a certificate the next time it get to that point so it goes return

but I do not know why it fails the 1st time.

it fails here :

 certificate = CertificateFactory.CreateCertificate(
                id.StoreType,
                id.StorePath,
                configuration.ApplicationUri,
                configuration.ApplicationName,
                subjectName,
                serverDomainNames,
                1024,
                120);

here is a error I got :

InvalidOperationException was caught
Could not add seft-signed cerificate to certificate store 
System.InvalidOperationException 

I have also see 

Could not open certificate store
Type=LocalMachine, Name= UA Application, Error= 000000B7 

does anyone know a work around to fix this? are does anyone know how to troubleshoot this?

also does all windows computer have a Certificate store?

Can it be lock? could that be why it does not work on some computers ?


Viewing all articles
Browse latest Browse all 31927

Trending Articles