Authentication on WCF Data Service or OData: Windows Authentication Part 1

No.of Views6108
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  04 Oct 2010 10:10:51
Tag : WCF , General
I am going to show how to enable windows authentication on WCF Data Service.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In this article, I am going to show how to enable windows authentication on WCF Data Service. 
Follow the below steps

Step 1

Create WCF Data Service.
Read below how to create WCF Data Service and introduction to OData.

http://www.codegain.com/articles/wcf/general/introduction-to-data-transfer-in-wcf.aspx

While creating data model to be exposed as WCF Data Service, we need to take care of only one thing that Data model should be created as SQL Login 

Image Loading

So while creating data connection for data model connects to data base through SQL Login.

Step 2

Host WCF Data Service in IIS.  WCF Data Service can be hosted in exactly the same way a WCF Service can be hosted. Read below how to host WCF 4.0 service in IIS 7.5

http://www.codegain.com/articles/wcf/miscellaneous/how-to-create-wcf-4-0-service-and-hosting-in-iis-7-5.aspx

Step 3
Now we need to configure WCF Service hosted in IIS for Windows authentication. 

Image Loading

Here I have hosted WCF Data Service in WcfDataService IIS web site. Select WcfDataService and in IIS category you can see Authentication tab. 

Image Loading

On clicking on Authentication tab, you can see various authentication options. Enable Windows authentication and disable all other authentication 

Image Loading

To enable or disable a particular option just click on that and at left top you can see the option to toggle 

Image Loading

Now by completing this step you have enabled the Windows authentication on WCF Data Service hosted in IIS.


Passing credential from .Net Client
If client windows domain is having access to server then

Image Loading

If client is not running in windows domain which is having access to server then credential we need to pass the as below,

Image Loading

So to fetch all the records
Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Services.Client;
using ConsoleApplication1.ServiceReference1; 

namespace ConsoleApplication1
{class Program
    {static void Main(string[] args)
        {
        
            NorthwindEntities1 entity = new NorthwindEntities1
                                       (new Uri("http://localhost:7890/wcfdataservice1.svc/"));
            entity.Credentials = System.Net.CredentialCache.DefaultCredentials;
            entity.Credentials = new System.Net.NetworkCredential("UserName", "Password", "Domian");try{

                var result = from r in entity.Products select r;foreach (var r in result)
                {
                    Console.WriteLine(r.ProductName);
                }
            }catch (DataServiceQueryException ex)
            {
                Console.WriteLine(ex.StackTrace);
            }    
                       
        }        
    }
}

Conclusion

In above article we saw how to enable Windows authentication on WCF Data Service and then how to consume from .Net client. In next article we will see how to consume Windows authenticated WCF Data Service from SilverLight client.

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top
</