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
67 lines (57 loc) · 2.16 KB
/
SessionFeature.cs
File metadata and controls
67 lines (57 loc) · 2.16 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
using System;
using System.Web;
using ServiceStack.Common.Utils;
using ServiceStack.Common.Web;
using ServiceStack.ServiceHost;
using ServiceStack.ServiceInterface.Auth;
using ServiceStack.WebHost.Endpoints;
namespace ServiceStack.ServiceInterface
{
public class SessionFeature : IPlugin
{
public const string OnlyAspNet = "Only ASP.NET Requests accessible via Singletons are supported";
public const string SessionId = "ss-id";
public const string PermanentSessionId = "ss-pid";
public const string SessionOptionsKey = "ss-opt";
public const string XUserAuthId = HttpHeaders.XUserAuthId;
private static bool alreadyConfigured;
public void Register(IAppHost appHost)
{
if (alreadyConfigured) return;
alreadyConfigured = true;
//Add permanent and session cookies if not already set.
appHost.RequestFilters.Add(AddSessionIdToRequestFilter);
}
public static void AddSessionIdToRequestFilter(IHttpRequest req, IHttpResponse res, object requestDto)
{
if (req.GetCookieValue(SessionId) == null)
{
res.CreateTemporarySessionId(req);
}
if (req.GetCookieValue(PermanentSessionId) == null)
{
res.CreatePermanentSessionId(req);
}
}
public static string GetSessionId()
{
if (HttpContext.Current == null)
throw new NotImplementedException(OnlyAspNet);
return HttpContext.Current.Request.ToRequest().GetSessionId();
}
public static void CreateSessionIds()
{
if (HttpContext.Current != null)
{
HttpContext.Current.Response.ToResponse()
.CreateSessionIds(HttpContext.Current.Request.ToRequest());
return;
}
throw new NotImplementedException(OnlyAspNet);
}
public static string GetSessionKey(string sessionId)
{
return IdUtils.CreateUrn<IAuthSession>(sessionId);
}
}
}