Click here to Skip to main content
15,881,424 members
Articles / Security / Encryption

Basic Authentication on a WCF REST Service

Rate me:
Please Sign up or sign in to vote.
4.89/5 (46 votes)
28 Feb 2011CPOL6 min read 346K   10.8K   191  
An assembly that adds Basic Authentication to an IIS hosted WCF REST service to validate against any back-end.
using System.ServiceModel.Channels;
using BasicAuthenticationUsingWCF;
using NUnit.Framework;

namespace BasicAuthenticationUsingWCFTest
{
   [TestFixture]
   public class HttpRequestAuthorizationExtractorTest
   {
      [Test]
      public void ShouldExtractBasicAuthorizationHeaderIfExists()
      {
         const string AuthorizationHeader = "Basic SGVsbG8gQmFzZTY0";
         Message dummyMessage = CreateRequestMessage(AuthorizationHeader);
         var extractor = new AuthorizationStringExtractor();
         string returnedAuthorizationHeader;
         bool result = extractor.TryExtractAuthorizationHeader(dummyMessage, out returnedAuthorizationHeader);
         Assert.That(returnedAuthorizationHeader, Is.EqualTo(AuthorizationHeader));
         Assert.That(result, Is.True);
      }

      [Test]
      public void ShouldValidateIfAuthorizationHeaderExists()
      {
         Message dummyMessage = CreateNormalRequestMessage();
         var extractor = new AuthorizationStringExtractor();
         string returnedAuthorizationHeader;
         bool result = extractor.TryExtractAuthorizationHeader(dummyMessage, out returnedAuthorizationHeader);
         Assert.That(result, Is.False);
      }

      private static Message CreateRequestMessage(string authorizationheaderToAdd)
      {
         const string BasicAuthenticationHeaderName = "Authorization";
         Message requestMessage = Message.CreateMessage(MessageVersion.None, null);
         var requestProperty = new HttpRequestMessageProperty();
         requestProperty.Headers.Add(BasicAuthenticationHeaderName, authorizationheaderToAdd);
         requestMessage.Properties.Add(HttpRequestMessageProperty.Name, requestProperty);
         return requestMessage;
      }

      private static Message CreateNormalRequestMessage()
      {
         var requestProperty = new HttpRequestMessageProperty();
         Message requestMessage = Message.CreateMessage(MessageVersion.None, null);
         requestMessage.Properties.Add(HttpRequestMessageProperty.Name, requestProperty);
         return requestMessage;
      }
   }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Architect http://www.simpletechture.nl
Netherlands Netherlands
Patrick Kalkman is a senior Software Architect with more than 20 years professional development experience. He works for SimpleTechture where he helps teams develop state of the art web applications.

Patrick enjoys writing his blog. It discusses agile software development. Patrick can be reached at patrick@simpletechture.nl.

Published Windows 8 apps:


Published Windows Phone apps:


Awards:

Best Mobile article of March 2012
Best Mobile article of June 2012

Comments and Discussions