【问题标题】:WSDL binding with both certificate and username/passwordWSDL 与证书和用户名/密码的绑定
【发布时间】: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


    【解决方案1】:

    我不相信您可以像这样拥有 2 种竞争类型的 ClientCredentials

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

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

    或者:

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

    您可能想要研究的另一种方法是使用TransportWithMessageCredential,它使用 SSL 或受信任的证书来保护通道和用户名/密码来保护各个消息,但如果没有更多详细信息,很难进一步建议.

    这是我不久前回答的一个问题,可能/可能没有帮助:

    WCF Client Using Certificate and Username/Password credentials?

    【讨论】:

    • 我尝试使用 TransportWithMessageCredential 但它不起作用“无法为具有权限的 SSL/TLS 安全通道建立信任关系”。
    • 我必须准确地说我可以在网络浏览器中访问 URL:它要求我选择证书,然后输入用户名和密码...你能解释一下我要初始化的属性吗正确使用 TransportWithMessageCredential
    • @user3798271 这可能会有所帮助:stackoverflow.com/questions/1742938/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多