发送数字证书
问题描述:
我为了使用makecert工具创建:发送数字证书
- 自签名证书
- 使用使用自签名证书
- 客户证书的服务器证书签名的自证书
然后,我在mmc的“可信证书颁发机构”部分中安装了自签名证书。
服务器证书和客户端证书安装在mmc的Personal部分。
然后,我使用服务器证书在IIS中将Web服务部署为HTTP。
然后我有另一个使用Web服务的应用程序。它发送Web服务请求的客户端证书,如下图所示:
public static void Main(string[] args)
{
X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection col = store.Certificates.Find(X509FindType.FindBySubjectName, "client.com", true);
if (col.Count == 1)
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls;
ClientServices web_service = new ClientServices();
web_service.ClientCertificates.Add(col[0]);
try
{
string check = web_service.CheckCertificate();
Console.WriteLine(check);
}
catch (WebException e)
{
Console.WriteLine(e.Message.ToString());
}
}
else
{
Console.WriteLine("The certificate was not found!");
}
Console.ReadKey();
}
在服务器端,我检查了类似这样的客户端证书:
[WebMethod]
public string CheckCertificate()
{
string message;
try
{
X509Certificate2 cert = new X509Certificate2(Context.Request.ClientCertificate.Certificate);
if (cert != null)
{
message = cert.SerialNumber.ToString();
}
else
{
message = "Error: No certificate was found!";
}
}
catch (Exception e)
{
message = e.Message.ToString();
}
return message;
}
每当我运行客户端应用程序,我收到以下错误信息:
请求被中止。无法创建SSL/TLS安全通道。
我该如何解决这个问题?
答
我发现了罪魁祸首。
我安装了WinHttpCertCfg工具并授予访问证书的私钥。
我使用的命令是这样的:
WinHttpCertCfg.exe -g -c LOCAL_MACHINE\MY -s "<name of certificate>" -a EVERYONE