Search code examples
c#winformsweb-servicessoapservice-reference

Configuring winform app to call SOAP https with windows autentication


I have a SOAP that I'm trying to work with but I encounter the next problem that I didn't find the solution in similar questions:
The SOAP has to be called via HTTPS and with Windows credentials.

What I tried to do in the app config is the next things:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <system.serviceModel>
    <bindings>
      <basicHttpsBinding>
        <binding name="WebServiceSoap">
          <security mode="TransportWithMessageCredential">
            <transport clientCredentialType="Windows" />
            <message clientCredentialType="UserName"/>
          </security>
        </binding>
      </basicHttpsBinding>
    </bindings>
    <client>
      <endpoint address="https://someapp/SDK/WebService.asmx"
        binding="basicHttpsBinding" bindingConfiguration="WebServiceSoap"
        contract="someapp_contract.WebServiceSoap" name="WebServiceSoap" />
    </client>
  </system.serviceModel>
</configuration>

And the error that I get is:

The username is not provided. Specify username in ClientCredentials.And the error 

I tried a differt confingruation too with basicHttpBinding and

<security mode="TransportCredentialOnly">
            <transport clientCredentialType="Windows" />

But than the error that is I get is(which is a very logical error):

provided URI scheme 'https' is invalid; expected 'http'...

Did anyone have the same issue in the past?

Thanks in advance.
Max

P.S:
If I do

<security mode="Transport">

instead of

<security mode="TransportWithMessageCredential">

I get the next error:

The HTTP request is unauthorized with client authentication scheme 'Negotiate'. The authentication header received from the server was 'Negotiate,NTLM'.

Solution

  • Following the settings above.
    When I use:

    <security mode="Transport">
    

    and get the error:

    The HTTP request is unauthorized with client authentication scheme 'Negotiate'. The authentication header received from the server was 'Negotiate,NTLM'.
    

    What I did are the next changes.
    1) first at the app.config

    <security mode="Transport">
      <transport clientCredentialType="Windows" />
      <message clientCredentialType="UserName" algorithmSuite="Default" />
    </security>
    

    Than at the code:

    public static class Ssl
    {
        private static readonly string[] TrustedHosts = new[] {
          "YourServiceName", 
          "YourServiceName2"
        };
    
        public static void EnableTrustedHosts()
        {
            ServicePointManager.ServerCertificateValidationCallback =
            (sender, certificate, chain, errors) =>
            {
                if (errors == SslPolicyErrors.None)
                {
                    return true;
                }
    
                var request = sender as HttpWebRequest;
                if (request != null)
                {
                    return TrustedHosts.Contains(request.RequestUri.Host);
                }
    
                return false;
            };
        }
    }
    

    And:

    YourContractInstance.ClientCredentials.Windows.ClientCredential.UserName = ***;
    YourContractInstance.ClientCredentials.Windows.ClientCredential.Domain = ***;
    YourContractInstance.ClientCredentials.Windows.ClientCredential.Password = ***;
    YourContractInstance.ClientCredentials.Windows.AllowNtlm = true;
    

    All of this workaround was made because I have some problem with the certificate|DNS configuration on the host of the SOAP service that I'm not allowed to access so I had to add the Service reference by IP.