-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSession.cs
More file actions
166 lines (152 loc) · 4.68 KB
/
Session.cs
File metadata and controls
166 lines (152 loc) · 4.68 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using System;
using System.Collections.Generic;
using System.Text.Json;
using Microsoft.AspNetCore.Http;
namespace Saber.Core
{
public class Session : IDisposable
{
public static int ExpiresInMinutes { get; set; } = 20;
public static string CookieName { get; set; } = "Saber";
private HttpContext _context { get; set; }
private string _key { get; set; }
private Dictionary<string, string> _data { get; set; }
private bool _changed { get; set; } = false;
private bool _tryget { get; set; } = false;
private bool _isnew { get; set; } = false;
public static Session Create(HttpContext context)
{
var key = context.Request.Cookies.ContainsKey(CookieName) ?
context.Request.Cookies[CookieName] : "";
return new Session(context, key);
}
public Session(HttpContext context, string key = "")
{
_context = context;
if(key == "")
{
//generate new key
_key = NewId(64);
}
else
{
_key = key;
}
}
public string Get(string key)
{
if(_data == null)
{
//load data from SQL
if(_tryget == false)
{
try
{
_data = Delegates.Session.Get(_key, ExpiresInMinutes);
}
catch (Exception) { }
_tryget = true;
}
}
if (_data != null && _data.ContainsKey(key))
{
return _data[key];
}
return null;
}
public void Set(string key, string value)
{
//load data from SQL
if (_data == null)
{
if (_tryget == false)
{
try
{
_data = Delegates.Session.Get(_key, ExpiresInMinutes);
}
catch (Exception) { }
_tryget = true;
}
if(_data == null){
_data = new Dictionary<string, string>();
_isnew = true;
}
}
if (_data.ContainsKey(key))
{
if(_data[key] != value)
{
_data[key] = value;
_changed = true;
}
}
else
{
_data.Add(key, value);
_changed = true;
}
}
public void Remove(string key)
{
if (_data.ContainsKey(key))
{
_data.Remove(key);
}
}
public void Dispose()
{
if(_changed == true)
{
//save session to SQL
Delegates.Session.Set(_key, Serialize, ExpiresInMinutes);
//update cookie
if (_context.Response.HasStarted == false && _isnew)
{
var options = new CookieOptions()
{
Expires = DateTime.Now.AddMinutes(ExpiresInMinutes)
};
if (App.CookiesUseSameSiteNone == true)
{
options.SameSite = SameSiteMode.None;
options.Secure = true;
}
_context.Response.Cookies.Append(CookieName, _key, options);
}
}
}
private string Serialize
{
get
{
return JsonSerializer.Serialize(_data).Replace("\\u0022", "\\\"");
}
}
private static string NewId(int length = 3)
{
string result = "";
for (var x = 0; x <= length - 1; x++)
{
int type = new Random().Next(1, 3);
int num;
switch (type)
{
case 1: //a-z
num = new Random().Next(0, 26);
result += (char)('a' + num);
break;
case 2: //A-Z
num = new Random().Next(0, 26);
result += (char)('A' + num);
break;
case 3: //0-9
num = new Random().Next(0, 9);
result += (char)('1' + num);
break;
}
}
return result;
}
}
}