@@ -46,7 +46,7 @@ public class ProtectedFileTokenCache : TokenCache, IAzureTokenCache
46
46
/// <summary>
47
47
/// A mutex to prevent IO to token cache file across threads / processes.
48
48
/// </summary>
49
- private static readonly Mutex fileLock = new Mutex ( false , @"Global \AzurePowerShellAdalTokenCacheFile" ) ;
49
+ private static readonly Mutex fileLock = new Mutex ( false , @"Local \AzurePowerShellAdalTokenCacheFile" ) ;
50
50
51
51
private static readonly Lazy < ProtectedFileTokenCache > instance = new Lazy < ProtectedFileTokenCache > ( ( ) => new ProtectedFileTokenCache ( ) ) ;
52
52
@@ -134,26 +134,32 @@ private void ReadFileIntoCache(string cacheFileName = null)
134
134
}
135
135
136
136
fileLock . WaitOne ( ) ;
137
- if ( _store . FileExists ( cacheFileName ) )
137
+ try
138
138
{
139
- var existingData = _store . ReadFileAsBytes ( cacheFileName ) ;
140
- if ( existingData != null )
139
+ if ( _store . FileExists ( cacheFileName ) )
141
140
{
142
- #if ! NETSTANDARD
143
- try
144
- {
145
- Deserialize ( ProtectedData . Unprotect ( existingData , null , DataProtectionScope . CurrentUser ) ) ;
146
- }
147
- catch ( CryptographicException )
141
+ var existingData = _store . ReadFileAsBytes ( cacheFileName ) ;
142
+ if ( existingData != null )
148
143
{
149
- _store . DeleteFile ( cacheFileName ) ;
150
- }
144
+ #if ! NETSTANDARD
145
+ try
146
+ {
147
+ Deserialize ( ProtectedData . Unprotect ( existingData , null , DataProtectionScope . CurrentUser ) ) ;
148
+ }
149
+ catch ( CryptographicException )
150
+ {
151
+ _store . DeleteFile ( cacheFileName ) ;
152
+ }
151
153
#else
152
- Deserialize ( existingData ) ;
154
+ Deserialize ( existingData ) ;
153
155
#endif
156
+ }
154
157
}
155
158
}
156
- fileLock . ReleaseMutex ( ) ;
159
+ finally
160
+ {
161
+ fileLock . ReleaseMutex ( ) ;
162
+ }
157
163
}
158
164
159
165
private void WriteCacheIntoFile ( string cacheFileName = null )
@@ -170,22 +176,30 @@ private void WriteCacheIntoFile(string cacheFileName = null)
170
176
#endif
171
177
172
178
fileLock . WaitOne ( ) ;
173
- if ( HasStateChanged )
179
+ try
174
180
{
175
- _store . WriteFile ( cacheFileName , dataToWrite ) ;
176
- HasStateChanged = false ;
181
+ if ( HasStateChanged )
182
+ {
183
+ _store . WriteFile ( cacheFileName , dataToWrite ) ;
184
+ HasStateChanged = false ;
185
+ }
186
+ }
187
+ finally
188
+ {
189
+ fileLock . ReleaseMutex ( ) ;
177
190
}
178
- fileLock . ReleaseMutex ( ) ;
179
191
}
180
192
181
193
private void EnsureCacheFile ( string cacheFileName = null )
182
194
{
183
195
fileLock . WaitOne ( ) ;
184
- if ( _store . FileExists ( cacheFileName ) )
196
+ try
185
197
{
186
- var existingData = _store . ReadFileAsBytes ( cacheFileName ) ;
187
- if ( existingData != null )
198
+ if ( _store . FileExists ( cacheFileName ) )
188
199
{
200
+ var existingData = _store . ReadFileAsBytes ( cacheFileName ) ;
201
+ if ( existingData != null )
202
+ {
189
203
#if ! NETSTANDARD
190
204
try
191
205
{
@@ -196,19 +210,23 @@ private void EnsureCacheFile(string cacheFileName = null)
196
210
_store . DeleteFile ( cacheFileName ) ;
197
211
}
198
212
#else
199
- Deserialize ( existingData ) ;
213
+ Deserialize ( existingData ) ;
200
214
#endif
215
+ }
201
216
}
202
- }
203
217
204
- // Eagerly create cache file.
218
+ // Eagerly create cache file.
205
219
#if ! NETSTANDARD
206
220
var dataToWrite = ProtectedData . Protect ( Serialize ( ) , null , DataProtectionScope . CurrentUser ) ;
207
221
#else
208
- var dataToWrite = Serialize ( ) ;
222
+ var dataToWrite = Serialize ( ) ;
209
223
#endif
210
- _store . WriteFile ( cacheFileName , dataToWrite ) ;
211
- fileLock . ReleaseMutex ( ) ;
224
+ _store . WriteFile ( cacheFileName , dataToWrite ) ;
225
+ }
226
+ finally
227
+ {
228
+ fileLock . ReleaseMutex ( ) ;
229
+ }
212
230
}
213
231
}
214
232
}
0 commit comments