aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio_22/Tango.Portal.Chat.Web/Utils/SessionUtils.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Software/Visual_Studio_22/Tango.Portal.Chat.Web/Utils/SessionUtils.cs')
-rw-r--r--Software/Visual_Studio_22/Tango.Portal.Chat.Web/Utils/SessionUtils.cs36
1 files changed, 36 insertions, 0 deletions
diff --git a/Software/Visual_Studio_22/Tango.Portal.Chat.Web/Utils/SessionUtils.cs b/Software/Visual_Studio_22/Tango.Portal.Chat.Web/Utils/SessionUtils.cs
new file mode 100644
index 000000000..91f118e0d
--- /dev/null
+++ b/Software/Visual_Studio_22/Tango.Portal.Chat.Web/Utils/SessionUtils.cs
@@ -0,0 +1,36 @@
+using Tango.Portal.Chat.Web.Models;
+
+namespace Tango.Portal.Chat.Web.Utils
+{
+ public class SessionUtils
+ {
+ public static void SetSessionUser(HttpContext context, SessionUser user)
+ {
+ if (context == null) throw new ArgumentNullException(nameof(context));
+ if (user == null) throw new ArgumentNullException(nameof(user));
+
+ var json = System.Text.Json.JsonSerializer.Serialize(user);
+ context.Session.SetString("SessionUser", json);
+ }
+
+ public static SessionUser? GetSessionUser(HttpContext context)
+ {
+ if (context == null) throw new ArgumentNullException(nameof(context));
+
+ var json = context.Session.GetString("SessionUser");
+ if (string.IsNullOrWhiteSpace(json))
+ {
+ return null;
+ }
+
+ return System.Text.Json.JsonSerializer.Deserialize<SessionUser>(json);
+ }
+
+ public static bool IsUserAuthenticated(HttpContext context)
+ {
+ if (context == null) throw new ArgumentNullException(nameof(context));
+ var user = GetSessionUser(context);
+ return user != null;
+ }
+ }
+}