Skip to content

Commit

Permalink
Made ProtectedData.Unprotect() errors more informative
Browse files Browse the repository at this point in the history
  • Loading branch information
pgonzal committed Apr 14, 2016
1 parent 28b3bcb commit fb501c1
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions Source/Yamster.Core/Model/YamsterApiSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ private void Load(string filename)
}
succeeded = true;
}
catch (Exception ex)
{
throw new InvalidOperationException("Error reading settings file:\r\n\"" + filename + "\"", ex);
}
finally
{
if (!succeeded)
Expand All @@ -241,11 +245,18 @@ private void ReadAuthenticationProperties(XElement rootElement, Version version)
}
else if (unprocessedToken.StartsWith(ProtectedPrefix))
{
string encrypted = unprocessedToken.Substring(ProtectedPrefix.Length);
byte[] bytes2 = Convert.FromBase64String(encrypted);
byte[] bytes1 = ProtectedData.Unprotect(bytes2, null, DataProtectionScope.LocalMachine);
byte[] bytes0 = ProtectedData.Unprotect(bytes1, null, DataProtectionScope.CurrentUser);
this.OAuthToken = Encoding.UTF8.GetString(bytes0);
try
{
string encrypted = unprocessedToken.Substring(ProtectedPrefix.Length);
byte[] bytes2 = Convert.FromBase64String(encrypted);
byte[] bytes1 = ProtectedData.Unprotect(bytes2, null, DataProtectionScope.LocalMachine);
byte[] bytes0 = ProtectedData.Unprotect(bytes1, null, DataProtectionScope.CurrentUser);
this.OAuthToken = Encoding.UTF8.GetString(bytes0);
}
catch (Exception ex)
{
throw new InvalidOperationException("Unable to decrypt OAuthToken (was it saved by a different user or PC?)", ex);
}
}
else if (unprocessedToken.StartsWith(UnprotectedPrefix))
{
Expand Down

0 comments on commit fb501c1

Please sign in to comment.