Blog Archives

asp .net could not establish trust relationship for the SSL/TLS secure channel

A quick google search revealed multiple reported resolutions, however, after following the steps in the MSDN blog reference listed below, the issue was still unresolved in my situation.

Additional details in the stack trace will reveal another similar message: “The remote certificate is invalid according to the validation procedure.”

In this specific scenario, the site in question is either not configured with a wildcard certificate for a subdomain of the parent site or the operation system I am working on does not support SNI. In the meantime, a workaround is needed to continue testing and development.

Additional reading on google revealed another solution which was more suitable and utilized a code based approach, as opposed to a server configuration based solution.

To make it more dynamic, I added a key into the app/web config to control if SSL errors should be ignored. Please note that it is also possible to replace the code based approach solely with an app/web config entry listed in the west-wind blog referenced below, but I personally prefer to go with code whenever possible.

<?xml version="1.0"?>
<configuration>
  <configSections>
  </configSections>
  <connectionStrings>
    <add name="ConnectionString"
      connectionString="Data Source=servername;Initial Catalog=databasename;"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings>
    <add key="ignoresslerrors" value="true"/>
  </appSettings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
 public class ConfigValues
    {
        public static string IgnoreSSLErrors { get { return getval("ignoresslerrors"); } }
}

public function main() {
connect("https://sitename.com",ConfigValues.IgnoreSSLErrors);
}

public function connect(string url, string ignoresslerrors) {
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

try
            {
                if (Convert.ToBoolean(ignoresslerrors))
                {
                    System.Net.ServicePointManager.ServerCertificateValidationCallback +=
            delegate(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate,
                                    System.Security.Cryptography.X509Certificates.X509Chain chain,
                                    System.Net.Security.SslPolicyErrors sslPolicyErrors)
            {
                return true; //will always accept the cert and ignore errors. this is not good common practice unless you are sure of the destination you are connecting to. needed in this scenario to continue development until issue with cert is resolved.
            };
                }
            }
            catch (Exception ex)
            {
                Shared.HandleError(ex);
            }
}

References

http://www.west-wind.com/weblog/posts/2011/Feb/11/HttpWebRequest-and-Ignoring-SSL-Certificate-Errors

http://blogs.msdn.com/b/jpsanders/archive/2009/09/16/troubleshooting-asp-net-the-remote-certificate-is-invalid-according-to-the-validation-procedure.aspx

asp .net c# luhn algorithm validation mod10 with realtime clientside Javascript

Read the rest of this entry

Validate Credit Card Numbers with Regular Expressions

Expressions below account for IIN numbers on CC’s of most major US issuers which includes validating starting character. These should be used in addition to Luhn Algorithm mod10 check.

These expressions can be used on clientside and codebehind to give your end users notification of invalid entry without wasting resources on a failed/invalid post. See snippet below. Expressions should technically be cross-platform.

Note: although very similar to those found on regular-expressions.info, there are some small differences which do not account for the old standards in my examples since these cards are no longer in circulation.

(C#)

string vs = @"((^4)[0-9]{15}$)|";
            string mc = @"((^5[1-5])[0-9]{14}$)|";
            string ax = @"((^3[4|7])[0-9]{14})$|";
            string ds = @"(^6(011|5[0-9]{2})[0-9]{12}$)";

string expirationmonth = @"((^[0-9]$)|(^[0-9][0-2]$))";

string ordertotalamount = @"((^[0-9]{1,5}$)|(^[0-9]{1,5}\.[0-9]{1,2}$))"; //does not account for currency symbols

System.Text.StringBuilder sbexp = new System.Text.StringBuilder();
            for (int i = 0; i <= 20; i++) //create list of years from now+20. same range amazon uses for card
            {
                sbexp.Append("(^" + (DateTime.Now.Year + i).ToString() + "$)|");
                if (i < 20) //don't append or on last
                {
                    sbexp.Append((DateTime.Now.Year + i).ToString());
                }
            }

            ((RegularExpressionValidator)validator).ValidationExpression = sbexp.ToString();

((RegularExpressionValidator)validator).ValidationExpression = vs+mc+ax+ds;

References
Wikipedia (IIN numbers), http://en.wikipedia.org/wiki/Bank_card_number
Wikipedia (Luhn Algorithm), http://en.wikipedia.org/wiki/Luhn_algorithm
RegularExpressions.info, http://www.regular-expressions.info/creditcard.html

Follow

Get every new post delivered to your Inbox.