具有传输和消息安全性的 WCF 绑定(bind)

标签 wcf soap wcf-security basic-authentication ws-security

我在一个大型项目中工作,该项目广泛使用 WCF 进行不同类型的通信。作为新要求的一部分,我们需要与第三方开发的 SOAP Web 服务进行通信。他们的服务是用 Java 开发的,有两个安全要求:

  1. 它需要基本身份验证传输和

  2. 消息必须使用 WS-Security (OASIS) 不可否认标准使用 X509 证书签名(未加密)

我遇到的问题是 WCF 提供的绑定(bind)允许使用传输或消息安全性,但不能同时使用这两者。因此,我可以签署 SOAP 消息或发送 BASIC 身份验证凭据,但到目前为止我还不能按要求执行这两项操作。

几天来我一直在努力寻找解决方案,到目前为止我得出的结论是,我最好的办法可能是以编程方式进行自定义绑定(bind)。使用多个来源,这就是我现在所拥有的:

var b = new CustomBinding();

var sec = (AsymmetricSecurityBindingElement)SecurityBindingElement.CreateMutualCertificateBindingElement(MessageSecurityVersion.WSSecurity10WSTrust13WSSecureConversation13WSSecurityPolicy12BasicSecurityProfile10);

UserNameSecurityTokenParameters tokenParamenters = new UserNameSecurityTokenParameters();
tokenParamenters.InclusionMode = SecurityTokenInclusionMode.AlwaysToRecipient;
tokenParamenters.RequireDerivedKeys = false;

b.Elements.Add(sec);
b.Elements.Add(new TextMessageEncodingBindingElement(MessageVersion.Soap11, Encoding.UTF8));
b.Elements.Add(new HttpsTransportBindingElement());

WSClient svc = new WSClient(b, new EndpointAddress(new Uri("https://serviceurl.com/SoapWS"), new DnsEndpointIdentity("CertificateIdentity"), new AddressHeaderCollection()));
svc.ClientCredentials.UserName.UserName = "username";
svc.ClientCredentials.UserName.Password = "password";

svc.ClientCredentials.ClientCertificate.Certificate = new X509Certificate2("Certificate.p12", "password");
svc.ClientCredentials.ServiceCertificate.DefaultCertificate = new X509Certificate2("Certificate.p12", "password");
svc.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;

string resp = svc.DoSomething();

虽然这似乎是使用证书对消息进行签名(无法完全检查),但基本的身份验证部分并未发生。服务器的响应是:

The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Basic realm="realm"'.

任何有关我如何获得此绑定(bind)以使用 BASIC 身份验证通过传输进行身份验证的见解都将非常有帮助。提前致谢。

PS:请注意,我无法控制我尝试与之通信的 Web 服务。

最佳答案

配置https传输 channel :

HttpsTransportBindingElement h = new HttpTransportBindingElement();
h.AuthenticationScheme = AuthenticationSchemes.Digest;
h.realm = ...

如果您遇到麻烦,我建议您先设置一个基本的 workign 传输基本 http 绑定(bind),这样您就可以看到您通过了服务器 https 传输(您应该会遇到 soap 错误 b/c 缺少签名)。

关于具有传输和消息安全性的 WCF 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18920097/

相关文章:

.net - 如何使用URL查询处理WCF-OData中的延续?

c# - 在 WCF 契约(Contract)中标记弃用的字段

python - 将列表与元组与列表进行比较并返回唯一的元组

c# - 仅使用 SSL 进行身份验证

.net - 不使用 SSL 的 WCF 安全性

c# - 调用异步 Web 方法完成后如何导航?

.net - 微软 Jabber 的替代品?

PHP SOAP 头构造

delphi - SOAP 客户端中的 Array_Of_Int

wcf - WCF Web 服务是否适合非 Microsoft SOAP 客户端?