aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio_22/Tango.Portal.Chat.Web/Controllers/HomeController.cs
blob: 6f1aa5dfae9305a00fcd605d831681a359d08888 (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
37
38
39
40
41
42
43
44
45
46
47
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using System.Text.Json;
using Tango.Portal.Chat.Web.ViewModels;

namespace Tango.Portal.Chat.Web.Controllers
{
    public sealed class HomeController : Controller
    {
        public IActionResult Index(String session)
        {
            if (Debugger.IsAttached)
            {
                HomeViewVM v = new HomeViewVM();
                v.UserName = "debug-user";
                return View(v);
            }

            String loginUrl = "https://twine-srv.com/login";

            if (String.IsNullOrWhiteSpace(session)) return new RedirectResult(loginUrl);

            String decryptedSession = String.Empty;

            try
            {
                decryptedSession = SimpleCryptoHelper.Decrypt(session);
            }
            catch
            {
                return new RedirectResult(loginUrl);
            }

            var template = new { UserName = "", Expires = DateTime.MinValue };

            var sessionUser = JsonSerializer.Deserialize(decryptedSession, template.GetType());
            if (sessionUser == null || (DateTime)(sessionUser as dynamic).Expires < DateTime.UtcNow)
            {
                return new RedirectResult(loginUrl);
            }

            HomeViewVM vm = new HomeViewVM();
            vm.UserName = (sessionUser as dynamic).UserName;
            return View(vm);
        }
    }
}