我要在一些应用程序(作为com对象)中使用C#dll来使用webservices。服务器需要使用证书和用户名/密码进行身份验证。
我尝试了很多解决方案,但都没有,所以我正在寻找解决方案。
我的最后一次尝试是这样的自定义绑定:
// 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();
感谢您的帮助。 马特
编辑:
My C#项目包含从服务器上的WSDL文件生成的服务引用。 我编译它来制作一个可以在使用WSDL的WebServices的Visual FoxPro客户端中使用的DLL。
如果我在浏览器中访问WebServices的URL,它会首先询问我的证书(我从列表中选择),然后我要输入用户名/密码:在浏览器中它可以正常工作。 / p>
现在我要从我的DLL中调用这个Web服务,但我不知道如何定义绑定和端点以进行相同的身份验证过程。
THX
答案 0 :(得分:0)
我不相信你可以拥有两种类似ClientCredentials
的竞争类型。
如果您查看配置,您将指定一种类型,而不是多种类型:
<security>
<message clientCredentialType="Certificate" />
</security>
或者:
<security>
<message clientCredentialType="UserName" />
</security>
您可能想要研究的另一种方法是使用TransportWithMessageCredential
,它使用SSL或受信任的证书来保护频道,使用用户名/密码来保护单个邮件,但没有进一步的详细信息&#39很难进一步提出建议。
这是我刚回答的一个问题,可能/可能没有帮助:
WCF Client Using Certificate and Username/Password credentials?