- Download source code — 32.4 KB
Table of contents
- Introduction and goal
- My other WCF FAQ articles
- Step 1: Create a WCF project
- Step 2: Ensure authentication mode is Windows
- Step 3: Define the binding in the web.config file
- Step 4: Bind the bindings with the 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
Introduction and goal
In this session, we will go through eight basic steps by which we can enable Windows authentication security on BasicHttpBinding
. There are two types of security you can define in WCF: transport level and message level. In this article, we will discuss how we can define transport level security on BasicHttpBinding
.
Nowadays 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 a 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 a WCF project
Create a WCF service application project as shown in the below figure:
By default, the WCF project creates a class file which has the GetData
function. This function takes in a number values and displays an explanatory sentence like ‘You entered 1 value’ when you enter ‘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 the authentication mode is Windows.
<authentication mode="Windows" />
Step 3: Define the binding in the web.config file
The third step is to define the bindings and the transport type. To define the bindings, we need to enter the 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>
Overall your <system.serviceModel>
XML part as a whole with bindings and services is as 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"> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel>
Step 5: Ensure that anonymous access is disabled
Go to IIS properties and click on the 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 IIS. Make the directory 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
Let’s consume the WCF service. Add an ASP.NET web application and do a add web reference. 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. 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.
For further reading do watch the below interview preparation videos and step by step video series.
- ASP.NET MVC Interview Questions & Answers
- C# Interview Questions with Answers
- Angular Interview Questions with Answers
- Learn Azure step by step.
- Learn MVC 5 Step by Step
- MVC Core Step by Step Tutorial
- SQL Server step by step
- Session & Viewstate in ASP.NET.
“I am fluent in over six million forms of communication…” – C-3PO
Historically, all of the services I’ve created on Windows (like a daemon from the Unix world), have been written in C/C++. Recently though however, I’ve needed to create a service in a .NET language like C#. There are several ways of doing this. I could have kept with the C++ model and used CLR integration, but I thought I might give C# a chance this time.
Additionally, not only did I need to create the service, I also needed to enable an IPC (inter process communication) type connection to another .NET application, in this case a web application. I decided to go with WCF (Windows Communications Framework). WCF is very cool, although I suspect not a communication system understood by C-3PO.
Whilst this post describes WCF, it should also be a good description of how to start with Windows Services in .NET.
Creating the service itself was quite easy. I was using Visual Studio 2013 and .NET 4.5. You simply create a new project of the type Windows Service, and you’re half way there.
The first thing to do is to rename Service1.cs in the designer to an appropriate name. This will be the name of your service. If you rename the file in the designer, the designer will rename the class for you as well.
The basic premise of a .NET Windows Service, is that the Service Control Manager on Windows will either automatically on boot, or manually, instruct your service to start. You then add code to be executed on service Start, and code to be executed on service Stop. What you do in between is up to you. Your service might be idle, waiting for an external event to trigger action, or you could have a loop based on a timer to execute tasks on any given frequency.
In this example, we will go with both. This service will react to WCF calls from an external application, and also perform a maintenance task every 24 hours. Additionally, throughout the examples I’ve kept clear of using structured error handling (try/catch/finally), and I’ve hardcoded information in (like the WCF URI). Generally, try make configuration parameters flexible and not hardcoded (perhaps use a .NET settings file, or the registry). Error handling code is also key, but makes the examples somewhat unwieldy.
Let’s add the initial code. All I’m going to do here, is initialize some basic variables and log some entries to the Windows Application Event Log.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Reflection; using System.ServiceProcess; using System.Text; namespace SampleWindowsService { public partial class SampleWindowsService : ServiceBase { private static string _strAppTitle = ""; private static string _strAppVersion = ""; private static string _strAppPath = ""; public SampleWindowsService() { Assembly aAssembly = Assembly.GetExecutingAssembly(); Version vAssembly = aAssembly.GetName().Version; InitializeComponent(); // Initialize Global Variables _strAppTitle = "Sample Windows Service"; _strAppVersion = string.Format("{0}.{1}.{2}", vAssembly.Major, vAssembly.Minor, vAssembly.Build); _strAppPath = System.IO.Path.GetDirectoryName(aAssembly.Location); // Set Service Parameters this.ServiceName = "SampleWindowService"; this.CanStop = true; this.CanPauseAndContinue = false; this.AutoLog = true; } protected override void OnStart(string[] args) { // Service Startup Code this.EventLog.WriteEntry(string.Format("Service {0} (v{1}) starting.", _strAppTitle, _strAppVersion)); } protected override void OnStop() { // Service Stop Code this.EventLog.WriteEntry("Service stopping."); } } }
All I’ve done here is import the System.Reflection namespace so that I could determine the assembly’s version, and put some basic initialization and logging code in. The AutoLog parameter instructs the service to automatically create event log entries for the service started and service stopped events. I’ve then added additional log entries for starting and stopping. I’ve also set the CanPauseAndContinue parameter to false. This means my service can be started and stopped, but not paused. If you do enable this, you simply need to override the OnPause and the OnContinue methods. Pausing a service can be useful, for example if you need to have a service still operate internally, but stop listening for client requests.
With that very basic code in place, we now need to install the service so that it can be started and stopped as desired. There are two ways of doing this. One way is to use the .NET InstallUtil utility, the other is to build install functionality into your assembly. In this post I’m going to go with the InstallUtil version, but for those interested here is a link to how to do this programmatically (so the service executable can install and uninstall itself).
The first thing we need to do to enable the service to be installed, is to open the Designer view of the service, right click and select Add Installer. This will add an installer class to your project. You then open the installer class and modify the parameters as required. In particular, you will need to update:
-
The Service Process Installer’s Account parameter – this is where you can configure your service to start as the specified Windows Local/Domain user account, or a system account, e.g. LocalService. Unless you need to use a real user account, stick with LocalService as a default, although be aware that LocalService is a non-administrative account. LocalSystem has full administrative access, but generally try avoid running a service with an administrative account if you can. If this is a real application being developed, ideally use a dedicated windows account with just the specific permissions needed to run the application.
- The Service Installer’s DisplayName and StartType parameters. The DisplayName parameter sets the name of the service as it appears in the Services administrative tool. The StartType parameter instructs the service to be started automatically on system start, or manually. If you select manually, you will need to use the net start/stop command, or the services administrative tool to start your service.
With that in place, build your project and then use the .NET InstallUtil utility to install the service (installutil nameofyourexecutable.exe). The easiest way to get to this utility is through the Visual Studio Command Prompt.
Now I’m going to add a timer so that we can put in some regular maintenance tasks. In my services, this timer is typically used to kick off a daily log management routine (as I have a tendency towards text file based logs rather than the event log). I’ve included the code excerpts below (I’ve removed the code previously described to keep the code in here readable.
namespace SampleWindowsService { public partial class SampleWindowsService : ServiceBase { private static System.Timers.Timer _oTimerLoop; private const int _iLoopTimer = 24; // Hours public SampleWindowsService() { _oTimerLoop = new System.Timers.Timer(); } protected override void OnStart(string[] args) { _oTimerLoop.Interval = 5000; // Set Initial Timer Interval to 5 seconds _oTimerLoop.Elapsed += _oTimerLoop_Elapsed; _oTimerLoop.Start(); } protected override void OnStop() { _oTimerLoop.Stop(); } private void _oTimerLoop_Elapsed(object sender, EventArgs e) { // Reset Loop Timer _oTimerLoop.Interval = 1000 * 60 * 60 * _iLoopTimer; // Initiate Scheduled Tasks... } } }
I’ve defined a variable to store an instance of the System.Timers.Timer class, attached an event handler method, and set the initial timeout. I’ve set the timer interval for 5 seconds so that 5 seconds after service start, the scheduled event occurs. In the event handler itself, the timer interval is reset back to 24 hours. So the timer kicks off 5 seconds after start, and then every 24 hours. Yes, I know I’m a bit strange using _oTimerLoop.Interval = 1000 x 60 x 60 x _iLoopTimer instead of simply using _oTimerLoop.Interval = 3600000 x _iLoopTimer. I find it easier to read if I can see the individual numbers, in this case 1000msec * 60 (seconds) * 60 (minutes) * _iLoopTimer (hours).
Now, when your timer routine does execute, it’s generally best to kick off your maintenance task asynchronously. This basically means start another thread, so that thread can execute your maintenance tasks without impacting the controlling thread. You can either use the System.Threading.Thread class, or a System.ComponentModel.BackgroundWorker class for this task.
Well, that’s the basic service structure complete. Let’s move onto WCF.
Windows Communications Framework is a framework for easily enabling communications between processes and systems. The framework handles the bulk of the underlying communications routines so that you can focus on your application logic, and not infrastructure code. In this example, I’m going to add several functions to our service, which can then be called from another application elsewhere.
The first thing we need to do is create an interface to describe the functions we will make available by WCF. An interface is a standard way of externally exposing functionality of a class in an object-oriented programming language. Right click on the project, and add a new interface. I’ve named the interface IIPCService. Interface names typically start with an I. So IPC Service interface gets a name of IIPCService. You will also at this point need to add a project reference to the System.ServiceModel.dll assembly. With that reference added, you can import the System.ServiceModel namespace, and add your list of exposed functions. In the example here, we’re only adding a single function. You can see the ServiceContract and OperationContract attributes added as well. Those attributes can be further configured to adjust the behaviour of WCF.
using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.Text; using System.Threading.Tasks; namespace SampleWindowsService { [ServiceContract] public interface IIPCService { [OperationContract] bool Function1(string strArgument1, out string strResult); } }
With the interface complete, now we need to build the logic itself. A lot of WCF can be controlled through application configuration files, but I’ll be doing the basics programmatically in this example.
Add a class to contain your WCF logic. I created a class called IPCService.
using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.ServiceModel.Channels; using System.ServiceModel.Description; using System.ServiceModel.Security; using System.Text; using System.Threading.Tasks; namespace SampleWindowsService { public class IPCService : IIPCService { private static ServiceHost _sHost = null; internal static void StartService() { BasicHttpBinding httpBinding; _sHost = new ServiceHost(typeof(IPCService)); httpBinding = new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly); httpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows; _sHost.AddServiceEndpoint(typeof(IIPCService), httpBinding, "http://computername:50000/SampleWindowsServiceIPC"); _sHost.Open(); } internal static void StopService() { if (_sHost != null) _sHost.Close(); } public bool Function1(string strArgument1, out string strResult) { OperationContext opContext = OperationContext.Current; MessageProperties mpMessage = opContext.IncomingMessageProperties; RemoteEndpointMessageProperty reMessage = (RemoteEndpointMessageProperty) mpMessage[RemoteEndpointMessageProperty.Name]; strResult = string.Format("Processed Message from [{0}]:{1} ({2}): {3}", reMessage.Address, reMessage.Port.ToString(), opContext.ServiceSecurityContext.WindowsIdentity.Name, strArgument1); return true; } } }
In this class, we first associate the class with the interface. This is done with the line public class IPCService : IIPCService. I then defined a ServiceHost variable to store the reference to the WCF host object. For this example, I’ve chosen to use the BasicHttpBinding class to describe the connectivity. This means that the HTTP protocol will be used for communications. There are other choices, but this was my preference. Note, using HTTP does not mean you need to install a web server (although you could host this in a web server as well). When the StartService method is called, the binding is created. When you create a binding, you need to specify the URI that your service will be accessible on. In this example I’ve used http://computername:50000/SampleWindowsServiceIPC. The breakup of this URI is as follows:
-
http:// – use HTTP.
- computername – your computername goes here.
- 50000 – the TCP port to listen on.
- SampleWindowsServiceIPC – more or less an arbitrary name with the entire string unique to your application.
I’ve also enabled authentication on this binding, so that any caller of this service must authenticate using Windows credentials. There’s numerous other options for this, or you could simply enable anonymous access. It’s generally a good idea to attach an authentication rule to the binding, as this is a network service – it is accessible over the network, depending on your computer/network/firewall configurations etc. Additionally, if you are using authentication, its a good idea to encrypt the communications, which can be done with certificates (HTTPS), IPSec, etc.
One small caveat however, is that because we’re using LocalService as the service account, the service doesn’t have permission to register that binding on the system. There are numerous fixes for this, including granting LocalService permission to establish the binding, pre-establishing the binding, or using an alternate account with permission. I’d generally use a specific service account, and grant it permission to maintain the binding.
I’ve also updated the OnStop and OnStart functions from the service to start/stop the WCF component.
protected override void OnStart(string[] args) { this.EventLog.WriteEntry(string.Format("Service {0} (v{1}) starting.", _strAppTitle, _strAppVersion)); _oTimerLoop.Interval = 5000; _oTimerLoop.Elapsed += _oTimerLoop_Elapsed; _oTimerLoop.Start(); IPCService.StartService(); } protected override void OnStop() { this.EventLog.WriteEntry("Service stopping."); IPCService.StopService(); }
And that’s it. The service will run and do whatever it needs to do. It will also host the WCF component which can interact with the rest of the service as required. You can see in the Function1 example above, I’m extracting the caller’s IP address, port and username.
The only thing to do now is create a small test application that calls the WCF function. This is the client application.
For this example, I’ve just created a simple Windows Forms Application. Add the service host reference again (System.ServiceModel.dll), and add a reference to the service application (to gain access to the interface). I’ve added a button and a textbox to the main form. Here’s the code we need (again, without error handling).
using System; using System.ComponentModel; using System.ServiceModel; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { ChannelFactory<SampleWindowsService.IIPCService> cfClient; BasicHttpBinding httpBinding; SampleWindowsService.IIPCService service; string strOutput = ""; httpBinding = new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly); httpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows; cfClient = new ChannelFactory<SampleWindowsService.IIPCService>(httpBinding, "http://localhost:50000/SampleWindowsServiceIPC"); cfClient.Credentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation; cfClient.Credentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials; service = cfClient.CreateChannel(); if (service.Function1("input", out strOutput) == true) textBox1.Text = strOutput; cfClient.Close(); } } }
Simple enough code, we create a http binding, attach the URI, specify the authentication method etc. The credentials of the current user are attached to the request. After that, we simply create a communications channel and call the function.
Personally, when I’m creating WCF functions I tend to make then return a boolean or integer based object. I use the returned object to determine success/failure of the call. I then use an out parameter to return data as required.
Pretty cool stuff and relatively simple to do. There’s a huge amount of other stuff you can configure with WCF, but I’ve tried to keep the example simple. It’s amazing what you can do with very little code these days, although it’s always best to understand what the code is actually doing, and keep security etc in mind.
~ Mike
Wcf Services With Windows Authentication And Varied Clients With Solutions
Hello, everyone! In this post, we will investigate how to discover the answer to Wcf Services With Windows Authentication And Varied Clients With Solutions using the computer language.
evaluationContext.Properties["Principal"]=HttpContext.Current.User;
By studying a variety of various examples, we were able to figure out how to fix the Wcf Services With Windows Authentication And Varied Clients With Solutions.
How do I pass credentials to WCF services for Windows authentication?
Please use the following procedure.
- First create a WCF service library in Visual Studio.
- Then create a WCF service application in Visual Studio.
- Then we need to configure the web. config for the service binding, security mode and username/password authentication.
- Now our service is ready.
How do you authenticate in WCF?
- Authentication and Authorization.
- Step 1: Create a WCF Service Application:
- Step 2: Add an AuthenticationService.
- Step 3: Create User Validator class.
- Step 4: Enable Custom Authentication in Global.asax.
- Step 5: Return a Cookie if valid user.
- Step 6: Modify the service configuration.
What are WCF services?
Windows Communication Foundation (WCF) is a framework for building service-oriented applications. Using WCF, you can send data as asynchronous messages from one service endpoint to another. A service endpoint can be part of a continuously available service hosted by IIS, or it can be a service hosted in an application.16-Dec-2021
Does ASP NET support Windows authentication?
Windows Authentication relies on the operating system to authenticate users of ASP.NET Core apps. You can use Windows Authentication when your server runs on a corporate network using Active Directory domain identities or Windows accounts to identify users.30-Aug-2022
How do I pass Windows Authentication credentials from client to Web API service?
Enable Windows Authentication In Web API And Angular App
- Create Web API Project and in Web.config select Authentication mode as “Windows”,
- Use Authorize attribute on the controller or on any action method for security.
- As per the prerequisite enable CORS at controller level along with SupportCredentials true,
Does Windows Authentication use LDAP?
Both Windows Active Directory and LDAP can be used to allow users to connect to Serv-U by using Active Directory credentials. Additionally, LDAP allows for authentication against other LDAP servers such as Apache Directory Server and OpenLDAP.
What are 3 ways to authenticate a user?
There are three common factors used for authentication: Something you know (such as a password) Something you have (such as a smart card) Something you are (such as a fingerprint or other biometric method)06-Jun-2011
What are three basic ways to authenticate?
There are three basic types of authentication. The first is knowledge-based — something like a password or PIN code that only the identified user would know. The second is property-based, meaning the user possesses an access card, key, key fob or authorized device unique to them. The third is biologically based.
How do I enable Windows Authentication for my website?
On the taskbar, click Start, and then click Control Panel. In Control Panel, click Programs and Features, and then click Turn Windows Features on or off. Expand Internet Information Services, then World Wide Web Services, then Security. Select Windows Authentication, and then click OK.22-Mar-2022
What are the types of WCF?
Understanding Various Types of WCF Bindings
- Basic Binding. This binding is provided by the BasicHttpBinding class.
- Web Binding. This binding is provided by the WebHttpBinding class.
- Web Service (WS) Binding.
- WS Dual Binding.
- TCP Binding.
- IPC Binding.
- MSMQ Binding.
- Federated WS Binding.
Hi,
Sharing a simple example on how to enable Windows Authentication for a WCF Service using basicHttpBinding.
We will take an example of the OOB WCF Service that gets created when we create a new WCF Service Application.
Add the following configuration in web.config
<system.serviceModel> <services> <service name="WcfService2.Service1"> <endpoint address="" binding="basicHttpBinding" bindingConfiguration="myBinding" contract="WcfService2.IService1"/> </service> </services> <bindings> <basicHttpBinding> <binding name="myBinding" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"> <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/> <security mode="TransportCredentialOnly"> <transport clientCredentialType="Windows"/> </security> </binding> </basicHttpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior> <!-- To avoid disclosing metadata information, set the values below to false before deployment --> <serviceMetadata httpGetEnabled="true" httpsGetEnabled="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> <protocolMapping> <add binding="basicHttpsBinding" scheme="https" /> </protocolMapping> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> </system.serviceModel>
And enable Windows Authentication in IIS
Hope it helps ..
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’.
8 steps to enable windows authentication on WCF BasicHttpBinding
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.dotnetfunda.com/articles/article221.aspx to see Windows Communication Framework (WCF) — Part 1
http://www.dotnetfunda.com/articles/article222.aspx to see Windows Communication Framework (WCF) — Part 2
http://www.dotnetfunda.com/articles/article343-wcf-tracing-faq.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
Get the source code at the top of this article.