我已经阅读了很多文章和答案,但我仍然无法解决问题。
我的项目使用.NET Framework 4.0。因此,我首先将WebService添加为服务引用,并在app.config中获取绑定信息。下面是我的尝试列表。
异常提示表示Web服务的URL必须是HTTPS,但是提供给我的Web服务URL是HTTP而不是HTTPS。但我并没有关注HttpS,而是尝试了第二次。
我遇到了以下异常:
我不知道如何在此时发送安全头信息。
我会得到以下异常:
"
我的项目使用.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
我得到了以下异常:
System.ArgumentException: The provided URI scheme 'http' is invalid;
expected 'https'. Parameter name: via at System.ServiceModel.Channels
异常提示表示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;
EndpointAddress address = new EndpointAddress("https://www.GivenServiceUrl.com/WebService"); //Note that the url does not end as ..svc or ..asmx! Just the name like this with no dot or extension
SubscriptionWSImplServiceClient ClientWs = new SubscriptionWSImplService(customBinding, address);
ClientWs.ClientCredentials.UserName.UserName = "some-username";
ClientWs.ClientCredentials.UserName.Password = "some-password";
var SomeResposne = ClientWs.RunMethod(); //I have exception at this point
我遇到了以下异常:
因此,我进行了第三次尝试,将项目中的引用设置为WebReference而不是ServiceReference。System.ServiceModel.Security.SecurityNegotiationException: 无法与权限为'https://www.GivenServiceUrl.com/WebService'的SSL/TLS安全通道建立信任关系
尝试 #3
SubscriptionWSImplService ClientWs2 = new SubscriptionWSImplService();
var SomeResposne = ClientWs.RunMethod(); //I have exception at this point
我不知道如何在此时发送安全头信息。
我会得到以下异常:
System.Web.Services.Protocols.SoapHeaderException: An error was
discovered processing the <wsse: Security> header
但是在这里,我无法添加必要的头信息。你能帮我吗?
"
尝试1或尝试2或尝试3,考虑到我使用Framework 4.0,应该选择哪一个?如何解决这个问题?
根据文档,最终请求的SOAP必须看起来像这样:
"<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sub="http://subscription.services.ws.fourplay.com.tr/">
<soapenv:Header>
<wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken wsu:Id="UsernameToken-12" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsse:Username>admin</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">123456</wsse:Password>
<wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">1lcj+WbCMlrPyhcud4QxiQ==</wsse:Nonce>
<wsu:Created>2012-06-05T10:23:26.879Z</wsu:Created>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
<soapenv:Body>
<sub:RunMethod>
<!--Optional:-->
<sub:opaque id>555555555</sub:opaque id>
<sub:params>
<!--Optional:-->
<name>GUID</name>
<!--Optional:-->
<value>2fc4ce1d-645e-41f4-811e-28510a02a17f </value>
</sub:params>
</sub: RunMethod >