Development

How to authenticate a Windows account with a WCF service

By 1st May 2014 2 Comments

One piece of coursework that I had to complete this year required us to communicate with a WCF service sat on a server on our university campus. Due to the limitations of public access, our lecturer told us that we couldn’t access the WCF service from home and would have to do the coursework in the labs. Thankfully at the time of the coursework handout, I knew what the issue was and it is to do with authentication. Having previously worked with WCF services while learning to develop using ASP.net and working at Seed Software, I knew the work around to getting access to the services at home.

In our university labs, we use active directory credentials to login to our accounts. This means that we can only access our accounts either via VPN to the university network or by logging in on a computer on the university network on campus.

Thankfully, WCF services allow you to pass your Windows credentials to it before you start attempting to call methods so that it won’t throw exceptions. Here are the steps to doing it using my coursework service as an example:

  1. Initialise the WCF service object
    var ClientServiceClient = new ClientServiceClient();
  2. Now pass your login username and password
    ClientServiceClient.ClientCredentials.Windows.ClientCredential.UserName = // Username (e.g. 400622)
    ClientServiceClient.ClientCredentials.Windows.ClientCredential.Password = // Password (e.g. password)

And there you have it, simple! You can then call any of the methods from your service reference without worrying about exceptions being thrown everywhere.

A simple tip/trick that saved me from the nightmare of being in the university labs and on campus daily.

Author James Croft

James is a senior software developer for Black Marble and Microsoft MVP in Windows Development with over 6 years of experience developing applications for the Windows platform. James’s passion for learning new technologies gives him the opportunities to expose them to the wider community through tutorials, lessons learned and best practices through his online blog site, personal YouTube channel, and local communities through talks at events, schools and universities.

More posts by James Croft

Join the discussion 2 Comments

Leave a comment