'Web Service - Windows Authentication

I have added a Web Service to an existing asp.net intranet application. The purpose is to expose functionality to other intranet applications on the same domain.

The intranet application uses Windows authentication. How can I setup the web service to use Windows authentication?



Solution 1:[1]

Client.localhost.Service1 service = new Client.localhost.Service1();
   service.Credentials = new System.Net.NetworkCredential("username", "pass", "");

Solution 2:[2]

Setting up the web service to use Windows Authentication is easy. You just change the authentication mode in IIS!

Communicating with that service is another matter. First of all, you need to set up the service reference properly in the consuming application's web config. The security section below is the most critical part in getting this to work.

<system.serviceModel>
<bindings>
<basicHttpBinding>
    <binding name="ServiceSoap" closeTimeout="00:01:00"
      openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
      allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
      maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
      messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
      useDefaultWebProxy="true">
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
      <security mode="Transport">
        <transport clientCredentialType="Windows" proxyCredentialType="None" realm="" />
        <message clientCredentialType="UserName" algorithmSuite="Default" />
      </security>
    </binding>
  </basicHttpBinding>
</bindings>
<client>
  <endpoint address="http://yourservice.com/Service.asmx"
    binding="basicHttpBinding" bindingConfiguration="ServiceSoap"
    contract="ServiceClient.IServiceSoap" name="ServiceSoap" />
</client>

Then, you need to set the Windows credentials of the client object before you begin using it.

var credentials = ServiceSoapClient.ClientCredentials;
credentials.Windows.ClientCredential.Domain = "domain";
credentials.Windows.ClientCredential.UserName = "user";
credentials.Windows.ClientCredential.Password = "pwd";
credentials.Windows.AllowNtlm = true;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Royi Namir
Solution 2 SouthShoreAK