forked from xamarin/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSessionFeature.cs
More file actions
63 lines (54 loc) · 1.72 KB
/
Copy pathSessionFeature.cs
File metadata and controls
63 lines (54 loc) · 1.72 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
using System;
using ServiceStack.Common;
using ServiceStack.ServiceHost;
using ServiceStack.WebHost.Endpoints;
namespace ServiceStack.ServiceInterface
{
/// <summary>
/// Configure ServiceStack to have ISession support
/// </summary>
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)
{
res.CreateTemporarySessionId(req);
}
if (req.GetCookieValue(PermanentSessionId) == null)
{
res.CreatePermanentSessionId(req);
}
});
}
public static string GetPermanentSessionId(this IHttpRequest httpReq)
{
return httpReq.GetItemOrCookie(PermanentSessionId);
}
public static string GetTemporarySessionId(this IHttpRequest httpReq)
{
return httpReq.GetItemOrCookie(SessionId);
}
public static string CreatePermanentSessionId(this IHttpResponse res, IHttpRequest req)
{
var sessionId = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
res.SetPermanentCookie(PermanentSessionId, sessionId);
req.Items[PermanentSessionId] = sessionId;
return sessionId;
}
public static string CreateTemporarySessionId(this IHttpResponse res, IHttpRequest req)
{
var sessionId = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
res.SetSessionCookie(SessionId, sessionId);
req.Items[SessionId] = sessionId;
return sessionId;
}
}
}