Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

delete corrupted tokencache #4

Merged
merged 2 commits into from
Nov 21, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Common/Commands.Common.Test/Common/ProfileCmdltsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,20 @@ public void ClearAzureProfileClearsTokenCache()
Assert.Equal(0, tokenCache.ReadItems().Count());
}

[Fact]
public void DeleteCorruptedTokenCache()
{
//setup
string testFileName = @"c:\foobar\TokenCache.dat";
ProfileClient.DataStore.WriteFile(testFileName, new byte[] { 0, 1 });

//Act
ProtectedFileTokenCache tokenCache = new ProtectedFileTokenCache(testFileName);

//Assert
Assert.False(ProfileClient.DataStore.FileExists(testFileName));
}

[Fact]
public void SetAzureSubscriptionAddsSubscriptionWithCertificate()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,39 @@ public static ProtectedFileTokenCache Instance
// Initializes the cache against a local file.
// If the file is already present, it loads its content in the ADAL cache
private ProtectedFileTokenCache()
{
Initialize(CacheFileName);
}

private void Initialize(string fileName)
{
AfterAccess = AfterAccessNotification;
BeforeAccess = BeforeAccessNotification;
lock (fileLock)
{
if (ProfileClient.DataStore.FileExists(CacheFileName))
if (ProfileClient.DataStore.FileExists(fileName))
{
var existingData = ProfileClient.DataStore.ReadFileAsBytes(CacheFileName);
var existingData = ProfileClient.DataStore.ReadFileAsBytes(fileName);
if (existingData != null)
{
Deserialize(ProtectedData.Unprotect(existingData, null, DataProtectionScope.CurrentUser));
try
{
Deserialize(ProtectedData.Unprotect(existingData, null, DataProtectionScope.CurrentUser));
}
catch (CryptographicException)
{
ProfileClient.DataStore.DeleteFile(fileName);
}
}
}
}
}

public ProtectedFileTokenCache(string cacheFile)
{
Initialize(cacheFile);
}

// Empties the persistent store.
public override void Clear()
{
Expand All @@ -81,7 +98,14 @@ void BeforeAccessNotification(TokenCacheNotificationArgs args)
var existingData = ProfileClient.DataStore.ReadFileAsBytes(CacheFileName);
if (existingData != null)
{
Deserialize(ProtectedData.Unprotect(existingData, null, DataProtectionScope.CurrentUser));
try
{
Deserialize(ProtectedData.Unprotect(existingData, null, DataProtectionScope.CurrentUser));
}
catch (CryptographicException)
{
ProfileClient.DataStore.DeleteFile(CacheFileName);
}
}
}
}
Expand Down