From f4f4f5b2700a58df969957425461ca6cfb0fc85a Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Mon, 5 Aug 2024 00:16:54 -0400 Subject: [PATCH] figuring out how to use cookies to store session state --- .../MochaWebApplication.cs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/mocha-dotnet/src/app/Mocha.ServerApplication/MochaWebApplication.cs b/mocha-dotnet/src/app/Mocha.ServerApplication/MochaWebApplication.cs index 64d145f..2ed16a6 100644 --- a/mocha-dotnet/src/app/Mocha.ServerApplication/MochaWebApplication.cs +++ b/mocha-dotnet/src/app/Mocha.ServerApplication/MochaWebApplication.cs @@ -110,13 +110,15 @@ font-weight: lighter; { base.OnInit(e); - if (e.Request.Form.ContainsKey("username") && e.Request.Form.ContainsKey("password")) + string tenantName = e.Context.Request.GetExtraData("TenantName"); + if (e.Context.Request.Form.ContainsKey("username") && e.Context.Request.Form.ContainsKey("password")) { - if (e.Request.Form["username"] == "mocha") + if (e.Context.Request.Form["username"] == "mocha") { - if (e.Request.Form["password"] == "testing") + if (e.Context.Request.Form["password"] == "testing") { - e.Response.Redirect("~/d/home.htmld"); + e.Context.Session[tenantName + ".UserToken"] = (new Guid()).ToString("b"); + e.Context.Response.Redirect(String.Format("~/{0}/d/home.htmld", tenantName)); } } } @@ -181,6 +183,8 @@ font-weight: lighter; } } + e.Context.Request.SetExtraData("TenantName", tenantName); + TenantHandle tenant = app.Oms.GetTenantByName(tenantName); if (tenant == TenantHandle.Empty) { @@ -214,8 +218,13 @@ font-weight: lighter; // e.Server.Routes.Add(new WebRoute("/{tenant}", new RedirectWebHandler("~/{tenant}/d/home.htmld"))); e.Server.Routes.Add(new WebRoute("/{tenant}/d/home.htmld", new WebHandler(delegate(WebContext ctx) { + string tenantName = ctx.Request.GetExtraData("TenantName"); + if (!ctx.Session.ContainsKey(tenantName + ".UserToken")) + { + ctx.Response.Redirect("~/" + ctx.Request.PathVariables["tenant"] + "/d/login.htmld"); + } + Console.WriteLine("req app path: " + ctx.Request.Path); - ctx.Response.Redirect("~/" + ctx.Request.PathVariables["tenant"] + "/d/login.htmld"); }))); e.Server.Routes.Add(new WebRoute("/{tenant}/d/login.htmld", new RedirectWebHandler("~/madi/authgwy/{tenant}/login.htmld"))); e.Server.Routes.Add(new WebRoute("/madi/authgwy/{tenant}/login.htmld", new LoginWebPage()));