예제 #1
0
        public IndexModule()
        {
            Get["/"] = parameters =>
            {
                dynamic model = new ExpandoObject();
                model.loggedIn = Session["loggedIn"];
                model.username = Session["username"];
                return(View["index", model]);
            };

            Get["/enable"] = parameters =>
            {
                Session["para"] = "YES";
                return("set.");
            };
            Get["/disable"] = parameters =>
            {
                Request.Session["para"] = "NO";
                return("set.");
            };
            Get["/what"] = _ =>
            {
                string output = "KEY NOT FOUND";
                try
                {
                    output = Request.Session["para"].ToString();
                } catch (Exception exc)
                {
                }
                return(output);

                System.Security.Cryptography.SHA256Managed c = new System.Security.Cryptography.SHA256Managed();
                c.ComputeHash(System.Text.Encoding.UTF8.GetBytes("asdf"), 0, System.Text.Encoding.UTF8.GetByteCount("asdf"));
            };
            Get["/count"] = _ =>
            {
                try
                {
                    Session["counter"] = (int)Session["counter"] + 1;
                } catch (Exception exc)
                {
                    Session["counter"] = 1;
                }
                return(Session["counter"].ToString());
            };
            Get["/test"] = parameters =>
            {
                string s = "";
                foreach (var x in Session)
                {
                    s += x + "<br />";
                }
                return(s);

                return(Request.Session == Session ? "y" : "n");
            };
            //Get["/{whatever*}"] = _ => Response.AsRedirect("/");
            Get["/(?:(?i)cv(\\.pdf)?)"] = _ => Response.AsFile("Content/cv.pdf");
            Before.AddItemToEndOfPipeline(ctx =>
            {
                Console.WriteLine("endofpipeline");
                var sesh = ctx.Request.Session;
                if (sesh.Any(x => x.Key == "initialized"))
                {
                    return(null);
                }
                Console.WriteLine("Setting session...");
                sesh["initialized"] = true;
                sesh["username"]    = "******";
                sesh["loggedIn"]    = false;

                return(null);
            });

            Get["/users/exists/{usr}"] = ctx => UserDatabase.UserExists(ctx.usr) ? "true" : "false";
            Post["/users/login"]       = ctx =>
            {
                var username = Request.Form.username.ToString();
                var password = Request.Form.password.ToString();
                System.Console.WriteLine(username);
                System.Console.WriteLine(password);

                if (!UserDatabase.UserExists(username))
                {
                    return("username not known.");
                }
                if (!UserDatabase.AttemptLogin(username, password))
                {
                    return("wrong password.");
                }
                Session["username"] = username;
                Session["loggedIn"] = true;

                return(Response.AsRedirect("/"));
            };
            Post["/users/add"] = ctx =>
            {
                var username = Request.Form.username.ToString();
                var password = Request.Form.password.ToString();
                System.Console.WriteLine(username);
                System.Console.WriteLine(password);

                if (UserDatabase.UserExists(username))
                {
                    return("username already exists.");
                }
                UserDatabase.AddUser(username, password);
                UserDatabase.AttemptLogin(username, password);
                Session["username"] = username;
                Session["loggedIn"] = true;

                return(Response.AsRedirect("/"));
            };
            Post["/users/logout"] = _ =>
            {
                Session["loggedIn"] = false;
                Session["username"] = "******";
                return(Response.AsRedirect("/"));
            };
        }