Click here to Skip to main content
15,885,824 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.Text;
using System.Security.Principal;

namespace WtfService
{
    public class WtfPrincipal : IPrincipal
    {
        IIdentity _identity;
        string[] _roles;

        public WtfPrincipal(IIdentity identity)
        {
            _identity = identity;
            LoadUsersRoles();
        }
        
        /// <summary>
        /// we should load up the user's roles from the database here based on username
        /// </summary>
        private void LoadUsersRoles()
        {
            //run query using on db using _identity.Name
            //then put into _role
            _roles = new string[] { "WtfUser" };
        }


        public IIdentity Identity
        {
            get { return _identity; }
        }

        public bool IsInRole(string role)
        {
            if (_roles.Contains(role))
                return true;
            else
                return false;   
        }

        public string[] Roles
        {
            get { return _roles; }
        }

    }
}

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