How to use certificate callback in SslStream.AuthenticateAsClient method?

前端 未结 3 1683
孤独总比滥情好
孤独总比滥情好 2021-01-14 18:47

My C#.NET SSL connect works when I import the certificate manually in IE (Tools/Internet Options/Content/Certificates), but how can I load the certificate by code? Here is m

3条回答
  •  逝去的感伤
    2021-01-14 19:03

    I wrote another method to add my certificate to Trusted Root Certification Authorities (root) before attempting to authenticate as client via SSLStream object:

    public static void InstallCertificate()
        {
            X509Store store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
            store.Open(OpenFlags.ReadWrite);
            string fileName = "sslcert.pem";
            X509Certificate2 certificate1;
            try
            {
                certificate1 = new X509Certificate2(fileName);
            }
            catch (Exception ex)
            {
                throw new Exception("Error loading SSL certificate file." + Environment.NewLine + fileName);
            }
    
            store.Add(certificate1);
            store.Close();
        }
    

    And then:

    InstallCertificate();
    sslStream.AuthenticateAsClient("Test");
    

    It works fine without any warnings or errors. But base question still remains unsolved:

    How can I use a certificate to authenticate as client without installing it in Windows?

提交回复
热议问题