带有证书和用户名/密码的WSDL绑定

本文关键字:密码 WSDL 绑定 证书 用户 | 更新日期: 2023-09-27 18:11:13

我必须制作一个c# dll来在某些应用程序中使用web服务(作为com对象)。服务器需要使用证书和用户名/密码进行身份验证。

我已经尝试了很多解决方案,但没有一个工作,所以我正在寻找一个解决方案。

我的最后一个尝试是这样的自定义绑定:

// Custom binding
CustomBinding binding = new CustomBinding();
var userNameToken = new UserNameSecurityTokenParameters();
userNameToken.InclusionMode = SecurityTokenInclusionMode.AlwaysToRecipient;
var securityElement = new AsymmetricSecurityBindingElement();
securityElement.IncludeTimestamp = true;
securityElement.RecipientTokenParameters = new X509SecurityTokenParameters(X509KeyIdentifierClauseType.SubjectKeyIdentifier, SecurityTokenInclusionMode.Never);
securityElement.InitiatorTokenParameters = new X509SecurityTokenParameters(X509KeyIdentifierClauseType.SubjectKeyIdentifier, SecurityTokenInclusionMode.AlwaysToRecipient);
securityElement.DefaultAlgorithmSuite = System.ServiceModel.Security.SecurityAlgorithmSuite.Basic256;
securityElement.SecurityHeaderLayout = SecurityHeaderLayout.Strict;
securityElement.SetKeyDerivation(false);
securityElement.EndpointSupportingTokenParameters.SignedEncrypted.Add(userNameToken);
securityElement.MessageProtectionOrder = System.ServiceModel.Security.MessageProtectionOrder.EncryptBeforeSign;
securityElement.MessageSecurityVersion = MessageSecurityVersion.WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11;
binding.Elements.Add(securityElement);
var encodingElement = new TextMessageEncodingBindingElement();
encodingElement.MessageVersion = MessageVersion.Soap12WSAddressingAugust2004;
binding.Elements.Add(encodingElement);
var httpElement = new HttpsTransportBindingElement();
httpElement.UseDefaultWebProxy = true;
binding.Elements.Add(httpElement); 
// Create the endpoint address. Note that the machine name 
EndpointAddress ea = new EndpointAddress("https://myURL/userservice");
// Create the client. 
UserServiceClient sNext = new UserServiceClient(binding, ea);
// Utilisation du WebService
sNext.ClientCredentials.UserName.UserName = "user";
sNext.ClientCredentials.UserName.Password = "pwd";
sNext.ClientCredentials.ClientCertificate.Certificate = autCertificat;
sNext.ClientCredentials.ServiceCertificate.DefaultCertificate = autCertificat;
sNext.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
sNext.MyService();

谢谢你的帮助。马特

编辑:

我的c#项目包含一个从服务器上的WSDL文件生成的服务引用。我把它编译成一个dll,可以在使用WSDL的WebServices的Visual FoxPro客户端中使用。

如果我在浏览器中转到WebServices的URL,它会首先询问我的证书(我从列表中选择),然后我必须输入用户/密码:在浏览器中它工作得很好。

现在我必须从我的DLL调用这个Webservices,但我不知道如何定义绑定和端点具有相同的身份验证过程。

Thx

带有证书和用户名/密码的WSDL绑定

我不相信你可以有两种竞争类型的ClientCredentials

如果你查看配置,你将指定一个类型,而不是多个:

<security>
    <message clientCredentialType="Certificate" />
</security>

或:

<security>
    <message clientCredentialType="UserName" />
</security>

您可能想要研究的另一种路由是使用TransportWithMessageCredential,它使用SSL或可信证书来保护通道,使用UserName/Password来保护单个消息,但是如果没有进一步的详细信息,就很难给出进一步的建议。

这是我之前回答过的一个问题,可能有帮助,也可能没有帮助:

WCF客户端使用证书和用户名/密码凭据?