Click here to Skip to main content
15,886,578 members
Articles / Security / Encryption

WCF .NET 4.0 Console Hosted Json Rest Secure HTTP Web Service with Authentication and Roles

Rate me:
Please Sign up or sign in to vote.
4.98/5 (20 votes)
7 Dec 2011CPOL9 min read 92.3K   3.4K   50  
A RESTful web service using the newest .NET platform features including automatic serialization/deserialization of complex types.
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using System.Security.Permissions;
using log4net;
using System.IO;
using System.Runtime.Serialization;

namespace WtfService
{
    // Start the service and browse to http://<machine_name>:<port>/Service1/help to view the service's generated help page
    // NOTE: By default, a new instance of the service is created for each call; change the InstanceContextMode to Single if you want
    // a single instance of the service to process all calls.	
    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    // NOTE: If the service is renamed, remember to update the global.asax.cs file
    public class WtfSvc
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(WtfSvc));

        /// <summary>
        /// Demonstrates http get where parameters are passed in the URL.
        /// Also requires the user to be in the role of 'WtfUser'.
        /// </summary>
        /// <param name="firstName"></param>
        /// <param name="lastName"></param>
        /// <returns></returns>
        [WebGet(UriTemplate = "HelloWorld/{firstName}/{lastName}")]
        [PrincipalPermission(SecurityAction.Demand, Role = "WtfUser")]
        public string HelloWorld(string firstName, string lastName)
        {
            return string.Format("Hello {0} {1}", firstName, lastName);
        }

        /// <summary>
        /// Demonstrates http post where request body contains two parameters of simple type.
        /// Also requires the user to be in the role of 'WtfUser'
        /// </summary>
        /// <param name="firstName">comes from the json content of the body of the post e.g. "{\"firstName\" : \"John\", \"lastName\" : \"Doe\" } }"</param>
        /// <param name="lastName">comes from the json content of the body of the post e.g. "{\"firstName\" : \"John\", \"lastName\" : \"Doe\" } }"</param>
        /// <returns></returns>
        [WebInvoke(Method = "POST", UriTemplate = "HelloWorldPostSimple")]
        [PrincipalPermission(SecurityAction.Demand, Role = "WtfUser")]
        public string HelloWorldPostSimple(string firstName, string lastName)
        {
            return string.Format("Hello {0} {1}", firstName, lastName);
        }

        /// <summary>
        /// Demonstrates http post where request body contains the json string representing an object of type Person.
        /// Also requires the user to be in the role of 'WtfUser'
        /// </summary>
        /// <param name="person">comes from the json content of the body of the post e.g. "{\"person\":{\"FirstName\" : \"John\", \"LastName\" : \"Doe\" } }"</param>
        /// <returns></returns>
        [WebInvoke(Method = "POST", UriTemplate = "HelloWorldPostComplex")]
        [PrincipalPermission(SecurityAction.Demand, Role = "WtfUser")]
        public string HelloWorldPostComplex(Person person)
        {
            return string.Format("Hello {0} {1}", person.FirstName, person.LastName);
        }

    }

    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

}

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
Software Developer (Senior) Self Employed
Australia Australia
Has a background in Finance and Software development.
Has worked as a RAD developer for the likes of Credit-Suisse and Westpac Banking corporation.
Currently self-employed working on some personal projects.

Comments and Discussions