【发布时间】:2014-07-10 17:31:26
【问题描述】:
我必须制作一个 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,但我不知道如何定义绑定和端点以具有相同的身份验证过程。
谢谢
【问题讨论】:
标签: c# web-services wsdl