There are lots of articles and other resources available on WCF security. However recently one of my clients put across a requirement where WCF REST service was used in an application but before the data could be fetched from the REST URL, he wanted to pass the windows credentials and authenticate the user. The client was using a Windows application. I took this thought ahead and came up with a solution that I will be sharing through this article
The WCF service created with below interface and Web.Config:
public interface IService1
string GetData(int value);
Public Interface IService1
<OperationContract, WebGet> _
Function GetData(ByVal value As Integer) As String
The ‘webHttpBinding’ uses security mode as ‘TransportWithCredential’ and uses ‘ClientCredentialType=Windows’. The Service behavior denies anonymous logon access.
To test this service, I have published it with IIS 7.0 and then tried to browse “Service.Svc”. It asks for the windows credential.
The console client application defined makes call to WCF REST service as shown below:
static void Main(string args)
var val = 100;
Uri reqUri = new Uri("http://localhost/REST_Security/Service1.svc/GetData?value=" + val);
Console.WriteLine("Enter USer Name");
string userName = Console.ReadLine();
string password = Console.ReadLine();
WebRequest req = WebRequest.Create(reqUri);
req.PreAuthenticate = true;
NetworkCredential credential = new NetworkCredential(userName, password);
req.Credentials = credential;
WebResponse resp = req.GetResponse();
DataContractSerializer data = new DataContractSerializer(typeof(string));
var res = data.ReadObject(resp.GetResponseStream());
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim val = 100
Dim reqUri As New Uri("http://localhost/REST_Security/Service1.svc/GetData?value=" & val)
Console.WriteLine("Enter USer Name")
Dim userName As String = Console.ReadLine()
Dim password As String = Console.ReadLine()
Dim req As WebRequest = WebRequest.Create(reqUri)
req.PreAuthenticate = True
Dim credential As New NetworkCredential(userName, password)
req.Credentials = credential
Dim resp As WebResponse = req.GetResponse()
Dim data As New DataContractSerializer(GetType(String))
Dim res = data.ReadObject(resp.GetResponseStream())
The NetworkCredential class is used to define client credentials and the important point here is that the ‘PreAuthenticate’ property of the WebRequest class is set to ‘true’. This indicates that credential information will be sent when the client makes a request to the service. The NetworkCredential object is passed to the ‘Credential’ property of the ‘WebRequestClass’.
After running the application the output will be displayed as below:
Conclusion: As like normal WCF services, WCF REST base services can also be easily configured for security.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce the pre-order of The Absolutely Awesome Book on C# and .NET. This is a concise technical eBook and will be available in PDF, ePub, and mobi.
Organized around concepts, this eBook aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core. Use these concepts in your next .NET Project or to crack your next .NET Interview.
Click here to Pre-Order this eBook at a Discounted Price!