From 8cb745063d64f02f76d0dc4a119e8f3233e63d85 Mon Sep 17 00:00:00 2001 From: Tracy Pearson Date: Tue, 22 Nov 2022 16:53:39 -0500 Subject: [PATCH] Using sessions is troubled. --- .../Controllers/HomeController.cs | 4 +- .../Controllers/SettingsController.cs | 15 ++++++++ .../Extensions/SessionExtensions.cs | 19 ++++++++++ WebPostPartialJsonObject/Startup.cs | 9 ++++- .../Views/Settings/Index.cshtml | 38 +++++++++++++++++++ .../WebPostPartialJsonObject.csproj | 5 +++ 6 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 WebPostPartialJsonObject/Controllers/SettingsController.cs create mode 100644 WebPostPartialJsonObject/Extensions/SessionExtensions.cs create mode 100644 WebPostPartialJsonObject/Views/Settings/Index.cshtml diff --git a/WebPostPartialJsonObject/Controllers/HomeController.cs b/WebPostPartialJsonObject/Controllers/HomeController.cs index 7ed30a3..ca9ab0d 100644 --- a/WebPostPartialJsonObject/Controllers/HomeController.cs +++ b/WebPostPartialJsonObject/Controllers/HomeController.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading.Tasks; +using WebPostPartialJsonObject.Extensions; using WebPostPartialJsonObject.Models; namespace WebPostPartialJsonObject.Controllers @@ -25,7 +26,8 @@ namespace WebPostPartialJsonObject.Controllers [HttpPost, Consumes("application/json")] public IActionResult Index([FromBody] ExpectedViewModel data) { - return View(); + HttpContext.Session.SetObject("KioskSettings", data); + return RedirectToAction("Index", "Settings"); } public IActionResult Privacy() diff --git a/WebPostPartialJsonObject/Controllers/SettingsController.cs b/WebPostPartialJsonObject/Controllers/SettingsController.cs new file mode 100644 index 0000000..a3a9512 --- /dev/null +++ b/WebPostPartialJsonObject/Controllers/SettingsController.cs @@ -0,0 +1,15 @@ +using Microsoft.AspNetCore.Mvc; +using WebPostPartialJsonObject.Extensions; +using WebPostPartialJsonObject.Models; + +namespace WebPostPartialJsonObject.Controllers +{ + public class SettingsController : Controller + { + public IActionResult Index() + { + ExpectedViewModel data = HttpContext.Session.GetObject("KioskSettings"); + return View(data); + } + } +} diff --git a/WebPostPartialJsonObject/Extensions/SessionExtensions.cs b/WebPostPartialJsonObject/Extensions/SessionExtensions.cs new file mode 100644 index 0000000..2aac606 --- /dev/null +++ b/WebPostPartialJsonObject/Extensions/SessionExtensions.cs @@ -0,0 +1,19 @@ +using Microsoft.AspNetCore.Http; +using Newtonsoft.Json; +using System.Text.Json.Serialization; + +namespace WebPostPartialJsonObject.Extensions +{ + public static class SessionExtensions + { + public static void SetObject(this ISession session, string key, object value) + { + session.SetString(key, JsonConvert.SerializeObject(value)); + } + public static T GetObject(this ISession session, string key) + { + var value = session.GetString(key); + return value == null ? default : JsonConvert.DeserializeObject(value); + } + } +} \ No newline at end of file diff --git a/WebPostPartialJsonObject/Startup.cs b/WebPostPartialJsonObject/Startup.cs index 28039dd..6d98d11 100644 --- a/WebPostPartialJsonObject/Startup.cs +++ b/WebPostPartialJsonObject/Startup.cs @@ -22,8 +22,12 @@ namespace WebPostPartialJsonObject // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { - services.AddControllersWithViews(); - } + services.AddSession(options => + { + options.IdleTimeout = TimeSpan.FromHours(2.0); + }); + services.AddControllersWithViews().AddSessionStateTempDataProvider(); + } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) @@ -37,6 +41,7 @@ namespace WebPostPartialJsonObject app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); + app.UseSession(); app.UseRouting(); diff --git a/WebPostPartialJsonObject/Views/Settings/Index.cshtml b/WebPostPartialJsonObject/Views/Settings/Index.cshtml new file mode 100644 index 0000000..576cac4 --- /dev/null +++ b/WebPostPartialJsonObject/Views/Settings/Index.cshtml @@ -0,0 +1,38 @@ +@model WebPostPartialJsonObject.Models.ExpectedViewModel + +@{ + ViewData["Title"] = "Index"; +} + +

Index

+ +

ExpectedViewModel

+
+
+
+
+
+
+ + + +
+
+ + + +
+
+ +
+
+
+
+ +
+ Back to List +
+ +@section Scripts { + @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} +} diff --git a/WebPostPartialJsonObject/WebPostPartialJsonObject.csproj b/WebPostPartialJsonObject/WebPostPartialJsonObject.csproj index 92605c5..f81f1b6 100644 --- a/WebPostPartialJsonObject/WebPostPartialJsonObject.csproj +++ b/WebPostPartialJsonObject/WebPostPartialJsonObject.csproj @@ -4,4 +4,9 @@ netcoreapp3.1 + + + + +