Skip to content

Commit 5fb8767

Browse files
committed
Merge pull request #4 from yugangw-msft/tokencache
delete corrupted tokencache
2 parents 14b5be8 + 1e53a65 commit 5fb8767

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

src/Common/Commands.Common.Test/Common/ProfileCmdltsTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,20 @@ public void ClearAzureProfileClearsTokenCache()
149149
Assert.Equal(0, tokenCache.ReadItems().Count());
150150
}
151151

152+
[Fact]
153+
public void DeleteCorruptedTokenCache()
154+
{
155+
//setup
156+
string testFileName = @"c:\foobar\TokenCache.dat";
157+
ProfileClient.DataStore.WriteFile(testFileName, new byte[] { 0, 1 });
158+
159+
//Act
160+
ProtectedFileTokenCache tokenCache = new ProtectedFileTokenCache(testFileName);
161+
162+
//Assert
163+
Assert.False(ProfileClient.DataStore.FileExists(testFileName));
164+
}
165+
152166
[Fact]
153167
public void SetAzureSubscriptionAddsSubscriptionWithCertificate()
154168
{

src/Common/Commands.Common/Authentication/ProtectedFileTokenCache.cs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,39 @@ public static ProtectedFileTokenCache Instance
4444
// Initializes the cache against a local file.
4545
// If the file is already present, it loads its content in the ADAL cache
4646
private ProtectedFileTokenCache()
47+
{
48+
Initialize(CacheFileName);
49+
}
50+
51+
private void Initialize(string fileName)
4752
{
4853
AfterAccess = AfterAccessNotification;
4954
BeforeAccess = BeforeAccessNotification;
5055
lock (fileLock)
5156
{
52-
if (ProfileClient.DataStore.FileExists(CacheFileName))
57+
if (ProfileClient.DataStore.FileExists(fileName))
5358
{
54-
var existingData = ProfileClient.DataStore.ReadFileAsBytes(CacheFileName);
59+
var existingData = ProfileClient.DataStore.ReadFileAsBytes(fileName);
5560
if (existingData != null)
5661
{
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+
}
5870
}
5971
}
6072
}
6173
}
6274

75+
public ProtectedFileTokenCache(string cacheFile)
76+
{
77+
Initialize(cacheFile);
78+
}
79+
6380
// Empties the persistent store.
6481
public override void Clear()
6582
{
@@ -81,7 +98,14 @@ void BeforeAccessNotification(TokenCacheNotificationArgs args)
8198
var existingData = ProfileClient.DataStore.ReadFileAsBytes(CacheFileName);
8299
if (existingData != null)
83100
{
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+
}
85109
}
86110
}
87111
}

0 commit comments

Comments
 (0)