techques
asked on
How to accept ssl cert automatically through httpRequest?
Hi
I use the code to get the xml return from a web service.
It throws exception:
System.Net.WebException: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel. ---> System.Security.Authentica tion.Authe nticationE xception: The remote certificate is invalid according to the validation procedure.
How can I code it in VC# to automatically accept the cert?
I use ASP.NET and C#.
I use the code to get the xml return from a web service.
It throws exception:
System.Net.WebException: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel. ---> System.Security.Authentica
How can I code it in VC# to automatically accept the cert?
I use ASP.NET and C#.
WebRequest request = WebRequest.Create(url);
WebResponse response = request.GetResponse();
I have did a validation check to bypass the certificate only from a desired server. If you want to enable all certificates, just ignore the conditions. The value for the certificate validation can be obtained from your certificate properties.
ASKER
The type or namespace name 'X509Certificate' could not be found (are you missing a using directive or an assembly reference?)
please include
using System.Security.Cryptograp hy.X509Cer tificates;
using System.Security.Cryptograp
ASKER
yes, i included using System.Security.Cryptograp hy.X509Cer tificates;
The type or namespace name 'SslPolicyErrors' could not be found
The type or namespace name 'SslPolicyErrors' could not be found
ASKER
I added
using System.Net.Security and it can compile. However, when I run your code, it throws the same error:
System.Net.WebException: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel. ---> System.Security.Authentica tion.Authe nticationE xception: The remote certificate is invalid according to the validation procedure
It did not bypass the cert.
using System.Net.Security and it can compile. However, when I run your code, it throws the same error:
System.Net.WebException: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel. ---> System.Security.Authentica
It did not bypass the cert.
Can you please post the code?
ASKER
Here is the code:
string h = "https://Hostmachine/csdb/servlet/Check?MSISDN="+me+"&Username=username& Password=p assword";
was tested with real ip, username and password
string h = "https://Hostmachine/csdb/servlet/Check?MSISDN="+me+"&Username=username&
was tested with real ip, username and password
private string Check(string me)
{
string h = "https://Hostmachine/servlet/Check?MSISDN="+me+"&Username=username&Password=password";
ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(CheckServerCertificateValid);
WebRequest request = WebRequest.Create(h);
WebResponse response = request.GetResponse();
Stream s = response.GetResponseStream();
StreamReader sreader = new StreamReader(s);
String str = sreader.ReadToEnd();
XmlDocument doc = new XmlDocument();
doc.LoadXml(str);
XmlNode node = doc.SelectSingleNode("//sourcecode");
string str_code = node.InnerText;
string str_num = "";
if (str_code.Equals("500"))
{
str_num = "505";
}
return str_num;
}
private static bool CheckServerCertificateValid(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error)
{
if (cert.Subject.Equals("CN=xxxx, OU=xxxx, O=xxxx, L=xxxx, S=xx, C=xx"))
{
return true;
}
return false;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window