Last active
August 29, 2015 14:00
-
-
Save LucasMoffitt/11018418 to your computer and use it in GitHub Desktop.
Windows 8.1 C# PasswordVault Wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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