Created
August 16, 2016 15:15
-
-
Save anonymous/8bb5319fb0b5297f209913ce77bd1f56 to your computer and use it in GitHub Desktop.
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
class FileCache : TokenCache | |
{ | |
private const DataProtectionScope DpScope = DataProtectionScope.CurrentUser; | |
private static readonly byte[] DpEntropy = Convert.FromBase64String("SmuJ3fxsOxitDcR+51332A=="); | |
private const string FileLockPrefix = "9384fef8-0a3e-4bad-8c5e-8d1001a80aba"; | |
private readonly object _fileLock; | |
private readonly FileInfo _file; | |
public FileCache(string filePath = @".\TokenCache.dat") | |
{ | |
_file = new FileInfo(filePath); | |
AfterAccess = AfterAccessNotification; | |
BeforeAccess = BeforeAccessNotification; | |
_fileLock = string.Intern(FileLockPrefix + _file.FullName.ToLowerInvariant()); | |
lock (_fileLock) | |
{ | |
ReadFile(); | |
} | |
} | |
public override void Clear() | |
{ | |
lock (_fileLock) | |
{ | |
_file.Delete(); | |
} | |
} | |
void BeforeAccessNotification(TokenCacheNotificationArgs args) | |
{ | |
lock (_fileLock) | |
{ | |
ReadFile(); | |
} | |
} | |
void AfterAccessNotification(TokenCacheNotificationArgs args) | |
{ | |
if (!HasStateChanged) | |
return; | |
lock (_fileLock) | |
{ | |
if (!HasStateChanged) | |
return; | |
WriteFile(); | |
HasStateChanged = false; | |
} | |
} | |
private void ReadFile() | |
{ | |
try | |
{ | |
var cipherText = ReadAllBytes(_file.FullName); | |
var clearText = Unprotect(cipherText, DpEntropy, DpScope); | |
Deserialize(clearText); | |
} | |
catch | |
{ | |
Deserialize(null); | |
} | |
} | |
private void WriteFile() | |
{ | |
var clearText = Serialize(); | |
var cipherText = Protect(clearText, DpEntropy, DpScope); | |
WriteAllBytes(_file.FullName, cipherText); | |
} | |
} |
oops forgot to include usings:
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System;
using System.IO;
using System.Security.Cryptography;
using static System.IO.File;
using static System.Security.Cryptography.ProtectedData;
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Improvements: