Click here to Skip to main content
11,568,157 members (35,395 online)
Click here to Skip to main content

8 steps to enable windows authentication on WCF BasicHttpBinding

, 9 May 2009 CPOL 253.6K 4.1K 121
Rate this:
Please Sign up or sign in to vote.
8 steps to enable windows authentication on WCF BasicHttpBinding
This is an old version of the currently published article.

8 steps to enable windows authentication on WCF BasicHttpBinding

Introduction and Goal

My other WCF FAQ articles

Step 1:- Create WCF project

Step 2 :- Ensure authentication mode is windows

Step 3 :- Define the binding in web.config file

Step 4 :- Bind the bindings with service interface

Step 5 :- Ensure that anonymous access is disabled

Step 6:- Host your WCF service on IIS

Step 7 :- Consume the WCF service

Step 8:- Create the WCF client

Source code

Introduction and Goal

In this session we will go through basic 8 steps by which we can enable windows authentication security on ‘BasicHttpBinding’. There are two types of security you can define in WCF one is the transport level and the other is the message level. In this article we will discuss how we can define transport level security on ‘BasicHttpBinding’.

Now a days I am distributing my 400 questions and answers ebook which covers major .NET related topics like WCF,WPF,WWF,Ajax,Core .NET,SQL Server,Architecture and lot lot more. I am sure you will enjoy this ebook.
http://www.questpond.com/SampleDotNetInterviewQuestionBook.zip

My other WCF FAQ articles

http://www.codeproject.com/KB/aspnet/WCFPart2.aspx
to see Windows Communication Framework (WCF) - Part 1

http://www.codeproject.com/KB/aspnet/WCF.aspx
to see Windows Communication Framework (WCF) - Part 2

WCFTracingFAQ.aspx
to see WCF Tracing FAQ

Step 1:- Create WCF project

Create a project of WCF service application as shown in the below figure.

By default the WCF project creates a class file which has ‘GetData’ function. This function takes in a number values and displays a explanatory sentence like ‘You entered 1 value’ , in case you have entered ‘1’.

public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
}

Step 2 :- Ensure authentication mode is windows

When we create a WCF service application it also has a web.config file associated with it. So open the web.config file and ensure that authentication mode is windows.

<authentication mode="Windows" />

Step 3 :- Define the binding in web.config file

The third step is to define the bindings and the transport type. To define the bindings we need to enter ‘basicHttpBinding’ element inside the ‘bindings’ XML tag. We also need to define the ‘clientCredentialType’ as windows.

<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpEndpointBinding">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<services>
.........
.........
</system.serviceModel>

Step 4 :- Bind the bindings with service interface

Now the bindings defined needs to be associated with a service interface i.e. ‘service1’. So we need to modify the services elements as shown below. You can note that we have defined an end point which has the binding association.

<system.serviceModel>
........
........
........
<services>
<service behaviorConfiguration="WCFWindowsBasicHttpBinding.Service1Behavior" name="WCFWindowsBasicHttpBinding.Service1">
<endpoint address="" binding="basicHttpBinding"
bindingConfiguration="BasicHttpEndpointBinding"
name="BasicHttpEndpoint" contract="WCFWindowsBasicHttpBinding.IService1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</service>
</services>
.........
.........
.........
.........
</system.serviceModel>

So over all your <system.serviceModel> XML part as whole with bindings and services is a shown below.

<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpEndpointBinding">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="WCFWindowsBasicHttpBinding.Service1Behavior" name="WCFWindowsBasicHttpBinding.Service1">
<endpoint address="" binding="basicHttpBinding"
bindingConfiguration="BasicHttpEndpointBinding"
name="BasicHttpEndpoint" contract="WCFWindowsBasicHttpBinding.IService1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WCFWindowsBasicHttpBinding.Service1Behavior">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>

Step 5 :- Ensure that anonymous access is disabled

Go to IIS properties and click on security tab and ensure that anonymous access is disabled and only windows authentication is enabled.

Step 6:- Host your WCF service on IIS

We need to host our service in the IIS. So make the directory as an IIS application so that your service can be hosted. Now if you try to browse the service i.e. the SVC file you will see that it pops up the authentication authorization security dialog box. So this service cannot be executed with windows authentication.

Step 7 :- Consume the WCF service

So let’s consume this WCF services. So add an ASP.NET webapplication and do a add webreference. You will be popped up with a dialog box as shown below. Click on add reference so that a proxy is generated for the WCF service.

Step 8:- Create the WCF client

Type in the following code snippet in your page load. So add the namespace reference and call the method ‘GetData’. The most important step to note is the credential supplied. ‘DefaultCredentials’ passes the current windows identity to the WCF service.

If you execute the service you should get the following display as shown below.

You can try commenting the below code in your client in other words we are not passing any credentials.

obj.Credentials = System.Net.CredentialCache.DefaultCredentials;

Now if you execute you should get the below error stating that this is an unauthorized call.

Source code

You can also download the source from Click Here

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Shivprasad koirala
Architect http://www.questpond.com
India India

I am a Microsoft MVP for ASP/ASP.NET and currently a CEO of a small
E-learning company in India. We are very much active in making training videos ,
writing books and corporate trainings. Do visit my site for 
.NET, C# , design pattern , WCF , Silverlight
, LINQ , ASP.NET , ADO.NET , Sharepoint , UML , SQL Server  training 
and Interview questions and answers


You may also be interested in...

Comments and Discussions


Discussions posted for the Published version of this article. Posting a message here will take you to the publicly available article in order to continue your conversation in public.
 
QuestionServiceClient1 does not contain definition for Credentials Pin
Member 996489623-Apr-13 21:30
memberMember 996489623-Apr-13 21:30 
AnswerRe: ServiceClient1 does not contain definition for Credentials Pin
Afazal MD 310420911-Dec-13 22:43
memberAfazal MD 310420911-Dec-13 22:43 
GeneralRe: ServiceClient1 does not contain definition for Credentials Pin
sudh198315-Feb-14 6:03
membersudh198315-Feb-14 6:03 
QuestionNot able to consume service after clientCredentialType is set to " Windows" Pin
Member 996489623-Apr-13 19:43
memberMember 996489623-Apr-13 19:43 
AnswerRe: Not able to consume service after clientCredentialType is set to " Windows" Pin
Member 996489623-Apr-13 20:54
memberMember 996489623-Apr-13 20:54 
QuestionIs there a way to set the credentials through the config file? Pin
programmeranalyst2-Apr-13 11:49
memberprogrammeranalyst2-Apr-13 11:49 
QuestionCode Not working in VS 2012 Pin
meeram3913-Dec-12 22:47
membermeeram3913-Dec-12 22:47 
AnswerRe: Code Not working in VS 2012 Pin
Afazal MD 310420911-Dec-13 22:42
memberAfazal MD 310420911-Dec-13 22:42 
Questionwell done Pin
Vincenzo Malvone11-Dec-12 3:36
memberVincenzo Malvone11-Dec-12 3:36 
QuestionVery nice Pin
saikat Malakar9-Nov-12 1:32
membersaikat Malakar9-Nov-12 1:32 
QuestionAuthntication not working if service added as Service Reference with BasicHttpBinding Pin
Sendilkumar.M22-Jun-12 2:38
memberSendilkumar.M22-Jun-12 2:38 
QuestionBasicHTTPBinding Pin
Nilpesh29-May-12 20:31
memberNilpesh29-May-12 20:31 
AnswerRe: BasicHTTPBinding Pin
andrusha00713-Jul-12 4:51
memberandrusha00713-Jul-12 4:51 
QuestionPassing Windows credentials from from Client to Database through WCF Pin
Manjunathabe0114-Mar-12 23:35
memberManjunathabe0114-Mar-12 23:35 
QuestionFacing one issue. Pin
rajwithu20-Dec-11 17:58
memberrajwithu20-Dec-11 17:58 
QuestionProduction Deployment? Pin
pravinpatkar14-Oct-11 11:10
memberpravinpatkar14-Oct-11 11:10 
GeneralMy vote of 4 Pin
Eng. bipin24-Sep-11 2:29
memberEng. bipin24-Sep-11 2:29 
Questionit is not working for WCF 4.0 and IIS 7 Pin
Vivek Shrivastava17-May-11 0:05
memberVivek Shrivastava17-May-11 0:05 
GeneralSecurity settings not enabled for the IIS application Pin
ABlokha7727-Mar-11 20:27
memberABlokha7727-Mar-11 20:27 
GeneralSpeciifc Windows Id Authentication Pin
meatcp4-Mar-11 9:56
membermeatcp4-Mar-11 9:56 
Generalit is not enabled for the IIS application that hosts this service Pin
BMWABCD16-Sep-10 12:52
memberBMWABCD16-Sep-10 12:52 
QuestionUnauthorized with client authentication scheme... Pin
Pradeep Babu Yadagani8-Sep-10 9:11
memberPradeep Babu Yadagani8-Sep-10 9:11 
QuestionSilverlight 3??? Pin
markbaer6-Jan-10 13:22
membermarkbaer6-Jan-10 13:22 
AnswerRe: Silverlight 3??? Pin
robalexclark15-Jan-10 1:58
memberrobalexclark15-Jan-10 1:58 
GeneralNot working Pin
biopsy20-Nov-09 14:36
memberbiopsy20-Nov-09 14:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150624.2 | Last Updated 10 May 2009
Article Copyright 2009 by Shivprasad koirala
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid