public SecureString GetPassword (string accountNameOrNull)
{
try
{
using (RegistryKey profileKey = Registry.CurrentUser.OpenSubKey (_profileRegistryKeyName))
{
foreach (string subKeyName in profileKey.GetSubKeyNames())
{
using (RegistryKey subKey = profileKey.OpenSubKey (subKeyName))
{
if (accountNameOrNull != null)
{
var registryAccountName = ConvertRegistryValueToString (subKey.GetValue ("Account Name"));
if (registryAccountName != accountNameOrNull)
continue;
}
var passwordValue = (byte[]) subKey.GetValue ("IMAP Password") ?? (byte[]) subKey.GetValue ("POP3 Password");
if (passwordValue == null)
continue;
var encPassword = passwordValue.Skip (1).ToArray();
try
{
var clearPassword = ProtectedData.Unprotect (encPassword, null, DataProtectionScope.CurrentUser);
return SecureStringUtility.ToSecureString(Encoding.Unicode.GetString (clearPassword).TrimEnd ('\0'));
}
catch (CryptographicException x)
{
s_logger.Error ("Error while decrypting account password. Using empty password", x);
return new SecureString ();
}
}
}
return new SecureString();
}
}
catch (Exception ex)
{
s_logger.Error ("Error while fetching account password from registry. Using empty password", ex);
return new SecureString ();
}
}
}