简体   繁体   English

如何为需要客户端身份验证证书的 Web 服务添加对 WCF 客户端的服务引用

[英]How to add a service reference to a WCF client for a web service requiring client authentication certificate

Caution, WCF noobie alert注意,WCF noobie 警报

I need to create a WCF client to query a non-WCF web service.我需要创建一个 WCF 客户端来查询非 WCF Web 服务。 The web service is not a WCF service. Web 服务不是 WCF 服务。 Additionally, the web service requires a client authentication certificate.此外,Web 服务需要客户端身份验证证书。 Now, I have the certificate, and can create a non-WCF client that works perfectly;现在,我有了证书,可以创建一个完美运行的非 WCF 客户端; I was able to 'Add Web Reference' and a certificate dialog box opened up to allow me to select the appropriate certificate, then went on to create the web reference.我能够“添加 Web 引用”并打开一个证书对话框以允许我选择适当的证书,然后继续创建 Web 引用。 Trying to create a WCF client via 'Add Service Reference' is another story, it just fails with a 403 Access Denied error.尝试通过“添加服务引用”创建 WCF 客户端是另一回事,它只是因 403 拒绝访问错误而失败。

I have the WSDL for the service, and have run svcutil.exe on it, but am not sure how to proceed from there.我有该服务的 WSDL,并已在其上运行 svcutil.exe,但我不确定如何从那里继续。

Thanks for any help!谢谢你的帮助!

I know this is the old question and it has been already solved but I would like to mention that Add service reference also works for WSDL files stored on disk.我知道这是一个老问题,它已经解决了,但我想提一下,添加服务引用也适用于存储在磁盘上的 WSDL 文件。 Marc has also mentioned it.马克也提到过。 Add service reference dialog accepts:添加服务引用对话框接受:

  • URL to WSDL WSDL 的 URL
  • URL to Metadata exchange endpoint元数据交换端点的 URL
  • Service URL where /mex is added internally内部添加 /mex 的服务 URL
  • Any file path to WSDL file WSDL 文件的任何文件路径

So if you have WSDL and all need XSD files you can use Add service reference as well.因此,如果您有 WSDL 并且都需要 XSD 文件,您也可以使用添加服务引用。 The only tricky part is that Add service reference dialog doesn't have Browse button and that is the reason why this functionality is not well known.唯一棘手的部分是“添加服务引用”对话框没有“浏览”按钮,这就是此功能不为人所知的原因。

I'm assuming that the service you are using is performing client SSL authentication.我假设您使用的服务正在执行客户端 SSL 身份验证。

Since add service reference is failing, you can use svcutil to generate the client from the WSDL file that you have.由于添加服务引用失败,您可以使用svcutil 您拥有的 WSDL 文件生成客户端 I think the syntax would be something like:我认为语法将是这样的:

svcutil *.wsdl /l:C# /out:Reference.cs /config /s /ct:System.Collections.Generic.List`1 /ser:Auto /tcv:Version35 /n:*,<NameOfYourNamespaceHere> /edb

This will generate a file, Reference.cs, that contains the proxy classes to the service (you can give this file whatever name you want).这将生成一个文件 Reference.cs,其中包含服务的代理类(您可以为该文件指定任何您想要的名称)。 Add this file to your project.将此文件添加到您的项目中。 A config file, output.config, will also be generated.还将生成一个配置文件 output.config。 You can add this configuration to your application configuration instead of typing it all in by hand.您可以将此配置添加到您的应用程序配置中,而不是手动输入所有内容。

Now you can follow this MSDN article on using Transport Security with Certificate Authentication .现在,您可以按照这篇 MSDN 文章使用证书身份验证使用传输安全 You can skip down to the client section where it shows how to attach the certificate to the request in code as well as in configuration.您可以跳到客户端部分,其中显示了如何在代码和配置中将证书附加到请求。

Good luck.祝你好运。

Stupid question (maybe): could you connect to the service endpoint, present it with your credentials stored in the certificate, and then download the WSDL (and possibly XSD) from there?愚蠢的问题(也许):您能否连接到服务端点,向它提供存储在证书中的凭据,然后从那里下载 WSDL(可能还有 XSD)? Or could it be the entity offering this service would be able to actually send you these files (or make them available for download)?或者提供此服务的实体是否能够实际向您发送这些文件(或使其可供下载)?

Once you have the WSDL (and XSD) file on disk, it should be easy enough to create WCF client for that (using either svcutil.exe or Add Service Reference ) based on those files, and then configure the appropriate security for it.一旦您在磁盘上拥有 WSDL(和 XSD)文件,就应该很容易基于这些文件为其创建 WCF 客户端(使用svcutil.exeAdd Service Reference ),然后为其配置适当的安全性。

Just a thought.... (worth $0.02?)只是一个想法......(价值 0.02 美元?)

Marc马克

OK, bit of a work-around here (and I've no idea what is going on technically): I noticed that when you add a Web Reference, the certificate you have chosen is cached and automatically used the next time you add the Web Reference (I noticed because I'd chosen the wrong certificate).好的,这里有一些解决方法(我不知道技术上发生了什么):我注意到当您添加 Web 引用时,您选择的证书会被缓存并在您下次添加 Web 时自动使用参考(我注意到是因为我选择了错误的证书)。 This caching seems to work across Web Reference and Service Reference so:这种缓存似乎适用于 Web 引用和服务引用,因此:

  • Add a Web Reference to the endpoint, choosing the certificate you wish to use向端点添加 Web 引用,选择您希望使用的证书
  • Remove this Web Reference删除此 Web 参考
  • Add a Service Reference to the same endpoint and Visual Studio will use the same certificate you chose for the Web Reference将服务引用添加到同一终结点,Visual Studio 将使用您为 Web 引用选择的相同证书

Worked on Visual Studio Community 2019, v16.7.7在 Visual Studio Community 2019 v16.7.7 上工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用客户端证书将 WCF 服务引用添加到 Visual Studio 2022 项目 - Add WCF Service Reference to Visual Studio 2022 Project with Client Certificate Tomcat应用程序到具有客户端证书身份验证的WCF服务 - Tomcat Application to WCF Service with Client Certificate Authentication 以编程方式配置WCF服务客户端和证书身份验证 - Configure WCF service client with certificate authentication programmatically 自托管WCF服务+客户端证书身份验证 - selfhosted WCF-Service + client certificate authentication 具有证书身份验证的WCF服务参考 - WCF service reference with certificate authentication 无法将Java服务引用添加到WCF客户端 - Unable to add Java service reference to WCF client 如何在没有svcutil的情况下创建客户端代理或在wcf中添加服务引用? - How to create client proxy without svcutil or add service reference in wcf? WCF 通过证书识别客户端的服务 - WCF Service that identifies client by certificate 如何将WCF服务参考客户端与X.509客户端证书(仅作为.pfx文件一起使用)一起使用? - How to use WCF service reference client with X.509 client certificate, which is only available as .pfx file? WCF 下的 SOAP web 服务的同时客户端证书和用户名身份验证 - Simultaneous client-certificate and username authentication of a SOAP web service under WCF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM