@@ -44,22 +44,39 @@ public static ProtectedFileTokenCache Instance
44
44
// Initializes the cache against a local file.
45
45
// If the file is already present, it loads its content in the ADAL cache
46
46
private ProtectedFileTokenCache ( )
47
+ {
48
+ Initialize ( CacheFileName ) ;
49
+ }
50
+
51
+ private void Initialize ( string fileName )
47
52
{
48
53
AfterAccess = AfterAccessNotification ;
49
54
BeforeAccess = BeforeAccessNotification ;
50
55
lock ( fileLock )
51
56
{
52
- if ( ProfileClient . DataStore . FileExists ( CacheFileName ) )
57
+ if ( ProfileClient . DataStore . FileExists ( fileName ) )
53
58
{
54
- var existingData = ProfileClient . DataStore . ReadFileAsBytes ( CacheFileName ) ;
59
+ var existingData = ProfileClient . DataStore . ReadFileAsBytes ( fileName ) ;
55
60
if ( existingData != null )
56
61
{
57
- Deserialize ( ProtectedData . Unprotect ( existingData , null , DataProtectionScope . CurrentUser ) ) ;
62
+ try
63
+ {
64
+ Deserialize ( ProtectedData . Unprotect ( existingData , null , DataProtectionScope . CurrentUser ) ) ;
65
+ }
66
+ catch ( CryptographicException )
67
+ {
68
+ ProfileClient . DataStore . DeleteFile ( fileName ) ;
69
+ }
58
70
}
59
71
}
60
72
}
61
73
}
62
74
75
+ public ProtectedFileTokenCache ( string cacheFile )
76
+ {
77
+ Initialize ( cacheFile ) ;
78
+ }
79
+
63
80
// Empties the persistent store.
64
81
public override void Clear ( )
65
82
{
@@ -81,7 +98,14 @@ void BeforeAccessNotification(TokenCacheNotificationArgs args)
81
98
var existingData = ProfileClient . DataStore . ReadFileAsBytes ( CacheFileName ) ;
82
99
if ( existingData != null )
83
100
{
84
- Deserialize ( ProtectedData . Unprotect ( existingData , null , DataProtectionScope . CurrentUser ) ) ;
101
+ try
102
+ {
103
+ Deserialize ( ProtectedData . Unprotect ( existingData , null , DataProtectionScope . CurrentUser ) ) ;
104
+ }
105
+ catch ( CryptographicException )
106
+ {
107
+ ProfileClient . DataStore . DeleteFile ( CacheFileName ) ;
108
+ }
85
109
}
86
110
}
87
111
}
0 commit comments