Click here to Skip to main content
15,896,557 members
Articles / Programming Languages / XML

Using InsomniaServer to Build a Web-interface for your Application

Rate me:
Please Sign up or sign in to vote.
4.80/5 (11 votes)
15 Jul 2011CPOL3 min read 83.7K   1.2K   41  
InsomniaServer enables you to add a fully-featured, customizable webserver to your projects. See how it works.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;

using InsomniaSoftware.Server;
using InsomniaSoftware.Server.Sources;


namespace SimpleLogin
{
    class Login
    {
        //Allowed users
        static LinkedList<User> users = new LinkedList<User>();

        //Handle to Form1
        static Form1 mainForm;


        /// <summary>
        /// Clears possibly existing users and loads new ones
        /// </summary>
        /// <param name="usersFile">XML file which stores the new users</param>
        public static void LoadUsers(string usersFile)
        {
            users.Clear();

            XmlTextReader xmlReader = new XmlTextReader(usersFile);
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlReader);

            XmlNode curNode = xmlDoc.FirstChild;
            while (curNode != null)
            {
                if (curNode.Name.ToLower() == "login")
                {
                    XmlNode curSubNode = curNode.FirstChild;
                    while (curSubNode != null)
                    {
                        User newUser = new User();
                        if (curSubNode.Name.ToLower() == "user")
                        {
                            foreach (XmlNode attribute in curSubNode.ChildNodes)
                            {
                                if (attribute.Name.ToLower() == "name")
                                    newUser.name = attribute.InnerText;

                                if (attribute.Name.ToLower() == "pw")
                                    newUser.pw = attribute.InnerText;
                            }
                        }
                        users.AddLast(newUser);

                        curSubNode = curSubNode.NextSibling;
                    }
                }
                curNode = curNode.NextSibling;
            }
            xmlReader.Close();
        }


        /// <summary>
        /// Adds all needed sources to the server
        /// </summary>
        /// <param name="server">Server handle</param>
        /// <param name="callingForm">Form which contains the list box to be filled with login data</param>
        public static void AddSources(InsomniaServer server, Form1 callingForm)
        {
            mainForm = callingForm;

            server.fileSystem.AddSource("index.html", FileSource.GetSource, "../pages/index.html");
            server.fileSystem.AddSource("dologin.html", GetDoLogin, null);
            server.fileSystem.AddSource("sec/*", GetMembersonlyContent, null);

            server.fileSystem.GetNode("sec").SetSecurityValidator(SecurityValidator, null, InternalRedirectSource.GetSource, "/index.html");
        }


        /// <summary>
        /// Gets the "dologin.html" source and redirects the request
        /// </summary>
        public static ObjectType GetDoLogin(string subPath, HttpRequest request, object userData, out Source source)
        {
            string passedName = null;
            string passedPW = null;
            string assignedID = null;
            try
            {
                passedName = request.GetParameter("username").ToString();
                passedPW = request.GetParameter("password").ToString();
            }
            catch { }

            bool matchFound = false;
            try
            {
                if (passedName != null && passedPW != null)
                {
                    try
                    {
                        foreach (User curUser in users)
                        {
                            if (curUser.name == passedName && curUser.pw == passedPW)
                            {
                                matchFound = true;
                                break;
                            }
                        }
                    }
                    catch { }
                }

                if (matchFound)
                {
                    //Create a new session, to store user's data
                    Session sess = request.server.sessionManager.RegisterSession();
                    sess.SetStringValue("username", passedName);
                    sess.SetValue("ip", request.remoteEndPoint.Address);
                    assignedID = sess.id;

                    //Redirect request to the secured area
                    source = new MovedPermanentlySource("/sec/index.html?sid=" + sess.id);
                    return ObjectType.File;
                }
                else
                {
                    //Login failed - send failed response
                    source = FileSource.Get("../pages/loginfailed.html");
                    return ObjectType.File;
                }
            }
            finally
            {
                if (mainForm != null)
                {
                    //Write login data and result to the form's list box
                    mainForm.Invoke(new Form1.AddListBoxItemDelegate(mainForm.listBoxLogins.Items.Add),
                                    DateTime.Now.ToShortDateString() + "   " + DateTime.Now.ToLongTimeString() + ": " +
                                    (passedName != null ? passedName : "[null]") + " / " + (passedPW != null ? passedPW : "[null]") + "  " +
                                    (matchFound ? ("SUCCEEDED (" + assignedID + ")") : "FAILED"));
                }
            }
        }


        /// <summary>
        /// Gets a source for the secured area
        /// </summary>
        public static ObjectType GetMembersonlyContent(string subPath, HttpRequest request, object userData, out Source source)
        {
            source = null;

            //Create full local path of the requested source
            string fullPath = "../pages/sec/" + subPath;

            if (File.Exists(fullPath))
            {
                if (subPath.EndsWith(".html") || subPath.EndsWith(".htm"))
                {
                    Session sess = null;
                    try
                    {
                        //Load user's session
                        sess = request.server.sessionManager.GetSession(request.GetParameter("sid").ToString());
                    }
                    catch { }

                    //Replace placeholders in html pages
                    StreamReader reader = new StreamReader(fullPath, Encoding.ASCII);
                    string pageContent = reader.ReadToEnd();
                    reader.Dispose();

                    if (sess != null)
                    {
                        pageContent = pageContent.Replace("<!--#USER#-->", sess.GetStringValue("username"));
                        pageContent = pageContent.Replace("<!--#SID#-->", sess.id);
                        pageContent = pageContent.Replace("<!--#IP#-->", sess.GetValue("ip").ToString());
                    }
                    source = new BinarySource(Encoding.ASCII.GetBytes(pageContent), ContentType.FromFileName(subPath));
                }
                else
                    source = FileSource.Get(fullPath);
                return ObjectType.File;
            }
            else
            {
                if (Directory.Exists(fullPath))
                    return ObjectType.Folder;
                else
                    return ObjectType.NotFound;
            }
        }


        /// <summary>
        /// Checks whether a request belongs to a logged in user
        /// </summary>
        public static bool SecurityValidator(object validatorUserData, string subPath, HttpRequest request, object sourceUserData)
        {
            try
            {
                Session sess = request.server.sessionManager.GetSession(request.GetParameter("sid").ValueToString());
                if (!sess.GetValue("ip").Equals(request.remoteEndPoint.Address))
                    return false;
                return true;
            }
            catch
            {
                return false;
            }
        }


        struct User
        {
            public string name;
            public string pw;
        }
    }
}

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
Student
Germany Germany
I was born in 1987. Unfortunately too late to experience the real rise of the PC. But fortunately late enough to enjoy things like MS's .net during my school time Wink | ;)

From the time when some relative taught me a little BASIC under MS DOS, I loved to tell computers what to do - even though my real start in programming was around the age of 16.

At the moment, I am studying Software Engineering at University of Augsburg, always hoping to find time to design and program.
Besides, I like meeting friends, spent time with my girlfriend and enjoy life Smile | :)

Comments and Discussions