Click here to Skip to main content
15,885,278 members
Articles / Web Development / ASP.NET

Extending ASP.NET role based Security with Custom Security Module (Permission Based, Page Level Authorization)

Rate me:
Please Sign up or sign in to vote.
4.80/5 (18 votes)
11 Nov 2011Ms-PL5 min read 107.8K   9.3K   74  
This project intends to extend the default ASP.NET role based Security to include Permission Based / Page Level Authorization Layer. Works with both ASP.NET and ASP.NET MVC. Permission rules to Allow/Deny access to website resources (like "Folder/File.aspx" or "Controller/Action") are stored in DB.
using System;
using System.Collections;
using System.ComponentModel.DataAnnotations;
using System.Web.Security;
using System.Web;
using System.Collections.Generic;
using System.Web.Mvc;

namespace Aadhaar.MVC.Models
{
    public class UsersModel
    {
        #region Fields
        public Guid id;
        public string name;
        public string loweredName;
        public string description { get; set; }
        public string password { get; set; }
        public int passwordFormat { get; set; }
        public string passwordSalt { get; set; }
        public string email;
        public string loweredEmail { get; set; }
        public string passwordQuestion { get; set; }
        public string passwordAnswer { get; set; }
        public string comments { get; set; }
        public bool isApproved { get; set; }
        public bool isLockedOut { get; set; }
        public DateTime creationDate;
        public DateTime lastActivityDate { get; set; }
        public DateTime lastLoginDate { get; set; }
        public DateTime lastLockedOutDate { get; set; }
        public DateTime lastPasswordChangeDate { get; set; }
        public int failedPasswordAttemptCount;
        public DateTime failedPasswordAttemptWindowStart;
        public int failedPasswordAnswerAttemptCount;
        public DateTime failedPasswordAnswerAttemptWindowStart;
        public IList applications { get; set; }
        public IList roles { get; set; }
        #endregion Fields



        #region Operations
        public MembershipUser ToMembershipUser(string providerName)
        {
            return (new MembershipUser(providerName, name, id, email, passwordQuestion, comments, isApproved,
                                       isLockedOut, creationDate, lastLoginDate, lastActivityDate, lastPasswordChangeDate,
                                       lastLockedOutDate));
        }
        public UsersModel FromMembershipUser(MembershipUser mu)
        {
            id =  (Guid)mu.ProviderUserKey;
            name = mu.UserName;
            email = mu.Email;
            passwordQuestion = mu.PasswordQuestion;
            comments = mu.Comment;
            isApproved = mu.IsApproved;
            isLockedOut = mu.IsLockedOut;
            creationDate = mu.CreationDate;
            lastActivityDate = mu.LastActivityDate;
            lastLoginDate = mu.LastLoginDate;
            lastPasswordChangeDate = mu.LastPasswordChangedDate;
            lastLockedOutDate = mu.LastLockoutDate;
            return this;
        }
        #endregion Operations
    }

    public class Permissions
    {

        public int? selectedActionId { get; set; }

        public int? selectedActionIdUser { get; set; }


        public int selectedUserKey { get; set; }
        public string selectedUserId { get; set; }
        public string selectedRoleName { get; set; }

        public IList<Aadhaar.Data.ViewModel.Permission> permissionsList { get; set; }
        public List<SelectListItem> rolesList { get; set; }
        public IList<Aadhaar.Data.ViewModel.Activities> actionsList { get; set; }
        public List<SelectListItem> PermTypes { get; set; }

        public int? PTypeR { get; set; }
        public int? PTypeU { get; set; }

        public Permissions()
        {
            rolesList = new List<SelectListItem>();
            PermTypes = new List<SelectListItem>();
            PermTypes.Add(new SelectListItem { Text = "Read", Value = "0" });
            PermTypes.Add(new SelectListItem { Text = "Write", Value = "1" });
            
        }

        public enum permissionTypes
        {
            ReadOnly = 0, Writable
        };
    }
}

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 Microsoft Public License (Ms-PL)


Written By
Software Developer (Senior)
Singapore Singapore
I love programming, reading, and meditation. I like to explore management and productivity.

Comments and Discussions