PC Review


Reply
Thread Tools Rate Thread

How To associate ServicePointManager with Connection?

 
 
Jeffrey Walton
Guest
Posts: n/a
 
      1st Jan 2012
Hi All,

I have a ConnectionString which includes 'Encrypt=true', which uses
SSL/TLS on the connection (or encourages its use).

I want to perform some additional processing and testing with
ServicePointManager in in ServerCertificateValidationCallback.

My test code is below (adapted from Arne Vajhøj's earlier code).
Unfortunately, ServerCertificateValidationCallback is not called, and
I can't seem to figure out how to wire in ServicePointManager and
ServerCertificateValidationCallback with Connection or
ConnectionString.

Any ideas?

Jeff

public static void Main(string[] args)
{
ServicePointManager.ServerCertificateValidationCallback =
PinCertificate;
//WebRequest wr = WebRequest.Create("https://sql-server.home.pvt/");
//wr.GetResponse();

String connectionString = "Server=tcp:SQL-Server; User
Id=development; Password=Password1; Encrypt=true";
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
}

public static bool PinCertificate(object sender, X509Certificate
certificate,
X509Chain chain, SslPolicyErrors
sslPolicyErrors)
{
if (certificate == null)
return false;

if (chain == null)
return false;

byte[] chash = certificate.GetCertHash();

StringBuilder sb = new StringBuilder(chash.Length * 2);
foreach (byte b in chash)
sb.AppendFormat("{0:X2}", b);

// Verify against known SHA1 thumb print of the certificate
String hash = sb.ToString();
if (hash != "NNNN...NNNN")
return false;

return true;
}
 
Reply With Quote
 
 
 
 
Arne Vajhøj
Guest
Posts: n/a
 
      1st Jan 2012
On 1/1/2012 2:52 PM, Jeffrey Walton wrote:
> I have a ConnectionString which includes 'Encrypt=true', which uses
> SSL/TLS on the connection (or encourages its use).
>
> I want to perform some additional processing and testing with
> ServicePointManager in in ServerCertificateValidationCallback.
>
> My test code is below (adapted from Arne Vajhøj's earlier code).
> Unfortunately, ServerCertificateValidationCallback is not called, and
> I can't seem to figure out how to wire in ServicePointManager and
> ServerCertificateValidationCallback with Connection or
> ConnectionString.
>
> Any ideas?


To my best knowledge ServicePointManager is a HTTP(S)/URI/web only
thing.

SQLServer TDS encryption is something different.

According to:

http://msdn.microsoft.com/en-us/library/ms189067.aspx

the check is strict out of the box for SQLServer 2008 R2.

Arne



> public static void Main(string[] args)
> {
> ServicePointManager.ServerCertificateValidationCallback =
> PinCertificate;
> //WebRequest wr = WebRequest.Create("https://sql-server.home.pvt/");
> //wr.GetResponse();
>
> String connectionString = "Server=tcp:SQL-Server; User
> Id=development; Password=Password1; Encrypt=true";
> SqlConnection connection = new SqlConnection(connectionString);
> connection.Open();
> }
>
> public static bool PinCertificate(object sender, X509Certificate
> certificate,
> X509Chain chain, SslPolicyErrors
> sslPolicyErrors)
> {
> if (certificate == null)
> return false;
>
> if (chain == null)
> return false;
>
> byte[] chash = certificate.GetCertHash();
>
> StringBuilder sb = new StringBuilder(chash.Length * 2);
> foreach (byte b in chash)
> sb.AppendFormat("{0:X2}", b);
>
> // Verify against known SHA1 thumb print of the certificate
> String hash = sb.ToString();
> if (hash != "NNNN...NNNN")
> return false;
>
> return true;
> }


 
Reply With Quote
 
Jeffrey Walton
Guest
Posts: n/a
 
      1st Jan 2012
On Jan 1, 3:03*pm, Arne Vajhøj <a...@vajhoej.dk> wrote:
> On 1/1/2012 2:52 PM, Jeffrey Walton wrote:
>
> > I have a ConnectionString which includes 'Encrypt=true', which uses
> > SSL/TLS on the connection (or encourages its use).

>
> > I want to perform some additional processing and testing with
> > ServicePointManager *in in ServerCertificateValidationCallback.

>
> > My test code is below (adapted from Arne Vajhøj's earlier code).
> > Unfortunately, ServerCertificateValidationCallback is not called, and
> > I can't seem to figure out how to wire in ServicePointManager and
> > ServerCertificateValidationCallback with Connection or
> > ConnectionString.

>
> > Any ideas?

>
> To my best knowledge ServicePointManager is a HTTP(S)/URI/web only
> thing.
>
> SQLServer TDS encryption is something different.
>
> According to:
>
> http://msdn.microsoft.com/en-us/library/ms189067.aspx
>
> the check is strict out of the box for SQLServer 2008 R2.

Thanks Arne.

https://connect.microsoft.com/Visual...ication-checks

Jeff
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:10 PM.