Tuesday, November 19, 2013

How to host a WCF in IIS and enable Windows Authentication

I came across a scenario where I wanted to deploy a web service in windows server. Since I am mainly familiar with Linux environment and Java technology it took a while for me to get it done. So here I am sharing the knowledge gathered by me for those who wants to achieve the same task with less time.

WCF is a runtime set of APIs in .NET framework to create Web services.
IIS is a Microsoft Web server.


Environment:

JDK 1.6
.NET 4.0
IIS 7.5
Windows 7 professional

When you type IIS on the search in Start menu and did not retrieve any related results you can follow below steps:

Go to Control Panel -> Programs -> Programs and Features and click on "Turn Windows Features on or off". Refer the attached diagram.



Make sure you have ticked what is shown in here to avoid getting exceptions when you deploy WCF in IIS and try to access it from the browser.


I mainly referred this link to create WCF application but did some slight changes on Web.config. (Important: Please create your application directory in C:\inetpub\wwwroot\ directory)

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
   <system.serviceModel>
      <bindings>
         <basicHttpBinding>
            <binding name="BasicHttpEndpointBinding">
               <security mode="TransportCredentialOnly">
                  <transport clientCredentialType="Windows" />
               </security>
            </binding>
         </basicHttpBinding>
      </bindings>
      <services>
         <service name="Microsoft.ServiceModel.Samples.CalculatorService">
            <endpoint address="" binding="basicHttpBinding" contract="Microsoft.ServiceModel.Samples.ICalculator" />
            <endpoint address="mex" binding="basicHttpBinding" contract="IMetadataExchange" />
         </service>
      </services>
      <behaviors>
         <serviceBehaviors>
            <behavior>
               <serviceMetadata httpGetEnabled="true" />
            </behavior>
         </serviceBehaviors>
      </behaviors>
   </system.serviceModel>
  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel"
              switchValue="Information, ActivityTracing"
              propagateActivity="true" >
        <listeners>
             <add name="xml"/>
        </listeners>
      </source>
      <source name="System.ServiceModel.MessageLogging">
        <listeners>
            <add name="xml"/>
        </listeners>
      </source>
      <source name="myUserTraceSource"
              switchValue="Information, ActivityTracing">
        <listeners>
            <add name="xml"/>
        </listeners>
      </source>
    </sources>
    <sharedListeners>
        <add name="xml"
             type="System.Diagnostics.XmlWriterTraceListener"
             initializeData="Error.svclog" />
    </sharedListeners>
  </system.diagnostics>
</configuration>

In order to create the application you need to first go to "IIS Manager" and expand the node and go to "Default Web Site" and right click on it as follows.


Now go to the application you have created and select it. In the feature view, under IIS click on "Authentication" and disable "Anonymous Authentication" and enable "Windows Authentication".

Now you need to add users in order to use windows authentication.
For that go to Computer Management -> Local Users and Groups -> Users and create new user. Uncheck "User must change password at next logon" and click "User cannot change password" and "Password never expires".

Then go to Computer Management -> Local Users and Groups ->Groups and select "IIS_IUSERS". Write click on it and click on All Tasks->Add to Group and add the user created in the previous step.

Now when you type the app url in browser you should be prompted to enter credentials.


No comments: