help me Certificate Verification (C#)

  • Thread starter Thread starter borovA
  • Start date Start date
B

borovA

I am trying to access an Web. The problem is that they
expect the client to submit a certificate. I can access the site from
my browser, but it always asks which certificate I want to use. How
can I do this in my code.

So far I can get the certificate. It's properly
formatted, and I can instantiate a X509Certificate object
with it. But I can't figure out how to add it to the Root
store, so it is trusted. Even if I add it to the Root
with IE, I still get the "The underlying connection was
closed: Could not establish trust relationship with remote
server." error message, when I connect to the HTTPS
resource via C#.

here is my code:

using System;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;

public class CertPolicy : ICertificatePolicy
{
public bool CheckValidationResult(ServicePoint sp, X509Certificate cert,
WebRequest request, int problem)
{
return true;
}
}

public class CertTest
{

public static void Main()
{
try
{
HttpWebRequest objRequest =
(HttpWebRequest)WebRequest.Create("https://myweb/");
objRequest.Method = "POST";
X509Certificate myCert =
X509Certificate.CreateFromCertFile(@"d:\a\cert.der");
X509CertificateCollection x509 = objRequest.ClientCertificates;
x509.Add (myCert);
objRequest.ClientCertificates.Add(myCert);
ServicePointManager.CertificatePolicy = new CertPolicy();

HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
}
catch( Exception e )
{
Console.WriteLine( e.ToString() );
}
}
}


Thanks for any help
 
I am trying to access an Web. The problem is that they
expect the client to submit a certificate. I can access the site from
my browser, but it always asks which certificate I want to use. How
can I do this in my code.

So far I can get the certificate. It's properly
formatted, and I can instantiate a X509Certificate object
with it. But I can't figure out how to add it to the Root
store, so it is trusted. Even if I add it to the Root
with IE, I still get the "The underlying connection was
closed: Could not establish trust relationship with remote
server." error message, when I connect to the HTTPS
resource via C#.

here is my code:

using System;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;

public class CertPolicy : ICertificatePolicy
{
public bool CheckValidationResult(ServicePoint sp, X509Certificate cert,
WebRequest request, int problem)
{
return true;
}
}

public class CertTest
{

public static void Main()
{
try
{
HttpWebRequest objRequest =
(HttpWebRequest)WebRequest.Create("https://myweb/");
objRequest.Method = "POST";
X509Certificate myCert =
X509Certificate.CreateFromCertFile(@"d:\a\cert.der");
X509CertificateCollection x509 = objRequest.ClientCertificates;
x509.Add (myCert);
objRequest.ClientCertificates.Add(myCert);
ServicePointManager.CertificatePolicy = new CertPolicy();

HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
}
catch( Exception e )
{
Console.WriteLine( e.ToString() );
}
}
}

Problem was trivial :-)

solution:
objRequest.ContentLength = 0;
 
Back
Top