This project is read-only.

Sessions

We have no plans on implementing sessions in the web server, since they should be strongly typed and stream lined for each application. But we have included some small helper classes to simplify session handling.

Helper classes

SessionProvider is the first class. It's a class with file based sessions (and it can cache all sessions in memory too). If caching is enabled, it will only write sessions to disk every 20 seconds if they have
been accessed the last minute (to not keep writing dead sessions to disk). SessionProvider works by hooking events in the web server to be able to send the setcookie in responses and load sessions on incoming requests.

The second class is Session,, it's the base class for all session implementations and is used by SessionProvider to load and store sessions.

Building your own session class.

I recommend that you wrap the session provider in your class to simplify session management.

    [Serializable]
    public class WebSession : Session
    {
        private static readonly SessionProvider<WebSession> _sessionProvider = new SessionProvider<WebSession>();

        static WebSession()
        {
            _sessionProvider.Cache = true;
        }

        /// <summary>
        /// Gets currently loaded session
        /// </summary>
        /// <remarks>
        /// Will not create sessions and manage new sessions, but returns a dummy one which is not handled by the provider class.
        /// Use the Create method to get a session that will be maintained by the provider class.
        /// </remarks>
        public static WebSession Current
        {
            get { return _sessionProvider.Current ?? new WebSession(); }
        }

        /// <summary>
        /// Gets or sets first name.
        /// </summary>
        public string FirstName { get; set; }

        /// <summary>
        /// Gets or sets user id.
        /// </summary>
        public int UserId { get; set; }

        /// <summary>
        /// Gets or sets user name.
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// Gets or sets user status level
        /// </summary>
        public UserStatus Status { get; set; }

        /// <summary>
        /// Gets or sets current errors.
        /// </summary>
        public static List<string> Errors { get; set; }

        /// <summary>
        /// Creates a new session and also sets it as the current one.
        /// </summary>
        /// <returns>Created session.</returns>
        public static WebSession Create()
        {
            return _sessionProvider.Create();
        }

        internal static void Start(Server webServer)
        {
            _sessionProvider.Start(webServer);
        }
    }

To use the fields:

Session.Current.UserName = "jonas";

The last thing you need to do is to pass the webserver to the Start method (should only be done once during application startup):
WebSession.Start(myWebServer);

Creating a new session

Here is an example from my user controlller. It will automatically send the set-cookie header when needed.

        public IViewData Login()
        {
            return Render();
        }

        public IActionResult LoginPost()
        {
            string userName = Form["username"];
            string password = Form["password"];
            string rememberMe = Form["rememberme"];

            // authenticate here.

            WebSession.Create();
            WebSession.Current.UserId = 1;
            WebSession.Current.Status = UserStatus.SuperAdmin;


            return RedirectToUri("/user/welcome/");
        }

Last edited Mar 21, 2010 at 6:18 PM by jgauffin, version 10

Comments

No comments yet.