Skip to content

Commit 4606dea

Browse files
committed
delete corrupted tokencache
1 parent 14b5be8 commit 4606dea

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

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

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

152+
[Fact]
153+
public void DeleteCorruptedTokenCache()
154+
{
155+
ProfileClient.DataStore.WriteFile(ProtectedFileTokenCache.CacheFileName, new byte[]{0,1});
156+
ProtectedFileTokenCache tokenCache = ProtectedFileTokenCache.Instance;
157+
Assert.False(ProfileClient.DataStore.FileExists(ProtectedFileTokenCache.CacheFileName));
158+
}
159+
152160
[Fact]
153161
public void SetAzureSubscriptionAddsSubscriptionWithCertificate()
154162
{

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace Microsoft.WindowsAzure.Commands.Utilities.Common.Authentication
2626
/// </summary>
2727
public class ProtectedFileTokenCache : TokenCache
2828
{
29-
private static readonly string CacheFileName = Path.Combine(AzurePowerShell.ProfileDirectory, "TokenCache.dat");
29+
public static readonly string CacheFileName = Path.Combine(AzurePowerShell.ProfileDirectory, "TokenCache.dat");
3030

3131
private static readonly object fileLock = new object();
3232

@@ -54,7 +54,14 @@ private ProtectedFileTokenCache()
5454
var existingData = ProfileClient.DataStore.ReadFileAsBytes(CacheFileName);
5555
if (existingData != null)
5656
{
57-
Deserialize(ProtectedData.Unprotect(existingData, null, DataProtectionScope.CurrentUser));
57+
try
58+
{
59+
Deserialize(ProtectedData.Unprotect(existingData, null, DataProtectionScope.CurrentUser));
60+
}
61+
catch (CryptographicException)
62+
{
63+
ProfileClient.DataStore.DeleteFile(CacheFileName);
64+
}
5865
}
5966
}
6067
}
@@ -81,7 +88,14 @@ void BeforeAccessNotification(TokenCacheNotificationArgs args)
8188
var existingData = ProfileClient.DataStore.ReadFileAsBytes(CacheFileName);
8289
if (existingData != null)
8390
{
84-
Deserialize(ProtectedData.Unprotect(existingData, null, DataProtectionScope.CurrentUser));
91+
try
92+
{
93+
Deserialize(ProtectedData.Unprotect(existingData, null, DataProtectionScope.CurrentUser));
94+
}
95+
catch (CryptographicException)
96+
{
97+
ProfileClient.DataStore.DeleteFile(CacheFileName);
98+
}
8599
}
86100
}
87101
}

0 commit comments

Comments
 (0)