Skip to content

Commit b35a3d8

Browse files
committed
Adding encryption of the serialized token cache on disk
(best practice)
1 parent 4f2c6e8 commit b35a3d8

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

TodoListClient/TokenCacheHelper.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static TokenCache GetUserCache()
5454
/// <summary>
5555
/// Path to the token cache
5656
/// </summary>
57-
public static string CacheFilePath = System.Reflection.Assembly.GetExecutingAssembly().Location + "msalcache.txt";
57+
public static readonly string CacheFilePath = System.Reflection.Assembly.GetExecutingAssembly().Location + ".msalcache.bin";
5858

5959
private static readonly object FileLock = new object();
6060

@@ -63,7 +63,9 @@ public static void BeforeAccessNotification(TokenCacheNotificationArgs args)
6363
lock (FileLock)
6464
{
6565
args.TokenCache.Deserialize(File.Exists(CacheFilePath)
66-
? File.ReadAllBytes(CacheFilePath)
66+
? ProtectedData.Unprotect(File.ReadAllBytes(CacheFilePath),
67+
null,
68+
DataProtectionScope.CurrentUser)
6769
: null);
6870
}
6971
}
@@ -76,7 +78,11 @@ public static void AfterAccessNotification(TokenCacheNotificationArgs args)
7678
lock (FileLock)
7779
{
7880
// reflect changesgs in the persistent store
79-
File.WriteAllBytes(CacheFilePath, args.TokenCache.Serialize());
81+
File.WriteAllBytes(CacheFilePath,
82+
ProtectedData.Protect(args.TokenCache.Serialize(),
83+
null,
84+
DataProtectionScope.CurrentUser)
85+
);
8086
// once the write operationtakes place restore the HasStateChanged bit to filse
8187
args.TokenCache.HasStateChanged = false;
8288
}

0 commit comments

Comments
 (0)