Showing posts with label REST. Show all posts
Showing posts with label REST. 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

Console client for a WCF Restful service protected with BASIC authentication(windows credential)

Please refer my earlier post for REST service details, below is an sample code to pass on windows credentials while calling WCF REST service(in IIS - Anonymous access is disabled and BASIC is enabled)

static void Main(string[] args)
        {
            Uri baseAddress = new Uri("http://localhost:8080/SimpleService");

            try
            {
                WebHttpBinding binding = new WebHttpBinding();

                binding.Security.Mode = WebHttpSecurityMode.TransportCredentialOnly;
                binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;

                WebChannelFactory cf = new WebChannelFactory(binding, baseAddress);
       
                //If Default domain is not added in IIS, provide it below - else not needed
                cf.Credentials.UserName.UserName = "user";
                cf.Credentials.UserName.Password = "password";

                SimpleInterface channel = cf.CreateChannel();
               
                Console.WriteLine("Reply Hi : {0}", channel.ReplyHi("Hello, world"));
                Console.WriteLine("Reply Hii : {0}",channel.ReplyHii("Hello, world"));
             }
            catch (CommunicationException ex)
            {
                Console.WriteLine("An exception occurred: " + ex.Message);
            }
        }

Issues while hosting Restful WCF services in IIS

When i hosted my Hello World WCF Restful services, i faced below issue

The requested content appears to be script and will not be served by the static file handler.

My application is using 'DefaultAppPool', For reasons unknown to me, running the below did work. 

c:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i

But soon after this i ran into below issue

"Could not load type 'System.ServiceModel.Activation.HttpModule'"

For reasons unknown to me, running the below did work :(.   

c:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe /iru

http://forums.asp.net/t/1432329.aspx/1 

http://www.fredmastro.com/post/HTTP-Error-40417-Not-Found-e28093-Using-WCF-SVC-Service.aspx 

http://support.microsoft.com/kb/2015129

Tuesday, 3 January 2012

Restful WCF services hosted in Console Application

Sorry - No descriptions, i am bored to death, hence only the code.

 namespace SimpleConsoleWCFHost
{
    [ServiceContract]
    public interface SimpleInterface
    {
        ///

        /// POST, bare body message - http://localhost:8080/SimpleService/SayHi/Suresh
        /// Content-Type: text/html
        ///

        ///
        [OperationContract]
        [WebInvoke(Method="POST", BodyStyle = WebMessageBodyStyle.Bare,
                UriTemplate="SayHi/{name}")]
        void SayHi(string name);

        ///
        /// GET, http://localhost:8080/SimpleService/ReplyHi/Suresh
        /// Content-Type: text/html
        ///

        ///
        ///
        [OperationContract]
        [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Bare,
                UriTemplate = "ReplyHi/{name}")]
        string ReplyHi(string name);

        ///
        /// GET, http://localhost:8080/SimpleService/ReplyHii?name=Suresh
        /// Content-Type: text/html
        ///

        ///
        ///
        [OperationContract]
        [WebGet]
        string ReplyHii(string name);

        ///
        /// GET, http://localhost:8080/SimpleService/ReplyByJson?name=Suresh
        /// Content-Type: text/json
        ///

        ///
        ///
        [OperationContract]
        [WebGet(ResponseFormat=WebMessageFormat.Json)]
        SimpleData ReplyByJson(string name);

        ///
        /// GET, http://localhost:8080/SimpleService/ReplyByXml?name=Suresh
        /// Content-Type: application/xml
        ///

        ///
        ///
        [OperationContract]
        [WebGet(ResponseFormat = WebMessageFormat.Xml)]
        SimpleData ReplyByXml(string name);

        ///
        /// POST, http://localhost:8080/SimpleService/ConverseUsingJson
        /// Content-Type: text/json
        /// {"Body":"Comment received at 03-01-2012 10:13:55","Header":"Welcome to JSON - Suresh"}
        ///

        ///
        ///
        [OperationContract]
        [WebInvoke(UriTemplate = "ConverseUsingJson", Method = "POST",
            RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        SimpleData ConverseUsingJson(SimpleData data);

        ///
        /// POST, http://localhost:8080/SimpleService/ConverseUsingXml
        /// Content-Type: application/xml
        /// colin
        /// http://www.britishdeveloper.co.uk/2011/01/how-to-post-rest-fiddler.html
        ///

        ///
        ///
        [OperationContract]
        [WebInvoke(UriTemplate = "ConverseUsingXml", Method = "POST",
            RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
        SimpleData ConverseUsingXml(SimpleData data);
    }

    [DataContract(Namespace = "")]
    public class SimpleData
    {
        [DataMember]
        public string Header { get; set; }

        [DataMember]
        public string Body { get; set; }
    }

    public class SimpleService : SimpleInterface
    {
        public void SayHi(string name)
        {
            Console.WriteLine("Hi " + name);
        }

        public string ReplyHi(string name)
        {
            return "Hi " + name;
        }

        public string ReplyHii(string name)
        {
            return "Hii " + name;
        }

        public SimpleData ReplyByJson(string name)
        {
            return new SimpleData
            {
                Header = "Welcome to JSON - " + name,
                Body = "Comment received at " + DateTime.UtcNow.ToString()
            };
        }

        public SimpleData ReplyByXml(string name)
        {
            return new SimpleData
            {
                Header = "Welcome to XML - " + name,
                Body = "Comment received at " + DateTime.UtcNow.ToString()
            };
        }

        public SimpleData ConverseUsingJson(SimpleData value)
        {
            value.Header = "JSON Conversation";

            return value;
        }

        public SimpleData ConverseUsingXml(SimpleData value)
        {
            value.Header = "XML Conversation";

            return value;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Uri baseAddress = new Uri("http://localhost:8080/");
            string serviceName = "SimpleService";

            using (WebServiceHost webHost = new WebServiceHost(typeof(SimpleService), baseAddress))
            {
                ServiceEndpoint sp = webHost.AddServiceEndpoint(typeof(SimpleInterface), new WebHttpBinding(), serviceName);
                //ServiceDebugBehavior sdb = webHost.Description.Behaviors.Find();
                //sdb.HttpHelpPageEnabled = false;

                webHost.Open();

                Console.WriteLine("Host Opened");
                Console.ReadLine();

                webHost.Close();

                Console.WriteLine("Host Closed");
            }
        }
    }
}


Some fiddler shots below