Search code examples
c#wcf

set WCF security to windows authentication


I've created an empty web application there I've added WCF Service. Using the following tutorial

https://www.guru99.com/restful-web-services.html

Here I get the application with WCF in it. Then I created my WCF and it is accessible from browser and it is returning data in json format. Which I really wanted to have. However I don't want it to be public I want basic authentication in it. Inside my web.conf I've the following

<endpointBehaviors>
        <behavior name="MyProject">
          <webHttp />
        </behavior>
      </endpointBehaviors>

I tried to replace it with

<webHttpBinding>
        <binding name="IncreasedTimeout">
          <security mode="TransportCredentialOnly">
            <transport clientCredentialType="Windows" />
          </security>
        </binding>
      </webHttpBinding>

but it didn't work for me . I just want clientCredentialType="Windows" somewhere in the Web.Config


Solution

  • Then I created my WCF and it is accessible from browser and it is returning data in json format

    This kind of WCF service should be created by WebHttpbinding. Therefore, we should apply basic authentication to this type of binding. Please refer to the below configuration.

       <system.serviceModel>
        <services>
          <service name="WcfService1.Service1">
            <endpoint address="" binding="webHttpBinding" contract="WcfService1.IService1" bindingConfiguration="mybinding"></endpoint>
          </service>
        </services>
        <bindings>
          <webHttpBinding>
            <binding name="mybinding">
              <security mode="Transport">
                <transport clientCredentialType="Basic"></transport>
              </security>
            </binding>
          </webHttpBinding>
        </bindings>
        <behaviors>
          <serviceBehaviors>
            <behavior>
              <serviceMetadata httpsGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
          </serviceBehaviors>
          <endpointBehaviors>
            <behavior>
              <webHttp />
            </behavior>
          </endpointBehaviors>
        </behaviors>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
      </system.serviceModel>
    

    One more thing we should pay attention to is that we should disable windows authentication in the IIS authentication module when hosting the WCF service with Basic authentication.
    enter image description here
    Feel free to let me know if the problem still exists.