none
Unable to import certificate. "Object reference not set to an instance of an object." RRS feed

  • Question

  • Hi there!

    I try to call a certificate without accessing it from the keystore.
    However, I'm getting the following error:

     "Object reference not set to an instance of an object."
    I've tried eevrything and after a day I'm still stuck. Maybe someone can help me?

    This is my code. The place where it throws an error is marked in bold!

    CustomBinding binding = new CustomBinding();
    
    
                    TextMessageEncodingBindingElement msg = new TextMessageEncodingBindingElement
                    {
                        MessageVersion = MessageVersion.Soap12
                    };
                    binding.Elements.Add(msg);
                    HttpsTransportBindingElement transp = new HttpsTransportBindingElement();
                    transp.RequireClientCertificate = true;
                    binding.Elements.Add(transp);
                    binding.Name = "vz801802Soap12";
    
    
                    var endpointStr = ssEndPoint;
                    var endpoint = new EndpointAddress(endpointStr);
    
                    Vecozo_ServiceReference.vz801802ServiceClient client = new Vecozo_ServiceReference.vz801802ServiceClient(binding, endpoint);
                    
                    try
                    {
                        
                        //creating the certificate object    
                        X509Certificate2 certificate = new X509Certificate2();
    
                        //Specify the TLS version to use
                        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                        string certificatePath = @"D:\Certificates\Testomgeving B.V.p12";
                        string CertificatePassword = "fakepassword";
                        client.ClientCredentials.ClientCertificate.Certificate.Import(certificatePath, CertificatePassword,X509KeyStorageFlags.MachineKeySet);
    
    
    
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }


    Monday, October 15, 2018 10:26 PM

All replies

  • Hi Niels,
    You could use the following code snippets to set up the client certificate.
    ServiceReference1.ServiceClient client = new ServiceReference1.ServiceClient("WSHttpBinding_IService");
    X509Certificate2 cert = new X509Certificate2(Path.Combine(@"D:\certificates\","cert.pfx"), "123456");
    client.ClientCredentials.ClientCertificate.Certificate = cert;

    In general, we construct the certificate object by using the full path of the certificate export file, and use the Path.Combine to generate the file path.
    Feel free to contact me if you have any questions
    Best Regards
    Abraham
    Thursday, October 18, 2018 6:28 AM
    Moderator