使用 Soap 请求发送客户端证书和授权 header 的理想方式是什么

What is the ideal way to send a client certificate and an Authorization header with Soap request

我有一个调用 Web 服务的 ASP.Net Web API 应用程序。现在(开发环境)Web 服务 (SOAP) 和我的 ASP.net 应用程序位于内部网络中。在生产中,Web api 应用程序将位于 public 互联网中,而 SOAP 服务将位于内部网络中。

在生产中,我们使用 CA SOA Gateway 验证每个服务调用并将请求重定向到内部 Web 服务。网关应用程序在 HTTPS 中,内部 Web 服务在 HTTP 中。

CA SOA 网关需要 2 因素身份验证,

我已按照 this article 在每个服务请求中包含授权 HTTP header。它工作正常。

我正在使用 wcf 客户端配置(在 web.config 中)在请求中包含客户端证书 (< security mode=transport />)。

我想从物理位置而不是证书存储添加客户端证书。我按照 this article 从文件附加客户端证书,但它对我不起作用,因为我正在使用 .Net 4.6.1 并且我没有 System.ServiceModel 命名空间来使用 DevAge.ServiceModel 代码(来自给定的示例代码)。

问题:

注意:授权 header 和客户端证书仅用于 SOA 网关的验证,它们不是 Web 服务所必需的。

感谢您的帮助。

关于Headers,也可以实现IClientMessageInspector接口,为每次调用添加一个header。 像这样:

public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request,  System.ServiceModel.IClientChannel channel)
{
   request.Properties.Add(HttpRequestMessageProperty.Name, new HttpRequestMessageProperty { .... });

}

这里有一个类似的问题和很好的例子:

这是我用过的另一个很好的例子:

有关 IClientMessageInspector 的更多信息:MSDN

关于证书,您可以在实例化客户端端点时从路径添加证书,如下所示:

var endpointIdentity = EndpointIdentity.CreateX509CertificateIdentity(new X509Certificate2(@"c:\cert\mycertificate.pfx", "password"));
var endpoint = new EndpointAddress(new Uri("http://dns/service.svc"), endpointIdentity);

或者添加到客户端,像这样:

var client = new YourService("BasicHttpBinding_IServiceInterface");
            client.ClientCredentials.ClientCertificate.Certificate = new X509Certificate2(@"c:\certifcate.pfx", "password", X509KeyStorageFlags.DefaultKeySet);

希望对您有所帮助。