@@ -73,27 +73,7 @@ public ProtectedFileTokenCache(string cacheFile, IDataStore store = null)
73
73
74
74
private void Initialize ( string fileName )
75
75
{
76
- lock ( fileLock )
77
- {
78
- if ( _store . FileExists ( fileName ) )
79
- {
80
- var existingData = _store . ReadFileAsBytes ( fileName ) ;
81
- if ( existingData != null )
82
- {
83
- try
84
- {
85
- Deserialize ( ProtectedData . Unprotect ( existingData , null , DataProtectionScope . CurrentUser ) ) ;
86
- }
87
- catch ( CryptographicException )
88
- {
89
- _store . DeleteFile ( fileName ) ;
90
- }
91
- }
92
- }
93
-
94
- // Create the file to start with
95
- _store . WriteFile ( CacheFileName , ProtectedData . Protect ( Serialize ( ) , null , DataProtectionScope . CurrentUser ) ) ;
96
- }
76
+ EnsureCacheFile ( fileName ) ;
97
77
98
78
AfterAccess = AfterAccessNotification ;
99
79
BeforeAccess = BeforeAccessNotification ;
@@ -113,45 +93,109 @@ public override void Clear()
113
93
// Reload the cache from the persistent store in case it changed since the last access.
114
94
void BeforeAccessNotification ( TokenCacheNotificationArgs args )
115
95
{
96
+ ReadFileIntoCache ( ) ;
97
+ }
98
+
99
+ // Triggered right after ADAL accessed the cache.
100
+ void AfterAccessNotification ( TokenCacheNotificationArgs args )
101
+ {
102
+ // if the access operation resulted in a cache update
103
+ EnsureStateSaved ( ) ;
104
+ }
105
+
106
+ void EnsureStateSaved ( )
107
+ {
108
+ if ( HasStateChanged )
109
+ {
110
+ WriteCacheIntoFile ( ) ;
111
+ }
112
+ }
113
+
114
+ private void ReadFileIntoCache ( string cacheFileName = null )
115
+ {
116
+ if ( cacheFileName == null )
117
+ {
118
+ cacheFileName = ProtectedFileTokenCache . CacheFileName ;
119
+ }
120
+
116
121
lock ( fileLock )
117
122
{
118
- if ( _store . FileExists ( CacheFileName ) )
123
+ if ( _store . FileExists ( cacheFileName ) )
119
124
{
120
- var existingData = _store . ReadFileAsBytes ( CacheFileName ) ;
125
+ var existingData = _store . ReadFileAsBytes ( cacheFileName ) ;
121
126
if ( existingData != null )
122
127
{
128
+ #if ! NETSTANDARD
123
129
try
124
130
{
125
131
Deserialize ( ProtectedData . Unprotect ( existingData , null , DataProtectionScope . CurrentUser ) ) ;
126
132
}
127
133
catch ( CryptographicException )
128
134
{
129
- _store . DeleteFile ( CacheFileName ) ;
135
+ _store . DeleteFile ( cacheFileName ) ;
130
136
}
137
+ #else
138
+ Deserialize ( existingData ) ;
139
+ #endif
131
140
}
132
141
}
133
142
}
134
143
}
135
144
136
- // Triggered right after ADAL accessed the cache.
137
- void AfterAccessNotification ( TokenCacheNotificationArgs args )
145
+ private void WriteCacheIntoFile ( string cacheFileName = null )
138
146
{
139
- // if the access operation resulted in a cache update
140
- EnsureStateSaved ( ) ;
147
+ if ( cacheFileName == null )
148
+ {
149
+ cacheFileName = ProtectedFileTokenCache . CacheFileName ;
150
+ }
151
+
152
+ #if ! NETSTANDARD
153
+ var dataToWrite = ProtectedData . Protect ( Serialize ( ) , null , DataProtectionScope . CurrentUser ) ;
154
+ #else
155
+ var dataToWrite = Serialize ( ) ;
156
+ #endif
157
+
158
+ lock ( fileLock )
159
+ {
160
+ if ( HasStateChanged )
161
+ {
162
+ _store . WriteFile ( cacheFileName , dataToWrite ) ;
163
+ HasStateChanged = false ;
164
+ }
165
+ }
141
166
}
142
167
143
- void EnsureStateSaved ( )
168
+ private void EnsureCacheFile ( string cacheFileName = null )
144
169
{
145
170
lock ( fileLock )
146
171
{
147
- if ( HasStateChanged )
172
+ if ( _store . FileExists ( cacheFileName ) )
148
173
{
149
- // reflect changes in the persistent store
150
- _store . WriteFile ( CacheFileName ,
151
- ProtectedData . Protect ( Serialize ( ) , null , DataProtectionScope . CurrentUser ) ) ;
152
- // once the write operation took place, restore the HasStateChanged bit to false
153
- HasStateChanged = false ;
174
+ var existingData = _store . ReadFileAsBytes ( cacheFileName ) ;
175
+ if ( existingData != null )
176
+ {
177
+ #if ! NETSTANDARD
178
+ try
179
+ {
180
+ Deserialize ( ProtectedData . Unprotect ( existingData , null , DataProtectionScope . CurrentUser ) ) ;
181
+ }
182
+ catch ( CryptographicException )
183
+ {
184
+ _store . DeleteFile ( cacheFileName ) ;
185
+ }
186
+ #else
187
+ Deserialize ( existingData ) ;
188
+ #endif
189
+ }
154
190
}
191
+
192
+ // Eagerly create cache file.
193
+ #if ! NETSTANDARD
194
+ var dataToWrite = ProtectedData . Protect ( Serialize ( ) , null , DataProtectionScope . CurrentUser ) ;
195
+ #else
196
+ var dataToWrite = Serialize ( ) ;
197
+ #endif
198
+ _store . WriteFile ( cacheFileName , dataToWrite ) ;
155
199
}
156
200
}
157
201
}
0 commit comments