Enabling windows authentication on a REST enabled service is relatively easier task than it’s appear. To test that windows authentication is enabled successfully or not use other browser than Internet explorer because IE will automatically do an NTLM negotiation with domain credentials. So when REST service is running in IE, it is not prompting for user credentials.
Service
A very simple contract as REST service, I am going to use.
Contract
namespace RestserviceWindows{
[ServiceContract] public interface IService1{ |
Service implementation
namespace RestserviceWindows{
public classService1 : IService1 |
Configuring the Service
Method #1 Using Factory class
- Create a simple REST service. To create a REST service navigates to File->New->Project->Web and select WCF Service Application project template.
- WCF will create default endpoints inside System.ServiceModel. Delete both the default endpoints. Delete below end points.
<endpoint address=“” binding=“wsHttpBinding” contract=“RestserviceWindows.IService1“>
<identity> |
-
Right click on .SVC file. If your service name is Service1 then right click on Service1.svc. After right clicking select View Markup. Add Factory here
<%@ ServiceHost Language=”C#” Debug=”true” Service=”RestserviceWindows.Service1″ CodeBehind=”Service1.svc.cs” Factory=”System.ServiceModel.Activation.WebServiceHostFactory“ %> |
- Host the service in IIS. To host right click on service and Publish in IIS.
- Configure IIS for windows authentication.
The above 5 steps are required to host a REST service in IIS with windows authentication.
Method #2 Configuring End Point without Meta Data
- Create a simple REST service. To create a REST service navigates to File->New->Project->Web and select WCF Service Application project template.
- Delete endpoint address for MEX and binding mexHttpBinding
- Configure EndPoint to enable REST service.
- Configure EndPoint to enable windows authentication on the service.
- Configure End Point behavior.
-
Make sure, there is no factory class provided for the service in markup of .svc
<%@ ServiceHost Language=”C#” Debug=”true” Service=”RestserviceWindows.Service1″ CodeBehind=”Service1.svc.cs” %> |
- Host the service in IIS. To host right click on service and Publish in IIS.
- Configure IIS for windows authentication.
<system.serviceModel> <services> <service <endpoint <identity> <dns </identity> </endpoint> </service> </services> <behaviors> <serviceBehaviors> <behavior <serviceMetadata <serviceDebug </behavior> </serviceBehaviors>
<behavior <webHttp/> </behavior> </endpointBehaviors> </behaviors>
<webHttpBinding> <binding <security <transport </security> </binding>
</bindings> </system.serviceModel> |
Method # 3 Configuring End Point with Meta Data
So far in above two methods, Meta Data is not getting exposed. Because mexHttpBinding only supports anonymous binding. So to enable windows authentication on mexHttpBinding , we need to configure IMetaDataContract with some other binding like basicHttpBinding or webHttpBinding.
- Create a simple REST service. To create a REST service navigates to File->New->Project->Web and select WCF Service Application project template.
- Configure EndPoint to enable REST service.
- Configure EndPoint to enable windows authentication on the service.
- Configure End Point behavior for both End Points.
- Make sure, there is no factory class provided for the service in markup of .svc
<%@ ServiceHost Language=”C#” Debug=”true” Service=”RestserviceWindows.Service1″ CodeBehind=”Service1.svc.cs” %> |
- Host the service in IIS. To host right click on service and Publish in IIS.
- Configure IIS for windows authentication.
<system.serviceModel> <services> <service <endpoint <identity> <dns </identity> </endpoint> <endpoint </service> </services> <behaviors> <serviceBehaviors> <behavior <serviceMetadata <serviceDebug </behavior> </serviceBehaviors>
<behavior <webHttp/> </behavior> </endpointBehaviors> </behaviors> <bindings> <webHttpBinding> <binding <security <transport </security> </binding> </webHttpBinding> </bindings> </system.serviceModel> |
Note: Binding for IMetaDataExchange contract is webHttpBinding.
Consuming the Service
To enable a client to consume a windows authenticated WCF service, client has to pass domain credentials . If HttpClient is used by the client to consume the service then client has to set the credential like
clt.TransportSettings.Credentials=System.Net.CredentialCache.DefaultCredentials;
where clt is instance of HttpClient .
- Add Microsoft.Http and System.RunTime.Serlization assembly at refernece
- Add namespace of Microsoft.Http and System.Runtime.Serlization
- Create insatnce of HttpClint
- Set the default credential from the cache.
- Perfrom HTTP operation.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Http; using System.Runtime.Serialization; using System.Net; namespace ConsoleApplication1 { class Program { clt.TransportSettings.Credentials = System.Net.CredentialCache.DefaultCredentials; |
Leave a Reply