Created
April 20, 2015 05:58
-
-
Save anonymous/289b7f6a3423c54e7022 to your computer and use it in GitHub Desktop.
AzureAdalCache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AzureAdalCache : TokenCache | |
{ | |
Guid _companyId; | |
ADALCacheEntry _cache; | |
private readonly IGenericRepository<ADALCacheEntry> _entries; | |
private readonly IUnitOfWork _unitOfWork; | |
public AzureAdalCache(Guid companyId, IGenericRepository<ADALCacheEntry> entries, IUnitOfWork unitOfWork) | |
{ | |
// associate the cache to the current user of the web app | |
_companyId = companyId; | |
this.AfterAccess = AfterAccessNotification; | |
this.BeforeAccess = BeforeAccessNotification; | |
this.BeforeWrite = BeforeWriteNotification; | |
_entries = entries; | |
_unitOfWork = unitOfWork; | |
// look up the entry in the DB | |
_cache = _entries.GetSingleOrDefault(c => c.CompanyId == _companyId); | |
// place the entry in memory | |
this.Deserialize((_cache == null) ? null : _cache.cacheBits); | |
} | |
// clean the db of all tokens associated with the user. | |
public override void Clear() | |
{ | |
base.Clear(); | |
_entries.Delete(x => x.CompanyId == _companyId); | |
_unitOfWork.Commit(); | |
} | |
// 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 = _entries.GetSingleOrDefault(x => x.CompanyId == _companyId); | |
} | |
else | |
{ | |
// retrieve last write from the DB | |
var status = _entries.GetSingleOrDefault(x => x.CompanyId == _companyId); | |
// if the in-memory copy is older than the persistent copy | |
if (status.LastWrite > _cache.LastWrite) | |
//// read from from storage, update in-memory copy | |
{ | |
_cache = _entries.GetSingleOrDefault(x => x.CompanyId == _companyId); | |
} | |
} | |
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 ADALCacheEntry | |
{ | |
CompanyId = _companyId, | |
cacheBits = this.Serialize(), | |
LastWrite = DateTime.UtcNow | |
}; | |
//// update the DB and the lastwrite | |
_entries.Delete(x => x.CompanyId == _companyId); | |
_entries.Add(_cache); | |
_unitOfWork.Commit(); | |
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment