forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMachineKey45CryptoSystem.cs
More file actions
52 lines (44 loc) · 1.58 KB
/
Copy pathMachineKey45CryptoSystem.cs
File metadata and controls
52 lines (44 loc) · 1.58 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
#if !NETSTANDARD2_0
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System.Reflection;
using System.Web;
using System.Web.Security;
namespace ServiceStack.Html.AntiXsrf
{
// Interfaces with the System.Web.MachineKey static class using the 4.5 Protect / Unprotect methods.
internal sealed class MachineKey45CryptoSystem : ICryptoSystem
{
private static readonly string[] _purposes = new string[] { "ServiceStack.Html.AntiXsrf.AntiForgeryToken.v1" };
private static readonly MachineKey45CryptoSystem _singletonInstance = GetSingletonInstance();
public static MachineKey45CryptoSystem Instance
{
get
{
return _singletonInstance;
}
}
private static MachineKey45CryptoSystem GetSingletonInstance()
{
return new MachineKey45CryptoSystem();
}
public string Protect(byte[] data)
{
#if NET_4_0
byte[] rawProtectedBytes = MachineKey.Protect(data, _purposes);
return HttpServerUtility.UrlTokenEncode(rawProtectedBytes);
#else
return HttpServerUtility.UrlTokenEncode(data);
#endif
}
public byte[] Unprotect(string protectedData)
{
#if NET_4_0
byte[] rawProtectedBytes = HttpServerUtility.UrlTokenDecode(protectedData);
return MachineKey.Unprotect(rawProtectedBytes, _purposes);
#else
return HttpServerUtility.UrlTokenDecode(protectedData);
#endif
}
}
}
#endif