What is MiniState?

MiniState is an attempt to provide simple abstraction layer to reading and writing state information like HTTP cookies to minimise cookie size and increase the quality of code and security.

Using MiniState

Getting Started

The easiest way to get started using MiniState:
  • Download the latest release or source code.
  • Add the RadicalResearch.MiniState.dll and RadicalResearch.MiniState.Cookies.dll assemblys to your project.
  • Derive from the CookieSettingStoreBase, adding the properties you want to store in the HTTP cookie.

Working With MiniState

Creating a cookie

To use MiniState you'll need to create an object that derives from CookieSettingStoreBase.

Then add properties to your object for each of the settings you want to store in the cookie.

Either decorate the properties with SettingDescriptorAttributes or override InitalizeSettingDescriptors to add descriptors for these properties.

This is a simple example object the derives from CookieSettingStoreBase. It has a single string property that will be stored as "a" in the cookie and protected from tampering by hash validation.
    /// <summary>Example cookie setting store object</summary>
    public class ExampleCookie : CookieSettingStoreBase
        #region Constants and Fields

        /// <summary>The name of the cookie</summary>
        private const string CookieName = "a";

        /// <summary>Version number to assign to the cookie.</summary>
        private const short VersionNumber = 100;

        /// <summary>Expire the validity of the cookie in 30 days</summary>
        private static readonly TimeSpan ValidLifetime = new TimeSpan(30, 0, 0, 0);


        #region Constructors and Destructors

        /// <summary>
        ///   Initializes a new instance of the <see cref = "ExampleCookie" /> class.
        /// </summary>
        public ExampleCookie()
            : base(CookieName, ValidLifetime, VersionNumber)
            // Expire in 1 day
            this.Cookie.Expires = DateTime.UtcNow.AddDays(1);


        #region Properties

        /// <summary>
        /// Gets and sets the display name.
        /// </summary>
        [SettingDescriptor("DisplayName", "a", true)]
        public string DisplayName
            get { return base["DisplayName"] as string; }
            set { base["DisplayName"] = value; }


Setting descriptor attributes

Decorating a property with a SettingDescriptorAttributes creates a SettingDescriptors associated with the property that is used to read and write the value to the HTTP cookie.

Defining Descriptors

If you don't want to use the declarative method of specifying descriptors with SettingDescriptorAttributes you can add Descriptors by overriding the InitalizeSettingDescriptors method and calling AddSettingDescriptor()

        protected override void InitalizeSettingDescriptors()
            // Add a descriptor for the "MySetting" property that will be stored as "x" in the cookie.
            this.AddSettingDescriptor("MySetting", new StringSettingDescriptor("x", true));

Last edited Dec 7, 2011 at 10:01 AM by radicalresearch, version 4


No comments yet.