text: Q:
  
  How do I add windows authentification to my web service
  
  I have WCF Service, my goal is to be able to retrieve information from my server using GET and POST. I almost got it to work. My problem is that to be able to access the information on the server I need to enter my credentials, otherwise I get the error 401 unauthorized
  I tried a few different things but I'm still new at using XML and when I try to add some code I always get errors.
  This is my code:
  C#
  [ServiceContract]
      public interface IRestServiceImpl
      {
          [OperationContract]
          [WebInvoke(Method = "GET",
              ResponseFormat = WebMessageFormat.Xml,
              BodyStyle = WebMessageBodyStyle.Wrapped,
              UriTemplate = "xml/{id}")]
          string XMLData(string id);
  
          [OperationContract]
          [WebInvoke(Method = "GET",
              ResponseFormat = WebMessageFormat.Json,
              BodyStyle = WebMessageBodyStyle.Wrapped,
              UriTemplate = "json/{id}")]
          string JSONData(string id);
      }
  
  XML
  <?xml version="1.0"?>
  <configuration>
  
    <system.web>
      <compilation debug="true" targetFramework="4.0" />
    </system.web>
    <system.serviceModel>
      <services>
        <service name="RestService.RestServiceImpl" behaviorConfiguration="ServiceBehaviour">
          <!-- Service Endpoints -->
          <!-- Unless fully qualified, address is relative to base address supplied above -->
          <endpoint address ="" binding="webHttpBinding" contract="RestService.IRestServiceImpl" behaviorConfiguration="web">
            <!-- 
                Upon deployment, the following identity element should be removed or replaced to reflect the 
                identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
                automatically.
            -->
          </endpoint>
        </service>
      </services>
  
      <behaviors>
        <serviceBehaviors>
          <behavior name="ServiceBehaviour">
            <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
            <serviceMetadata httpGetEnabled="true"/>
            <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
            <serviceDebug includeExceptionDetailInFaults="false"/>
          </behavior>
        </serviceBehaviors>
        <endpointBehaviors>
          <behavior name="web">
            <webHttp/>
          </behavior>
        </endpointBehaviors>
      </behaviors>
      <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    </system.serviceModel>
    <system.webServer>
      <modules runAllManagedModulesForAllRequests="true"/>
    </system.webServer>
  
  </configuration>
  
  I still don't know how to fix this.
  
  A:
  
  Try this (link to code download below listing): 
  // IRestService.cs
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Runtime.Serialization;
  using System.ServiceModel;
  using System.ServiceModel.Web;
  using System.Text;
  
  namespace RestService
  {
      // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
      [ServiceContract]
      public interface IRestServiceImpl
      {
          [OperationContract]
          [WebInvoke(Method = "GET",
              ResponseFormat = WebMessageFormat.Xml,
              BodyStyle = WebMessageBodyStyle.Wrapped,
              UriTemplate = "xml/{id}")]
          string XMLData(string id);
  
          [OperationContract]
          [WebInvoke(Method = "GET",
              ResponseFormat = WebMessageFormat.Json,
              BodyStyle = WebMessageBodyStyle.Wrapped,
              UriTemplate = "json/{id}")]
          string JSONData(string id);
      }
  }
  
  // RestService.cs
  namespace RestService
  {
      public class RestServiceImpl : IRestServiceImpl
      {
          public string JSONData(string id)
          {
              return id;
          }
  
          public string XMLData(string id)
          {
              return id;
          }
      }
  }
  
  <!-- RestService.svc (Source) -->
  <%@ ServiceHost Language="C#" Debug="true" Service="RestService.RestServiceImpl" CodeBehind="RestService.svc.cs"
      Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
  
  <!--  Web.config -->
  <?xml version="1.0"?>
  <configuration>
    <system.web>
      <compilation debug="true" targetFramework="4.0" />
    </system.web>
    <system.serviceModel>
      <services>
        <service name="RestService.RestServiceImpl" behaviorConfiguration="ServiceBehaviour">
          <endpoint address="" binding="webHttpBinding"  bindingConfiguration="webBinding" contract="RestService.IRestServiceImpl" behaviorConfiguration="web">
          </endpoint>
        </service>
      </services>
      <bindings>
        <webHttpBinding>
          <binding name="webBinding">
            <security mode="TransportCredentialOnly">
              <transport clientCredentialType="Windows" proxyCredentialType="Windows">
              </transport>
            </security>
          </binding>
        </webHttpBinding>
      </bindings>
      <behaviors>
        <serviceBehaviors>
          <behavior name="ServiceBehaviour">
            <serviceMetadata httpGetEnabled="true"/>
            <serviceDebug includeExceptionDetailInFaults="false"/>
            <serviceCredentials>
              <windowsAuthentication allowAnonymousLogons="false" includeWindowsGroups="true" />
            </serviceCredentials>
          </behavior>
        </serviceBehaviors>
        <endpointBehaviors>
          <behavior name="web">
            <webHttp/>
          </behavior>
        </endpointBehaviors>
      </behaviors>
    </system.serviceModel>
    <system.webServer>
      <modules runAllManagedModulesForAllRequests="true"/>
    </system.webServer>
  </configuration>
  
  Results below (your base URL will likely differ). Ignore the "Endpoint not found" message you see. Rest services should not expose metadata (unless HATEOAS is considered metadata in your mind :)  
  http://localhost:62001/RestService.svc/xml/absee
  <XMLDataResponse xmlns="http://tempuri.org/">
  <XMLDataResult>absee</XMLDataResult>
  </XMLDataResponse>
  
  http://localhost:62001/RestService.svc/json/absee
  {"JSONDataResult":"absee"}
  
  Download the code here
  You may want to set it to run under IIS Express in the project settings under the Web tab. 
  
  