I know this is a fairly common fault, but have not found the answer in similar questions.
I am calling a webservice over https, adding a certificate to the request (found by thumbprint in certificate store):
X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
byte[] certifikat = null;
foreach (X509Certificate2 cert in store.Certificates)
{
if (cert.Thumbprint.Equals(thumbprint))
certifikat = cert.GetRawCertData();
}
System.Security.Cryptography.X509Certificates.X509Certificate cert =
new System.Security.Cryptography.X509Certificates.X509Certificate(certifikat);
request.ClientCertificates.Add(cert);
ServicePointManager.SecurityProtocol =
SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls;
I have added a section to the config file to get a System.Net trace. My web client finds the certificate, and loads it, but System.Net cannot find it (SecureChannel#34640832 - Cannot find the certificate in either the LocalMachine store or the CurrentUser store).
Why?
Full System.Net log below:
System.Net Verbose: 0 : [5284] WebRequest::Create(https://erst-apipreprod.virk.dk/distribution-service-cvr-ekstern/HentAktuelDeltagerEkstern/enhedsnr/123456789?virksomheder&attributter)
System.Net Verbose: 0 : [5284] HttpWebRequest#2383799::HttpWebRequest(https://erst-apipreprod.virk.dk/distribution-service-cvr-ekstern/HentAktuelDeltagerEkstern/enhedsnr/123456789?virksomheder&attributter#-1497397424)
System.Net Information: 0 : [5284] Current OS installation type is 'Server'.
System.Net Information: 0 : [5284] RAS supported: True
System.Net Verbose: 0 : [5284] Exiting HttpWebRequest#2383799::HttpWebRequest()
System.Net Verbose: 0 : [5284] Exiting WebRequest::Create() -> HttpWebRequest#2383799
System.Net Verbose: 0 : [5284] ServicePoint#58870012::ServicePoint(erst-apipreprod.virk.dk:443)
System.Net Information: 0 : [5284] Associating HttpWebRequest#2383799 with ServicePoint#58870012
System.Net Verbose: 0 : [5284] HttpWebRequest#2383799::GetResponse()
System.Net Information: 0 : [5284] Associating Connection#60068066 with HttpWebRequest#2383799
System.Net Information: 0 : [5284] Connection#60068066 - Created connection from 192.168.64.34:50453 to 193.88.50.132:443.
System.Net Information: 0 : [5284] TlsStream#3741682::.ctor(host=erst-apipreprod.virk.dk, #certs=1)
System.Net Information: 0 : [5284] Associating HttpWebRequest#2383799 with ConnectStream#33675143
System.Net Information: 0 : [5284] HttpWebRequest#2383799 - Request: GET /distribution-service-cvr-ekstern/HentAktuelDeltagerEkstern/enhedsnr/123456789?virksomheder&attributter HTTP/1.1
System.Net Information: 0 : [5284] ConnectStream#33675143 - Sending headers
{
Accept: application/json
Content-Type: application/json
Host: erst-apipreprod.virk.dk
Connection: Keep-Alive
}.
System.Net Information: 0 : [5284] SecureChannel#34640832::.ctor(hostname=erst-apipreprod.virk.dk, #clientCertificates=1, encryptionPolicy=RequireEncryption)
System.Net Information: 0 : [5284] Enumerating security packages:
System.Net Information: 0 : [5284] Negotiate
System.Net Information: 0 : [5284] NegoExtender
System.Net Information: 0 : [5284] Kerberos
System.Net Information: 0 : [5284] NTLM
System.Net Information: 0 : [5284] TSSSP
System.Net Information: 0 : [5284] pku2u
System.Net Information: 0 : [5284] WDigest
System.Net Information: 0 : [5284] Schannel
System.Net Information: 0 : [5284] Microsoft Unified Security Protocol Provider
System.Net Information: 0 : [5284] CREDSSP
System.Net Information: 0 : [5284] SecureChannel#34640832 - Attempting to restart the session using the user-provided certificate: [Subject]
{serial number of certificate here}
[Issuer]
CN=TRUST2408 OCES CA II, O=TRUST2408, C=DK
[Serial Number]
53C25339
[Not Before]
22-02-2016 09:56:44
[Not After]
22-02-2019 09:31:06
[Thumbprint]
0FDA1E459B5F2065292EA0A6589185DFAA04CC52
.
System.Net Information: 0 : [5284] SecureChannel#34640832 - Left with 1 client certificates to choose from.
System.Net Information: 0 : [5284] SecureChannel#34640832 - Trying to find a matching certificate in the certificate store.
System.Net Information: 0 : [5284] SecureChannel#34640832 - Locating the private key for the certificate: [Subject]
{serial number of certificate here}
[Issuer]
CN=TRUST2408 OCES CA II, O=TRUST2408, C=DK
[Serial Number]
53C25339
[Not Before]
22-02-2016 09:56:44
[Not After]
22-02-2019 09:31:06
[Thumbprint]
0FDA1E459B5F2065292EA0A6589185DFAA04CC52
.
System.Net Information: 0 : [5284] SecureChannel#34640832 - Cannot find the certificate in either the LocalMachine store or the CurrentUser store.
System.Net Information: 0 : [5284] AcquireCredentialsHandle(package = Microsoft Unified Security Protocol Provider, intent = Outbound, scc = System.Net.SecureCredential)
System.Net Information: 0 : [5284] InitializeSecurityContext(credential = System.Net.SafeFreeCredential_SECURITY, context = (null), targetName = erst-apipreprod.virk.dk, inFlags = ReplayDetect, SequenceDetect, Confidentiality, AllocateMemory, InitManualCredValidation)
System.Net Information: 0 : [5284] InitializeSecurityContext(In-Buffer length=0, Out-Buffer length=123, returned code=ContinueNeeded).
System.Net Information: 0 : [5284] InitializeSecurityContext(credential = System.Net.SafeFreeCredential_SECURITY, context = 9c6cc0:f5b8fb5320, targetName = erst-apipreprod.virk.dk, inFlags = ReplayDetect, SequenceDetect, Confidentiality, AllocateMemory, InitManualCredValidation)
System.Net Information: 0 : [5284] InitializeSecurityContext(In-Buffers count=2, Out-Buffer length=0, returned code=ContinueNeeded).
System.Net Information: 0 : [5284] InitializeSecurityContext(credential = System.Net.SafeFreeCredential_SECURITY, context = 9c6cc0:f5b8fb5320, targetName = erst-apipreprod.virk.dk, inFlags = ReplayDetect, SequenceDetect, Confidentiality, AllocateMemory, InitManualCredValidation)
System.Net Information: 0 : [5284] InitializeSecurityContext(In-Buffers count=2, Out-Buffer length=0, returned code=ContinueNeeded).
System.Net Information: 0 : [5284] InitializeSecurityContext(credential = System.Net.SafeFreeCredential_SECURITY, context = 9c6cc0:f5b8fb5320, targetName = erst-apipreprod.virk.dk, inFlags = ReplayDetect, SequenceDetect, Confidentiality, AllocateMemory, InitManualCredValidation)
System.Net Information: 0 : [5284] InitializeSecurityContext(In-Buffers count=2, Out-Buffer length=0, returned code=ContinueNeeded).
System.Net Information: 0 : [5284] InitializeSecurityContext(credential = System.Net.SafeFreeCredential_SECURITY, context = 9c6cc0:f5b8fb5320, targetName = erst-apipreprod.virk.dk, inFlags = ReplayDetect, SequenceDetect, Confidentiality, AllocateMemory, InitManualCredValidation)
System.Net Information: 0 : [5284] InitializeSecurityContext(In-Buffers count=2, Out-Buffer length=0, returned code=CredentialsNeeded).
System.Net Information: 0 : [5284] SecureChannel#34640832 - We have user-provided certificates. The server has specified 8 issuer(s). Looking for certificates that match any of the issuers.
System.Net Information: 0 : [5284] SecureChannel#34640832 - Selected certificate: [Subject]
{serial number of certificate here}
[Issuer]
CN=TRUST2408 OCES CA II, O=TRUST2408, C=DK
[Serial Number]
53C25339
[Not Before]
22-02-2016 09:56:44
[Not After]
22-02-2019 09:31:06
[Thumbprint]
0FDA1E459B5F2065292EA0A6589185DFAA04CC52
.
System.Net Information: 0 : [5284] SecureChannel#34640832 - Left with 1 client certificates to choose from.
System.Net Information: 0 : [5284] SecureChannel#34640832 - Trying to find a matching certificate in the certificate store.
System.Net Information: 0 : [5284] SecureChannel#34640832 - Locating the private key for the certificate: [Subject]
{serial number of certificate here}
[Issuer]
CN=TRUST2408 OCES CA II, O=TRUST2408, C=DK
[Serial Number]
53C25339
[Not Before]
22-02-2016 09:56:44
[Not After]
22-02-2019 09:31:06
[Thumbprint]
0FDA1E459B5F2065292EA0A6589185DFAA04CC52
.
System.Net Information: 0 : [5284] SecureChannel#34640832 - Cannot find the certificate in either the LocalMachine store or the CurrentUser store.
System.Net Information: 0 : [5284] Using the cached credential handle.
System.Net Information: 0 : [5284] InitializeSecurityContext(credential = System.Net.SafeFreeCredential_SECURITY, context = 9c6cc0:f5b8fb5320, targetName = erst-apipreprod.virk.dk, inFlags = ReplayDetect, SequenceDetect, Confidentiality, AllocateMemory, InitManualCredValidation)
System.Net Information: 0 : [5284] InitializeSecurityContext(In-Buffers count=2, Out-Buffer length=141, returned code=ContinueNeeded).
System.Net Information: 0 : [5284] InitializeSecurityContext(credential = System.Net.SafeFreeCredential_SECURITY, context = 9c6cc0:f5b8fb5320, targetName = erst-apipreprod.virk.dk, inFlags = ReplayDetect, SequenceDetect, Confidentiality, AllocateMemory, InitManualCredValidation)
System.Net Information: 0 : [5284] InitializeSecurityContext(In-Buffers count=2, Out-Buffer length=0, returned code=IllegalMessage).
System.Net Error: 0 : [5284] Exception in HttpWebRequest#2383799:: - The request was aborted: Could not create SSL/TLS secure channel..
System.Net Error: 0 : [5284] Exception in HttpWebRequest#2383799::GetResponse - The request was aborted: Could not create SSL/TLS secure channel..