Connect web service with SSL connection
作者:Peter 日期:2010-07-05
1. Import the cert file
2. Import the pfx file
3.
程序代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using ConsoleApplication1.ScsWebReference;
namespace ConsoleApplication1
{
class Program
{
static string ServiceUrl = "https://172.22.120.154/scs/scsapiwebservice.asmx?wsdl";
static string CertSerialNumber = "1fcf8092000000002d07";
static void Main(string[] args)
{
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>
{
if (sslPolicyErrors == SslPolicyErrors.RemoteCertificateNameMismatch || sslPolicyErrors == SslPolicyErrors.None)
{
return true;
}
else
{
return false;
}
};
bdk scs = new bdk();
scs.Url = ServiceUrl;
scs.ClientCertificates.Add(GetCert(CertSerialNumber));
string testConnectionOut;
scs.TestConnection("Scs Test", out testConnectionOut);
}
private static X509Certificate GetCert(string serialNumber)
{
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.OpenExistingOnly);
try
{
foreach (X509Certificate2 cert in store.Certificates)
{
if (cert.GetSerialNumberString().Equals(serialNumber, StringComparison.OrdinalIgnoreCase))
{
if (!cert.HasPrivateKey)
{
throw new ApplicationException("The cert (" + serialNumber + ") does not have a private key");
}
Console.Write("Got the cert, serial number:" + serialNumber);
return cert;
}
}
throw new ApplicationException("Cannot find cert with SN=" + serialNumber);
}
finally
{
store.Close();
}
}
}
}
2. Import the pfx file
3.
程序代码using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using ConsoleApplication1.ScsWebReference;
namespace ConsoleApplication1
{
class Program
{
static string ServiceUrl = "https://172.22.120.154/scs/scsapiwebservice.asmx?wsdl";
static string CertSerialNumber = "1fcf8092000000002d07";
static void Main(string[] args)
{
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>
{
if (sslPolicyErrors == SslPolicyErrors.RemoteCertificateNameMismatch || sslPolicyErrors == SslPolicyErrors.None)
{
return true;
}
else
{
return false;
}
};
bdk scs = new bdk();
scs.Url = ServiceUrl;
scs.ClientCertificates.Add(GetCert(CertSerialNumber));
string testConnectionOut;
scs.TestConnection("Scs Test", out testConnectionOut);
}
private static X509Certificate GetCert(string serialNumber)
{
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.OpenExistingOnly);
try
{
foreach (X509Certificate2 cert in store.Certificates)
{
if (cert.GetSerialNumberString().Equals(serialNumber, StringComparison.OrdinalIgnoreCase))
{
if (!cert.HasPrivateKey)
{
throw new ApplicationException("The cert (" + serialNumber + ") does not have a private key");
}
Console.Write("Got the cert, serial number:" + serialNumber);
return cert;
}
}
throw new ApplicationException("Cannot find cert with SN=" + serialNumber);
}
finally
{
store.Close();
}
}
}
}
评论: 0 | 引用: 0 | 查看次数: 78
发表评论
上一篇
下一篇

文章来自:
Tags: 




