@@ -41,11 +41,6 @@ public class MSALPerUserMemoryTokenCache
41
41
/// </summary>
42
42
private readonly DateTimeOffset cacheDuration = DateTimeOffset . Now . AddHours ( 48 ) ;
43
43
44
- /// <summary>
45
- /// The internal handle to the client's instance of the Cache
46
- /// </summary>
47
- private ITokenCache UserTokenCache ;
48
-
49
44
/// <summary>
50
45
/// Once the user signes in, this will not be null and can be ontained via a call to Thread.CurrentPrincipal
51
46
/// </summary>
@@ -77,18 +72,15 @@ private void Initialize(ITokenCache tokenCache, ClaimsPrincipal user)
77
72
{
78
73
this . SignedInUser = user ;
79
74
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 ) ;
84
78
85
79
if ( this . SignedInUser == null )
86
80
{
87
81
// No users signed in yet, so we return
88
82
return ;
89
83
}
90
-
91
- this . LoadUserTokenCacheFromMemory ( ) ;
92
84
}
93
85
94
86
/// <summary>
@@ -107,7 +99,7 @@ internal string GetMsalAccountId()
107
99
/// <summary>
108
100
/// Loads the user token cache from memory.
109
101
/// </summary>
110
- private void LoadUserTokenCacheFromMemory ( )
102
+ private void LoadUserTokenCacheFromMemory ( ITokenCacheSerializer tokenCache )
111
103
{
112
104
string cacheKey = this . GetMsalAccountId ( ) ;
113
105
@@ -116,21 +108,21 @@ private void LoadUserTokenCacheFromMemory()
116
108
117
109
// Ideally, methods that load and persist should be thread safe. MemoryCache.Get() is thread safe.
118
110
byte [ ] tokenCacheBytes = ( byte [ ] ) this . memoryCache . Get ( this . GetMsalAccountId ( ) ) ;
119
- this . UserTokenCache . DeserializeMsalV3 ( tokenCacheBytes ) ;
111
+ tokenCache . DeserializeMsalV3 ( tokenCacheBytes ) ;
120
112
}
121
113
122
114
/// <summary>
123
115
/// Persists the user token blob to the memoryCache.
124
116
/// </summary>
125
- private void PersistUserTokenCache ( )
117
+ private void PersistUserTokenCache ( ITokenCacheSerializer tokenCache )
126
118
{
127
119
string cacheKey = this . GetMsalAccountId ( ) ;
128
120
129
121
if ( string . IsNullOrWhiteSpace ( cacheKey ) )
130
122
return ;
131
123
132
124
// 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 ) ;
134
126
}
135
127
136
128
/// <summary>
@@ -139,9 +131,6 @@ private void PersistUserTokenCache()
139
131
public void Clear ( )
140
132
{
141
133
this . memoryCache . Remove ( this . GetMsalAccountId ( ) ) ;
142
-
143
- // Nulls the currently deserialized instance
144
- this . LoadUserTokenCacheFromMemory ( ) ;
145
134
}
146
135
147
136
/// <summary>
@@ -155,7 +144,7 @@ private void UserTokenCacheAfterAccessNotification(TokenCacheNotificationArgs ar
155
144
// if the access operation resulted in a cache update
156
145
if ( args . HasStateChanged )
157
146
{
158
- this . PersistUserTokenCache ( ) ;
147
+ this . PersistUserTokenCache ( args . TokenCache ) ;
159
148
}
160
149
}
161
150
@@ -165,7 +154,7 @@ private void UserTokenCacheAfterAccessNotification(TokenCacheNotificationArgs ar
165
154
/// <param name="args">Contains parameters used by the MSAL call accessing the cache.</param>
166
155
private void UserTokenCacheBeforeAccessNotification ( TokenCacheNotificationArgs args )
167
156
{
168
- this . LoadUserTokenCacheFromMemory ( ) ;
157
+ this . LoadUserTokenCacheFromMemory ( args . TokenCache ) ;
169
158
}
170
159
171
160
/// <summary>
0 commit comments