0

I have an issue when trying to get some SSL Certificate information.

Generally, the code works fine, but in some cases, I am getting a 403 Forbidden error. I want to get the certificate information even if this happens. There are also a couple of cases where I'm getting invalid logon credentials as well.

I want to get the X509Certificate object regardless of either of these issues.

If I use IE to browse them, it gives me the certificate information, regardless of the message.

So for the sample code, both request and response are defined as object variables, and request is set up properly before calling getRequest().

This code works fine if there are no web errors, but if there is an error, the variable response is null.

Is there another way to get the X509Certificate into an object variable called cert, even seeing these errors?

I honestly don't care if I get the response working, I only care about the X509Certificate.

// callback used to validate the certificate in an SSL conversation private static bool ValidateRemoteCertificate( object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors policyErrors ) { // allow any old dodgy certificate... log.write(0, "entering ValidateRemoeCertificate "); return true; } // getRequest private int getRequest() { try { log.write(1, "Validating " + webHostName); // request.Method = method; ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateRemoteCertificate); response = (HttpWebResponse)request.GetResponse(); response.Close(); } catch (Exception w) { if (w.Message.Contains("401")) { log.write(4, "Web Site Authentication Required for " + host + ":" + port); return 401; } else if (w.Message.Contains("403")) { log.write(4, "Forbidden Access for " + host + ":" + port); return 403; } else { log.write(4, "Error Reading Web Site " + host + " Port " + port + " " + w.Message); return 400; } } return 0; } 

Thank you for any assistance!

1 Answer 1

1

Use the SslStream class instead. Use a constructor that takes a RemoteCertificateValidationCallback delegate, which is called during AuthenticateAsClient to examine the server's certificate.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you very much akton... it worked and is now exactly what I need. Much appreciated!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.