forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSessionFeature.cs
More file actions
47 lines (41 loc) · 1.33 KB
/
SessionFeature.cs
File metadata and controls
47 lines (41 loc) · 1.33 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
using System;
using ServiceStack.Common;
using ServiceStack.ServiceHost;
using ServiceStack.WebHost.Endpoints;
namespace ServiceStack.ServiceInterface
{
public static class SessionFeature
{
public const string SessionId = "ss-id";
public const string PermanentSessionId = "ss-pid";
private static bool alreadyConfigured;
public static void Init(IAppHost appHost)
{
if (alreadyConfigured) return;
alreadyConfigured = true;
//Add permanent and session cookies if not already set.
appHost.RequestFilters.Add((req, res, dto) => {
if (req.GetCookieValue(SessionId) == null)
{
var sessionId = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
res.SetSessionCookie(SessionId, sessionId);
req.Items[SessionId] = sessionId;
}
if (req.GetCookieValue(PermanentSessionId) == null)
{
var permanentId = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
res.SetPermanentCookie(PermanentSessionId, permanentId);
req.Items[PermanentSessionId] = permanentId;
}
});
}
public static string GetPermanentSessionId(this IHttpRequest httpReq)
{
return httpReq.GetItemOrCookie(PermanentSessionId);
}
public static string GetTemporarySessionId(this IHttpRequest httpReq)
{
return httpReq.GetItemOrCookie(SessionId);
}
}
}