Я прочитал много статей и ответов, но я не смог его обработать.Как добавить заголовок SOAP Security
Я использую .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
Исключения говорят, что URL-адрес веб-службы должен быть HTTPS, но данный URL-адрес веб-службы - это HTTP, а не HTTPS.
Но я не возражал против HttpS и пошел на вторую попытку.
Попытка # 2
Как указано в this answer Я попытался, как показано ниже:
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
я получаю исключение, как показано ниже:
System.ServiceModel.Security .SecurityNegotiationException: не удалось установить доверительные отношения для SSL/TLS защищенного канала с полномочиями «https://www.GivenServiceUrl.com/WebService»
Поэтому я пошел на мою 3-й попытку, который я дал ссылку, как WebReference не так ServiceReference на моем проекте.
Попытка # 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 >
Вы не сможете сделать это с помощью Web Reference. Они не поддерживают WS-Security –