jak dodać nagłówek SOAP Security

Przeczytałem wiele artykułów i odpowiedzi, ale nie mogłem tego rozwiązać.

Korzystam z .NET Framework 4.0 w moim projekcie. Po raz pierwszy dodałem WebService jako odniesienie do usługi i otrzymałem powiązania na moim app.config. I zamierzam wymienić moje próby

Próba # 1

Wykonałem instancję usługi w ten sposób i dodałem poświadczenia:

Na 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

Otrzymuję wyjątek jak poniżej:

 System.ArgumentException: The provided URI scheme 'http' is invalid;
 expected 'https'. Parameter name: via at System.ServiceModel.Channels

Wyjątki mówią, że adres URL usługi internetowej musi być HTTPS, ale podany adres URL usługi internetowej to HTTP, a nie HTTPS.

Ale nie miałem nic przeciwko HttpS i poszedłem na moją drugą próbę.

Próba # 2

Jak stwierdzono wta odpowiedź Próbowałem jak poniżej:

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

Otrzymuję wyjątek jak poniżej:

System.ServiceModel.Security.SecurityNegotiationException: nie można ustanowić relacji zaufania dla bezpiecznego kanału SSL / TLS z uprawnieniami ”https://www.GivenServiceUrl.com/WebService'

Dlatego poszedłem na moją trzecią próbę, którą podałem jako WebReference nie jako ServiceReference w moim projekcie.

Próba # 3

SubscriptionWSImplService ClientWs2 = new SubscriptionWSImplService();
var SomeResposne = ClientWs.RunMethod(); //I have exception at this point

Nie wiem, jak wysłać nagłówek zabezpieczeń w tym momencie.

Otrzymuję wyjątek jak poniżej:

 System.Web.Services.Protocols.SoapHeaderException: An error was
 discovered processing the <wsse: Security> header

Ale tutaj nie mogłem dodać potrzebnego nagłówka. Czy mógłbyś mi z tym pomóc?

Próba 1 lub Próba 2 lub Próba 3, który z nich powinien być użyty również zważywszy, że używam Framework 4.0? A jak mogę rozwiązać problem?

Zgodnie z dokumentacją ostatecznego żądania SOAP musi wyglądać tak:

<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 >

questionAnswers(2)

yourAnswerToTheQuestion