我需要与拥有.asmx
网络服务的第三方进行通信。此Web服务使用https。我有所需的证书(.pfx)。
首次尝试在Visual Studio中使用Add Service Reference
添加此服务时,出现错误。我通过将证书导入Personal
商店来传递此错误。我这样做后,我尝试再次添加服务参考,它的工作原理。所以现在我可以创建一个Web服务实例。好的。
但现在我想调用该服务。当我这样做时,我得到了这个错误:
302通知:缺少数字证书
那么如何告诉我的服务使用正确的证书呢?
答案 0 :(得分:2)
尝试在获取请求流之前添加此内容:
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
request.ProtocolVersion = HttpVersion.Version10;
request.ClientCertificates.Add(new X509Certificate2("YourPfxFile(full path).pfx", "password for your pfx file");
根据您的安全要求和环境,您可能需要使用不同的SecurityProrocolType值。
答案 1 :(得分:2)
我终于设法解决了我的问题如下:
var service = new Service1SoapClient();
service.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.CurrentUser, StoreName.TrustedPublisher, X509FindType.FindByIssuerName, "name_of_issuer");
((BasicHttpBinding)service.Endpoint.Binding).Security.Mode = BasicHttpSecurityMode.Transport;
((BasicHttpBinding)service.Endpoint.Binding).Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
答案 2 :(得分:0)
在请求Web服务时,我还面临着“请求被中止:无法创建SSL / TLS安全通道”。 修复了以下代码的问题,希望对您有所帮助并节省时间。
HttpWebRequest web_request = (HttpWebRequest)WebRequest.Create("https://yourservices/test.asmx");
web_request.ContentType = "text/xml;charset=\"utf-8\"";
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
web_request.ProtocolVersion = HttpVersion.Version10;
X509Certificate2Collection certificates = new X509Certificate2Collection();
certificates.Import(certName, password, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet);
web_request.ClientCertificates = certificates;
web_request.Accept = "text/xml";
web_request.Method = "POST";
using (Stream stm = web_request.GetRequestStream())
{
using (StreamWriter stmw = new StreamWriter(stm))
{
stmw.Write(soap);
}
}
WebResponse web_response = null;
StreamReader reader = null;
try
{
web_response = web_request.GetResponse();
Stream responseStream = web_response.GetResponseStream();
XmlDocument xml = new XmlDocument();
reader = new StreamReader(responseStream);
xml.LoadXml(reader.ReadToEnd());
}
catch (Exception webExp) {
string exMessage = webExp.Message;
}