22
33using System . Threading ;
44using Microsoft . AspNetCore . Http ;
5- using Microsoft . Extensions . Caching . Memory ;
65using System . Collections . Generic ;
76
87namespace WebApp_OpenIDConnect_DotNet . Models
98{
9+ /// <summary>
10+ /// This implementation is just for demo purposes and does not scale. For better cache implementations see
11+ /// https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/tree/master/2-WebApp-graph-user/2-1-Call-MSGraph
12+ /// </summary>
1013 public class MSALStaticCache
1114 {
1215 private static Dictionary < string , byte [ ] > staticCache = new Dictionary < string , byte [ ] > ( ) ;
1316
1417 private static ReaderWriterLockSlim SessionLock = new ReaderWriterLockSlim ( LockRecursionPolicy . NoRecursion ) ;
15- string UserId = string . Empty ;
16- string CacheId = string . Empty ;
18+ private readonly string userId = string . Empty ;
19+ private readonly string cacheId = string . Empty ;
1720 private readonly HttpContext httpContext = null ;
1821 private ITokenCache cache ;
1922
2023 public MSALStaticCache ( string userId , HttpContext httpcontext )
2124 {
2225 // not object, we want the SUB
23- UserId = userId ;
24- CacheId = UserId + "_TokenCache" ;
26+ this . userId = userId ;
27+ cacheId = this . userId + "_TokenCache" ;
2528 httpContext = httpcontext ;
2629 }
2730
@@ -36,7 +39,7 @@ public ITokenCache EnablePersistence(ITokenCache cache)
3639 public void Load ( TokenCacheNotificationArgs args )
3740 {
3841 SessionLock . EnterReadLock ( ) ;
39- byte [ ] blob = staticCache . ContainsKey ( CacheId ) ? staticCache [ CacheId ] : null ;
42+ byte [ ] blob = staticCache . ContainsKey ( cacheId ) ? staticCache [ cacheId ] : null ;
4043 if ( blob != null )
4144 {
4245 args . TokenCache . DeserializeMsalV3 ( blob ) ;
@@ -49,7 +52,7 @@ public void Persist(TokenCacheNotificationArgs args)
4952 SessionLock . EnterWriteLock ( ) ;
5053
5154 // Reflect changes in the persistent store
52- staticCache [ CacheId ] = args . TokenCache . SerializeMsalV3 ( ) ;
55+ staticCache [ cacheId ] = args . TokenCache . SerializeMsalV3 ( ) ;
5356 SessionLock . ExitWriteLock ( ) ;
5457 }
5558
0 commit comments