Skip to content

Instantly share code, notes, and snippets.

@LucasMoffitt
Last active August 29, 2015 14:00
Show Gist options
  • Save LucasMoffitt/11018418 to your computer and use it in GitHub Desktop.
Save LucasMoffitt/11018418 to your computer and use it in GitHub Desktop.
Windows 8.1 C# PasswordVault Wrapper
public static class VaultManager
{
private const string VaultKeyResource = "My App Or Something...";
private static readonly PasswordVault Vault = new PasswordVault();
public static string Username
{
get
{
try
{
var credentials = Vault.FindAllByResource(VaultKeyResource).FirstOrDefault();
return credentials.UserName;
}
catch (Exception)
{
return null;
}
}
}
public static string Password
{
get
{
try
{
return Vault.Retrieve(VaultKeyResource, Username).Password;
}
catch (Exception)
{
return null;
}
}
}
public static bool Save(string userName, string password)
{
try
{
if (HasCredentials)
Delete();
Vault.Add(new PasswordCredential(VaultKeyResource, userName, password));
return true;
}
catch (Exception)
{
return false;
}
}
public static bool Delete()
{
try
{
Vault.Remove(Vault.Retrieve(VaultKeyResource, Username));
return true;
}
catch (Exception)
{
return false;
}
}
public static bool HasCredentials
{
get
{
try
{
return Vault.FindAllByResource(VaultKeyResource).FirstOrDefault() != null;
}
catch (Exception)
{
return false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment