Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法为WCF SOAP请求设置ClientCredentials_Wcf_Soap - Fatal编程技术网

无法为WCF SOAP请求设置ClientCredentials

无法为WCF SOAP请求设置ClientCredentials,wcf,soap,Wcf,Soap,我正在使用Visual Studio 2019尝试创建一个.Net 4.5.2客户端,该客户端使用HTTP上的SOAP使用远程Web服务。要进行身份验证,服务要求将客户端证书附加到所有请求。客户端实例化System.ServiceModel.ClientBase类。似乎无论我如何生成客户机类,我都无法设置ClientCredentials,因为ClientCredentials是只读的 以下是我用来生成客户端类的命令: svcutil.exe /t:metadata https://exampl

我正在使用Visual Studio 2019尝试创建一个.Net 4.5.2客户端,该客户端使用HTTP上的SOAP使用远程Web服务。要进行身份验证,服务要求将客户端证书附加到所有请求。客户端实例化System.ServiceModel.ClientBase类。似乎无论我如何生成客户机类,我都无法设置ClientCredentials,因为ClientCredentials是只读的

以下是我用来生成客户端类的命令:

svcutil.exe /t:metadata https://example.com?WSDL
svcutil.exe /language:cs /config:app.config /messagecontract *.xsd *.wsdl
以下是我在web.config文件中使用的绑定:

<bindings>
  <basicHttpBinding>
    <binding name="TCSOnlineServicePortBinding">
      <!-- I am not sure of the mode below, message also does not work -->
      <security mode="TransportWithMessageCredential">
        <transport clientCredentialType="Certificate" proxyCredentialType="None" />
        <message clientCredentialType="Certificate" algorithmSuite="Default" />
      </security>
    </binding>
  </basicHttpBinding>
</bindings>
<client>
  <endpoint address="https://example.com"
      binding="basicHttpBinding" bindingConfiguration="TCSOnlineServicePortBinding"
      contract="TCSOnlineService" name="TCSOnlineServicePort" />
</client>

有人能看出我做错了什么吗?提前感谢。

我们最好使用
LocalMachine store
配置证书,然后将运行客户端应用程序的当前用户添加到证书私钥的管理组中

ServiceReference1.TestServiceClient client = new ServiceReference1.TestServiceClient();
            client.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindByThumbprint, "9ee8be61d875bd6e1108c98b590386d0a489a9ca");
            var result = client.GetResult("a");
            result = client.Test();
            Console.WriteLine(result);

对于使用Powershell生成的客户端证书,我们最好针对应用程序DotNet4.6.2或更高版本。
以这种方式提供客户端证书存在问题。我们需要确保当前用户可以读取证书的私钥

  TCSSvcClient.ClientCredentials.ClientCertificate.Certificate = new X509Certificate2();
此外,使用证书验证客户端需要客户端和服务器端之间的信任关系。您在呼叫服务之前是否建立了信任关系?


如果有什么我可以帮忙的,请随时告诉我。

我不是.NET专家,但仅提供证书是不够的。对于客户端身份验证,您的代码也必须具有访问私钥的权限。使用证书身份验证传输安全性的链接似乎正是我所寻找的。谢谢
  TCSSvcClient.ClientCredentials.ClientCertificate.Certificate = new X509Certificate2();