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.Security.Cryptography;
using System.Text;

namespace Aadhaar.Data.Util
{
    /// <summary>
    /// Helper class to generate a machine key for use in the application's config file to override the default key used by the provider.
    /// </summary>
    internal static class KeyCreator
    {
        #region Main for Testing
        //public static void main(string[] args)
        //{
        //    string decryptionKey = CreateKey(Convert.ToInt32(args[1]));
        //    string validationKey = CreateKey(Convert.ToInt32(args[2]));
        //    Console.WriteLine("<machinekey validationkey=\"{0}\" decryptionkey=\"{1}\" validation=\"sha1\"/>", validationKey, decryptionKey);
        //}
        #endregion Main for Testing

        #region Operations
        /// <summary>
        /// Creates a key based on the indicated size.
        /// </summary>
        /// <param name="numBytes">size of the key.</param>
        /// <returns>Generated key of the specified length.</returns>
        public static string CreateKey(int numBytes)
        {
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
            byte[] buff = new byte[numBytes];
            rng.GetBytes(buff);
            return BytesToHexString(buff);
        }
        /// <summary>
        /// Converts the given byte array to a hex string representation.
        /// </summary>
        /// <param name="bytes">the data to convert.</param>
        /// <returns>Hexadecimal string representation of the given byte array./</returns>
        public static string BytesToHexString(byte[] bytes)
        {
            StringBuilder hexString = new StringBuilder(64);
            for (int counter = 0; counter < bytes.Length; counter++)
            {
                hexString.Append(string.Format("{0:X2}", bytes[counter]));
            }
            return hexString.ToString();
        }
        #endregion Operations
    }
}

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