如何调用需要C#中的证书的Web服务?

如何调用需要C#中的证书的Web服务?,c#,visual-studio,web-services,certificate,C#,Visual Studio,Web Services,Certificate,我需要与具有.asmxweb服务的第三方通信。此web服务正在使用https。我有所需的证书(.pfx) 在Visual Studio中首次尝试使用add service Reference添加此服务时,我遇到了一个错误。我通过将证书导入个人存储区而通过了此错误。在我这样做之后,我再次尝试添加服务引用,结果成功了。现在我可以创建web服务的一个实例。很好 但是现在我想调用服务。当我这样做时,我会得到这个错误: 302通知:数字证书丢失 那么,如何告诉我的服务使用正确的证书呢?在获取请求流之前,尝

我需要与具有
.asmx
web服务的第三方通信。此web服务正在使用https。我有所需的证书(.pfx)

在Visual Studio中首次尝试使用
add service Reference
添加此服务时,我遇到了一个错误。我通过将证书导入
个人
存储区而通过了此错误。在我这样做之后,我再次尝试添加服务引用,结果成功了。现在我可以创建web服务的一个实例。很好

但是现在我想调用服务。当我这样做时,我会得到这个错误:

302通知:数字证书丢失
那么,如何告诉我的服务使用正确的证书呢?

在获取请求流之前,尝试添加以下内容:

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");

根据您的安全要求和环境,您可能需要使用不同的SecurityRoroColType值。

我最终成功地解决了我的问题,如下所示:

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;
在请求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;
        }

感谢您的回答,但我的服务上没有属性
ClientCertificates
var service=new Service1.Service1SoapClient()
在SOAP服务调用的情况下,需要将其添加到生成的代理类Reference.cs中。唯一的问题是,一旦修改了这个类,更新web引用将重新编写生成的代理,并且需要重新应用手动修改。这对我来说是新的。我找到了
Reference.cs
文件,但我需要在该文件的何处使用此代码?该文件相当大(370行代码),需要使用您调用的方法向服务发送请求。在该方法中,没有属性为
ClientCertificates
或“ProtocolVersion”的变量
SsoPocV2.RaetService.GetXSSORequest inValue=new SsoPocV2.RaetService.GetXSSORequest();inValue.Body=new SsoPocV2.RaetService.GetXSSORequestBody();inValue.Body.xmlstr=xmlstr;SsoPocV2.RaetService.GetXSSOResponse retVal=((SsoPocV2.RaetService.Service1Soap)(this)).GetXSSO(无效);返回retVal.Body.getxsoresult如果您需要添加的引用有任何问题。使用System.Security.Cryptography.X509证书