Getting Started with ASP.NET MVC - Part 5: How to do programming with razor syntax
First Time? You can support us by signing up. It takes only 5 seconds. Click here to sign up. If you already have an account, click here to login.


Upload Image Close it
Select File

Browse by Tags · View All
BRH 58
#ASP.NET 55
ASP.NET 50
#DOTNET 49
.NET 40
WCF 21
DOTNET 12
c# 8
windows azure 7
SILVERLIGHT 7

Archive · View All
April 2011 9
March 2011 9
February 2011 8
December 2010 7
November 2010 5
September 2010 5
August 2010 5
May 2011 4
October 2010 4
January 2011 2

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

Dec 3 2010 1:55AM by Dhananjay Kumar   

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://dhananjaykumar.net/2010/06/13/introduction-to-wcf-data-service-and-odata/

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

So while creating data connection for data model connect 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://dhananjaykumar.net/2010/09/07/walkthrough-on-creating-wcf-4-0-service-and-hosting-in-iis-7-5/

Step 3

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

image

Here I have hosted WCF Data Service in WcfDataService IIS web site.

Select WcfDataService and in IIS category you can see Authentication tab.

image

On clicking on Authentication tab, you can see various authentication options.

Enable Windows authentication and disable all other authentication

image

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

image

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

NorthwindEntities1 entity = newNorthwindEntities1
                           (newUri("http://localhost:7890/wcfdataservice1.svc/"));
entity.Credentials = System.Net.CredentialCache.DefaultCredentials;

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

entity.Credentials = newSystem.Net.NetworkCredential("UserName", "Password", "Domian");

So to fetch all the records

Program.cs

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

namespace ConsoleApplication1
{
    classProgram
    {
        staticvoid Main(string[] args)
        {
            NorthwindEntities1 entity = newNorthwindEntities1
                                       (newUri("http://localhost:7890/wcfdataservice1.svc/"));
            entity.Credentials = System.Net.CredentialCache.DefaultCredentials;
            entity.Credentials = newSystem.Net.NetworkCredential("UserName", "Password", "Domian");
            try
            {
                var result = from r inentity.Productsselect r;
                foreach (var r in result)
                {
                    Console.WriteLine(r.ProductName);
                }
            }
            catch (DataServiceQueryException ex)
            {
                Console.WriteLine(ex.StackTrace);
            }
        }
    }
}

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 SilverLightclient.

Tags: #DOTNET, #WCF, WCF, #ASP.NET, ASP.NET, BRH, .NET,


Dhananjay Kumar
38 · 5% · 1188
0
Liked
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

Your Comment


Sign Up or Login to post a comment.

    Copyright © Beyondrelational.com Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising