-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathADALTokenCache.cs
More file actions
101 lines (93 loc) · 4.08 KB
/
ADALTokenCache.cs
File metadata and controls
101 lines (93 loc) · 4.08 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using DotNetNuke.Entities.Portals;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System;
using System.Data.Entity;
using System.Linq;
namespace LinkWise.Modules.OneDrive
{
public class ADALTokenCache : TokenCache
{
private ApplicationDbContext db = new ApplicationDbContext();
string User;
UserTokenCache Cache;
string cacheKey = "#O365:pid=" + PortalSettings.Current.PortalId.ToString() + ":uid=" + PortalSettings.Current.UserId.ToString();
// constructor
public ADALTokenCache(string user)
{
// associate the cache to the current user of the web app
User = user;
this.AfterAccess = AfterAccessNotification;
this.BeforeAccess = BeforeAccessNotification;
this.BeforeWrite = BeforeWriteNotification;
// look up the entry in the DB
//Cache = db.UserTokenCacheList.FirstOrDefault(c => c.webUserUniqueId == User);
Cache = db.GetUserTokenCacheList(PortalSettings.Current.UserId.ToString());
// place the entry in memory
this.Deserialize((Cache == null) ? null : Cache.cacheBits);
}
// clean up the DB
public override void Clear()
{
base.Clear();
db.RemoveUserTokenCacheList(cacheKey);
//foreach (var cacheEntry in db.UserTokenCacheList)
//db.UserTokenCacheList.Remove(cacheEntry);
//db.SaveChanges();
}
// Notification raised before ADAL accesses the cache.
// This is your chance to update the in-memory copy from the DB, if the in-memory version is stale
void BeforeAccessNotification(TokenCacheNotificationArgs args)
{
if (Cache == null)
{
// first time access
//Cache = db.UserTokenCacheList.FirstOrDefault(c => c.webUserUniqueId == User);
Cache = db.GetUserTokenCacheList(cacheKey);
}
else
{ // retrieve last write from the DB
//var status = from e in db.UserTokenCacheList
// where (e.webUserUniqueId == User)
// select new
// {
// LastWrite = e.LastWrite
// };
var status = db.GetUserTokenCacheList(cacheKey);
// if the in-memory copy is older than the persistent copy
//if (status.First().LastWrite > Cache.LastWrite)
if (status.LastWrite > Cache.LastWrite)
//// read from from storage, update in-memory copy
{
//Cache = db.UserTokenCacheList.FirstOrDefault(c => c.webUserUniqueId == User);
Cache = db.GetUserTokenCacheList(cacheKey);
}
}
this.Deserialize((Cache == null) ? null : Cache.cacheBits);
}
// Notification raised after ADAL accessed the cache.
// If the HasStateChanged flag is set, ADAL changed the content of the cache
void AfterAccessNotification(TokenCacheNotificationArgs args)
{
// if state changed
if (this.HasStateChanged)
{
Cache = new UserTokenCache
{
UserTokenCacheId = PortalSettings.Current.UserId,
webUserUniqueId = User,
cacheBits = this.Serialize(),
LastWrite = DateTime.Now
};
//// update the DB and the lastwrite
//db.Entry(Cache).State = Cache.UserTokenCacheId == 0 ? EntityState.Added : EntityState.Modified;
//db.SaveChanges();
db.SetUserTokenCacheList(cacheKey, Cache);
this.HasStateChanged = false;
}
}
void BeforeWriteNotification(TokenCacheNotificationArgs args)
{
// if you want to ensure that no concurrent write take place, use this notification to place a lock on the entry
}
}
}