我正在尝试使用 WS 安全性连接到 WS。
我确实用 SOAP UI 进行了测试:
效果很好。
我确实用 C# 代码尝试了很多方法,但总是出现异常: 例如这个方法:
using (var client = new DocumentWSWs.DocumentUploadWSClient())
{
DocumentWSWs.StatusRequest currentStatusRequest = new DocumentWSWs.StatusRequest();
client.ClientCredentials.UserName.UserName = UserNameWs;
client.ClientCredentials.UserName.Password = PasswordWs;
SetTlsSecurite();
DocumentWSWs.StatusResponse getCurrentStatusResponse = client.GetStatus(currentStatusRequest);
}
与app.config:
<endpoint address="https://*************"
binding="basicHttpBinding" bindingConfiguration="DocumentUploadWSSoap11"
contract="DocumentWSWs.DocumentUploadWS" name="DocumentEndPoint" >
<headers>
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken Id="UsernameToken-49">
<wsse:Username>*******</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">***************</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</headers>
</endpoint>
<binding name="DocumentUploadWSSoap11">
<security mode="Transport">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
但是我得到:HTTP 请求未通过“匿名”客户端身份验证方案授权
我确实尝试过安全模式=“TransportWithMessageCredential”
但是我收到了这个错误:
在客户端配置服务 ServiceModel 中找不到引用合约“DocumentUploadWSSoap11.DocumentUploadWS”的默认端点元素。可能没有找到应用程序的配置文件,或者在客户端元素中找不到与合约对应的端点元素。
尽管检查了很多博客和论坛,我还是发现了错误。或者博客不清楚如何使用给定的代码。
有人可以给我正确的方向或调用此 WS 的方式吗
谢谢你。
凭证已正确获取
以编程方式配置端点和绑定(如果您不想使用 app.config 中的配置)。您可以像这样创建自定义绑定和端点:
BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportWithMessageCredential);
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
EndpointAddress endpointAddress = new EndpointAddress("https://*************");
using (var client = new DocumentWSWs.DocumentUploadWSClient(binding, endpointAddress))
{
client.ClientCredentials.UserName.UserName = UserNameWs;
client.ClientCredentials.UserName.Password = PasswordWs;
// Now, you can call the service methods.
}
现在拨打服务电话:
DocumentWSWs.StatusRequest currentStatusRequest = new DocumentWSWs.StatusRequest();
DocumentWSWs.StatusResponse getCurrentStatusResponse = client.GetStatus(currentStatusRequest);