我试图通过其相应的wsdl使用Web服务.此服务依赖于符合Web服务安全基本安全配置文件1.0的身份验证,包括http://docs.oasis-open.org/wss/2004/01/oasis-200401wss-wssecurity-secext-1.0的正确xmls命名空间. xsd必须包含在请求中.
例:
<wsse:UsernameToken xmlns:wsse='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd' >
<wsse:Username>
Bob
</wsse:Username>
<wsse:Password Type='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText'>
1234
</wsse:Password>
</wsse:UsernameToken>
Run Code Online (Sandbox Code Playgroud)
我的第一次尝试是Add Service Reference针对wsdl以及使用它们生成的代理
ServicePointManager.ServerCertificateValidationCallback =
(object s, X509Certificate certificate, X509Chain chain,
SslPolicyErrors sslPolicyErrors) => true;
var basicHttpBinding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
basicHttpBinding.Security.Transport.ClientCredentialType =
HttpClientCredentialType.Basic;
var endpoint = new EndpointAddress("https://secure-ausomxana.crmondemand.com/..."
using (var client = new ContactClient(basicHttpBinding, endpoint))
{
var credential = client.ClientCredentials.UserName;
credential.UserName = "bob";
credential.Password = "1234";
var input = ...
var output = client.ContactQueryPage(input);
}
Run Code Online (Sandbox Code Playgroud)
但是,尝试使用Fiddler查询SOAP消息时,我发现没有添加任何UsernameToken元素.
履行这份合同的正确方法是什么?
编辑: …
我已经阅读了很多文章和答案,但我无法解决这个问题.
我在我的项目中使用.NET Framework 4.0.所以,我首先添加了WebService作为服务引用,并在我的app.config上获取绑定.而且我将列出我的尝试
尝试#1
我实例化了这样的服务并添加了凭据:
在App.Config上
<binding name="SubscriptionWSImplServiceSoapBinding" >
<security mode="TransportWithMessageCredential">
<transport clientCredentialType="Basic"/>
</security>
</binding>
SubscriptionWSImplServiceClient ClientWs = new SubscriptionWSImplServiceClient();
ClientWs.ClientCredentials.UserName.UserName = "some-username";
ClientWs.ClientCredentials.UserName.Password = "some-password";
var SomeResposne = ClientWs.RunMethod(); //I have exception at this point
Run Code Online (Sandbox Code Playgroud)
我得到一个例外如下:
System.ArgumentException: The provided URI scheme 'http' is invalid;
expected 'https'. Parameter name: via at System.ServiceModel.Channels
Run Code Online (Sandbox Code Playgroud)
例外情况说Web服务的url必须是HTTPS,但给定的Web服务URL是HTTP而不是HTTPS.
但我不介意HttpS并且进行了第二次尝试.
尝试#2
正如在这个答案中所说, 我尝试如下:
BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportWithMessageCredential);
binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
CustomBinding customBinding = new CustomBinding(binding);
SecurityBindingElement element = customBinding.Elements.Find<SecurityBindingElement>();
element.IncludeTimestamp = false; …Run Code Online (Sandbox Code Playgroud)