Search code examples
wcfwindows-servicesbasic-authentication

Add basic authentication to WCF Service hosted in a Windows service


How can I add basic authentication to a WCF service hosted in a Windows service?

I added a security tag to my binding, but I don't get an authentication window when I call the service url in the browser. What am I doing wrong/what am I missing?

<bindings>
  <basicHttpBinding>
    <binding name="MyDefaultBinding" allowCookies="true" maxReceivedMessageSize="20000000" maxBufferSize="20000000">
      <readerQuotas maxDepth="500" maxArrayLength="20000000" maxStringContentLength="20000000"/>  
    <security mode="TransportCredentialOnly">
       <transport clientCredentialType="Basic" />
   </security>
    </binding>
  </basicHttpBinding>
</bindings>

Solution

  • You will not get that authentication window when you just access the service helper page. The authentication is configured for service endpoint - not for helper page or WSDL (those are "separate endpoints").

    Try to modify your configuration:

    <bindings>
      <customBinding>
        <binding name="securedPages">
          <textMessageEncoding messageVersion="None" />
          <httpsTransport authenticationScheme="Basic" />
        </binding>
      <customBinding>
      <basicHttpBinding>
        <binding name="MyDefaultBinding" allowCookies="true" maxReceivedMessageSize="20000000" maxBufferSize="20000000">
          <readerQuotas maxDepth="500" maxArrayLength="20000000" maxStringContentLength="20000000"/>  
          <security mode="TransportCredentialOnly">
            <transport clientCredentialType="Basic" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="securedService">
          <serviceMetadata httpGetEnabled="true" httpGetBinding="customBinding" 
                           httpGetBindingConfiguration="securedPages" />
          <serviceDebug httpHelpPageEnabled="true" httpHelpPageBinding="customBinding" 
                        httpHelpPageBindingConfiguration="securedPages" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="..." behaviorConfiguration="securedService">
        ...
      </service>
    </services>