@@ -41,11 +41,6 @@ public class MSALPerUserMemoryTokenCache
4141 /// </summary>
4242 private readonly DateTimeOffset cacheDuration = DateTimeOffset . Now . AddHours ( 48 ) ;
4343
44- /// <summary>
45- /// The internal handle to the client's instance of the Cache
46- /// </summary>
47- private ITokenCache UserTokenCache ;
48-
4944 /// <summary>
5045 /// Once the user signes in, this will not be null and can be ontained via a call to Thread.CurrentPrincipal
5146 /// </summary>
@@ -77,18 +72,15 @@ private void Initialize(ITokenCache tokenCache, ClaimsPrincipal user)
7772 {
7873 this . SignedInUser = user ;
7974
80- this . UserTokenCache = tokenCache ;
81- this . UserTokenCache . SetBeforeAccess ( this . UserTokenCacheBeforeAccessNotification ) ;
82- this . UserTokenCache . SetAfterAccess ( this . UserTokenCacheAfterAccessNotification ) ;
83- this . UserTokenCache . SetBeforeWrite ( this . UserTokenCacheBeforeWriteNotification ) ;
75+ tokenCache . SetBeforeAccess ( this . UserTokenCacheBeforeAccessNotification ) ;
76+ tokenCache . SetAfterAccess ( this . UserTokenCacheAfterAccessNotification ) ;
77+ tokenCache . SetBeforeWrite ( this . UserTokenCacheBeforeWriteNotification ) ;
8478
8579 if ( this . SignedInUser == null )
8680 {
8781 // No users signed in yet, so we return
8882 return ;
8983 }
90-
91- this . LoadUserTokenCacheFromMemory ( ) ;
9284 }
9385
9486 /// <summary>
@@ -107,7 +99,7 @@ internal string GetMsalAccountId()
10799 /// <summary>
108100 /// Loads the user token cache from memory.
109101 /// </summary>
110- private void LoadUserTokenCacheFromMemory ( )
102+ private void LoadUserTokenCacheFromMemory ( ITokenCacheSerializer tokenCache )
111103 {
112104 string cacheKey = this . GetMsalAccountId ( ) ;
113105
@@ -116,21 +108,21 @@ private void LoadUserTokenCacheFromMemory()
116108
117109 // Ideally, methods that load and persist should be thread safe. MemoryCache.Get() is thread safe.
118110 byte [ ] tokenCacheBytes = ( byte [ ] ) this . memoryCache . Get ( this . GetMsalAccountId ( ) ) ;
119- this . UserTokenCache . DeserializeMsalV3 ( tokenCacheBytes ) ;
111+ tokenCache . DeserializeMsalV3 ( tokenCacheBytes ) ;
120112 }
121113
122114 /// <summary>
123115 /// Persists the user token blob to the memoryCache.
124116 /// </summary>
125- private void PersistUserTokenCache ( )
117+ private void PersistUserTokenCache ( ITokenCacheSerializer tokenCache )
126118 {
127119 string cacheKey = this . GetMsalAccountId ( ) ;
128120
129121 if ( string . IsNullOrWhiteSpace ( cacheKey ) )
130122 return ;
131123
132124 // Ideally, methods that load and persist should be thread safe.MemoryCache.Get() is thread safe.
133- this . memoryCache . Set ( this . GetMsalAccountId ( ) , this . UserTokenCache . SerializeMsalV3 ( ) , this . cacheDuration ) ;
125+ this . memoryCache . Set ( this . GetMsalAccountId ( ) , tokenCache . SerializeMsalV3 ( ) , this . cacheDuration ) ;
134126 }
135127
136128 /// <summary>
@@ -139,9 +131,6 @@ private void PersistUserTokenCache()
139131 public void Clear ( )
140132 {
141133 this . memoryCache . Remove ( this . GetMsalAccountId ( ) ) ;
142-
143- // Nulls the currently deserialized instance
144- this . LoadUserTokenCacheFromMemory ( ) ;
145134 }
146135
147136 /// <summary>
@@ -155,7 +144,7 @@ private void UserTokenCacheAfterAccessNotification(TokenCacheNotificationArgs ar
155144 // if the access operation resulted in a cache update
156145 if ( args . HasStateChanged )
157146 {
158- this . PersistUserTokenCache ( ) ;
147+ this . PersistUserTokenCache ( args . TokenCache ) ;
159148 }
160149 }
161150
@@ -165,7 +154,7 @@ private void UserTokenCacheAfterAccessNotification(TokenCacheNotificationArgs ar
165154 /// <param name="args">Contains parameters used by the MSAL call accessing the cache.</param>
166155 private void UserTokenCacheBeforeAccessNotification ( TokenCacheNotificationArgs args )
167156 {
168- this . LoadUserTokenCacheFromMemory ( ) ;
157+ this . LoadUserTokenCacheFromMemory ( args . TokenCache ) ;
169158 }
170159
171160 /// <summary>
0 commit comments