aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio_22/Tango.Portal.Chat.Web/Utils/SessionUtils.cs
blob: 91f118e0d56d8d87f2eefd546f67f0f76497ae17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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;
        }
    }
}