使用WCF消耗受基本身份验证和证书保护的SOAP服务

时间:2019-10-28 17:37:29

标签: c# .net wcf ws-security wsse

我正在尝试在Visual Studio 2019中构建客户端,该客户端将使用受ID /通过和证书保护的SOAP服务。

我通过导入WSDL创建了一个SoapUI项目,然后在该项目上将ws-security设置配置为具有传出签名,并导入了具有我要使用的证书的密钥库。最后,我在请求上设置auth以使用Basic,设置id / pass并在Outgoing WSS下拉列表中选择我的外发签名。完成所有这些操作后,我可以使用Soap UI完美地调用该服务。

在Visual Studio中,我创建了一个新的控制台项目,使用相同的WSDL添加了连接的服务引用,现在我正在尝试编写代码。我尝试通过设置设置凭据

System.ServiceModel.BasicHttpsBinding myBinding = new System.ServiceModel.BasicHttpsBinding();
System.ServiceModel.EndpointAddress myAddress = new System.ServiceModel.EndpointAddress("url");
ServiceReference1.CaseTypeClient myClient = new ServiceReference1.CaseTypeClient(myBinding, myAddress);
myClient.ClientCredentials.UserName.UserName = "ID";
myClient.ClientCredentials.UserName.Password = "Pass";
System.Security.Cryptography.X509Certificates.X509Certificate2 myCert = new System.Security.Cryptography.X509Certificates.X509Certificate2("CertFileName", "CertPass");
myClient.ClientCredentials.ClientCertificate.Certificate = myCert;

但是,当我调用该方法时,我从服务返回了一条皂漏消息,提示“ XmlException:限定名称'wsse:FailedAuthentication'中使用的未绑定前缀”。

做我想完成的事情的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

如果要基于客户端代理调用服务,则可以基于自动生成的绑定配置和身份验证模式在配置文件中提供相应的凭据。
我想知道什么是身份验证模式。设置证书后,为什么需要提供身份证/通行证?通信期间是否有Web代理?
此外,为了确保WCF服务可以访问该证书,我们通常将客户端证书安装在本地证书存储中,并添加Everyone帐户证书的私钥管理组,然后为该证书提供以下语句:

    ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
                client.ClientCredentials.Windows.ClientCredential.UserName = "administrator";
                client.ClientCredentials.Windows.ClientCredential.Password = "123456";
client.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindByThumbprint, "9b8db0dfe615458ace0ae9e89fcb983c5d16f633");

如果该项目基于基本身份验证,我们只需要使用Base64编码的用户名和密码来设置http标头。

  

授权:基本YWRtaW5pc3RyYXRvcjphYmNkMTIzNCE =

请随时告诉我是否有什么可以帮助的