Showing posts with label Custom Basic Authentication. Show all posts
Showing posts with label Custom Basic Authentication. Show all posts

Tuesday, 26 February 2013

WCF Restful service with BASIC Authentication

Just wanted to show code for configuring BASIC authentication on REST service.

Hosting part
---------------
               WebHttpBinding binding = new WebHttpBinding();
                binding.Security.Mode = WebHttpSecurityMode.TransportCredentialOnly;
                binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
                binding.Security.Transport.Realm = "domain"; //Enter domain name

                host = new WebServiceHost(typeof(Service), address.Uri);
                host.AddServiceEndpoint(typeof(IService), binding, address.Uri);
                host.Open();

Client
--------
           WebHttpBinding binding = new WebHttpBinding();

            binding.Security.Mode = WebHttpSecurityMode.TransportCredentialOnly;
            binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
           
            using (WebChannelFactory factory = new WebChannelFactory(binding, address.Uri))
            {
                factory.Credentials.UserName.UserName = "user";
                factory.Credentials.UserName.Password = "password";

                var channel = factory.CreateChannel();

                channel.CallSomething();
            }

Wednesday, 4 January 2012

Custom(!!!) Basic Authentication

How often we think about using BASIC authentication only to figure out that it is hardwired to windows accounts? So what we have Custom Basic authentication using httpmodules. Excellent article here

One more compelling reason to go for Custom BASIC authentication, it is not tied to  UTF-8(uses ISO-8859-1), hence passing non 8-bit characters doesn't work at all.
http://stackoverflow.com/questions/702629/utf-8-characters-mangled-in-http-basic-auth-username/703341#703341
http://stackoverflow.com/questions/7242316/what-encoding-should-i-use-for-http-basic-authentication
http://greenbytes.de/tech/webdav/draft-reschke-basicauth-enc-latest.html
http://tools.ietf.org/html/rfc2617#section-2