【发布时间】:2023-04-06 06:07:02
【问题描述】:
我们有一个 Web 应用程序,它必须使用双向 SSL 证书身份验证与 HTTP 服务进行通信。以下是相关代码
X509Certificate certificate = GetCertFromStore(StoreName.My, StoreLocation.LocalMachine, certName);
if (certificate != null)
{
Log.Debug($"Adding client cert {certificate.Subject}");
WebRequestHandler handler = new WebRequestHandler();
handler.ClientCertificates.Add(certificate);
handler.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
handler.UseProxy = false;
using (var client = new HttpClient(handler))
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var postObj = new
{
// somestuff
};
var postBody = JsonConvert.SerializeObject(postObj);
var httpContent = new StringContent(postBody, Encoding.UTF8, "application/json");
HttpResponseMessage httpResponse = client.PostAsync(queryUrl, httpContent).Result;
if (httpResponse.IsSuccessStatusCode)
{
// codey code
}
else
{
Log.Error($"some error stuff.");
}
}
}
else
{
Log.Error($"Unable to find the certificate with name: {certName}");
}
目前我们正在使用我们提供给第三方服务提供商并从他们那里安装相关服务器证书的自签名证书。
在本地开发机器上一切正常 - 连接已建立,服务调用和响应正确完成。
但是,当我们在 Azure VM(我们的测试环境)上进行部署时,证书交换会失败。查看 Wireshark 跟踪,我们可以看到服务器证书被接受,但它显示发送了 0 个客户端证书。我们知道在存储中找到了客户端证书(由于日志记录),我们可以从 Wireshark 跟踪中看到客户端证书名称确实列在服务器证书请求的“可分辨名称”中。
无法弄清楚如何,即使代码将我们预期的客户端证书添加到 WebRequestHandler 它在握手期间消失了。
【问题讨论】:
-
该 Azure VM 个人存储中是否存在自签名证书?
-
嗨。是的,它已安装在 VM 个人商店中,我们的代码在 GetCertFromStore(...) 函数中加载它。
-
有没有具体的错误信息?
-
不,只显示发送了 0 个客户端证书。
标签: c# azure ssl virtual-machine