Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 同时使用证书和用户名/密码的WSDL绑定_C#_Web Services_Wsdl - Fatal编程技术网

C# 同时使用证书和用户名/密码的WSDL绑定

C# 同时使用证书和用户名/密码的WSDL绑定,c#,web-services,wsdl,C#,Web Services,Wsdl,我必须制作一个C dll,以便在某些应用程序中将Web服务用作com对象。服务器需要使用证书和用户名/密码进行身份验证 我尝试过很多解决方案,但都不奏效,所以我正在寻找解决方案 我最后一次尝试是这样的定制绑定: // Custom binding CustomBinding binding = new CustomBinding(); var userNameToken = new UserNameSecurityTokenParameters(); userNameToken.Inclusio

我必须制作一个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,该dll可用于使用WSDL的Web服务的VisualFoxPro客户端

如果我在浏览器中访问Web服务的URL,它会首先询问我从列表中选择的证书,然后输入用户/密码:在浏览器中,它工作正常

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


Thx

我不相信你可以有两种这样相互竞争的客户凭证

如果查看此配置,您将指定一种类型,而不是多种类型:

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

您可能希望研究的另一种路由是使用,它使用SSL或受信任证书来保护通道,使用用户名/密码来保护单个消息,但如果没有进一步的详细信息,则很难进一步提供建议

这是我刚才回答的一个可能/可能没有帮助的问题:


我尝试使用TransportWithMessageCredential,但它不起作用。无法为具有权限的SSL/TLS安全通道建立信任关系。我必须确定我可以在web浏览器中访问URL:它要求我选择证书,然后输入用户名和密码。。。你能解释一下我需要初始化哪些属性才能正确使用吗TransportWithMessageCredential@user3798271这可能有助于:
<security>
    <message clientCredentialType="UserName" />
</security>