-
Notifications
You must be signed in to change notification settings - Fork 3
/
EncryptedFileCache.cs
66 lines (61 loc) · 2.6 KB
/
EncryptedFileCache.cs
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
using System.IO;
using System.Security.Cryptography;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
// see https://github.com/AzureAD/azure-activedirectory-library-for-dotnet/wiki/Token-cache-serialization
namespace PlannerExAndImport
{
class EncryptedFileCache : TokenCache
{
private static readonly object fileLock = new object();
private string cacheFilePath;
// Initializes the cache against a local file.
// If the file is already present, it loads its content in the ADAL cache
public EncryptedFileCache(string filePath = @".\TokenCache.dat")
{
cacheFilePath = filePath;
this.AfterAccess = AfterAccessNotification;
this.BeforeAccess = BeforeAccessNotification;
lock (fileLock)
{
this.Deserialize(File.Exists(cacheFilePath) ?
ProtectedData.Unprotect(File.ReadAllBytes(cacheFilePath), null,
DataProtectionScope.CurrentUser)
: null);
}
}
// Empties the persistent store.
public override void Clear()
{
base.Clear();
File.Delete(cacheFilePath);
}
// Triggered right before ADAL needs to access the cache.
// Reload the cache from the persistent store in case it changed since the last access.
void BeforeAccessNotification(TokenCacheNotificationArgs args)
{
lock (fileLock)
{
this.Deserialize(File.Exists(cacheFilePath) ?
ProtectedData.Unprotect(File.ReadAllBytes(cacheFilePath), null,
DataProtectionScope.CurrentUser)
: null);
}
}
// Triggered right after ADAL accessed the cache.
void AfterAccessNotification(TokenCacheNotificationArgs args)
{
// if the access operation resulted in a cache update
if (this.HasStateChanged)
{
lock (fileLock)
{
// reflect changes in the persistent store
File.WriteAllBytes(cacheFilePath, ProtectedData.Protect(this.Serialize(),
null, DataProtectionScope.CurrentUser));
// once the write operation took place, restore the HasStateChanged bit to false
this.HasStateChanged = false;
}
}
}
}
}