help me Certificate Verification (C#)

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
 
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() );
}
}
}

Problem was trivial :)

solution:
objRequest.ContentLength = 0;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top