Search code examples
c#asp.netsslssl-certificatesystem.net.httpwebrequest

ASP.NET, HttpWebRequest and "Could not create SSL/TLS secure channel"


Yes, I know there are dozen of similar questions on SO and google. Yes, I have read them all and tried everything what was said there. And yes, any solution has not helped me.

I have class library, which is used in context of ASP.NET application. My class library downloads data from third-party HTTPS website. That website requires client certificate. I added certificate to LOCAL_MACHINE\MY and granted access to it in winhttpcertcfg.

My LogIn method finds that certificate in store. As I see in debug output - everything is fine on this stage. But later, when I try to load pages, I get The request was aborted: Could not create SSL/TLS secure channel. exception.

My code:

client = new XHttpClient();
X509Store store;
X509CertificateCollection certificates;
store = new X509Store(StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
certificates = store.Certificates.Find(
    X509FindType.FindByIssuerName,
    "Xxxxxxxx Root CA", true);
// everything is fine here, certificates[0] is my certificate
client.ClientCertificates.AddRange(certificates);
store.Close();

Any ideas?


Solution

  • Okay, guys. I found the reason myself. Thanks to this post, I found that IIS 7.5 runs under IIS_IUSRS user, not NetworkService as I believed. Once I granted access to certificate using winhttpcertcfg, it started to work.