Created
December 3, 2024 14:35
-
-
Save MartinZikmund/dcad8c9b6d74fd37beb1247878e5d32b to your computer and use it in GitHub Desktop.
This file contains 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 class CredentialsService : ICredentialsService | |
{ | |
private const string AppName = "MyBlueskyClient"; | |
private readonly PasswordVault _passwordVault; | |
private readonly ApplicationDataContainer _localSettings = ApplicationData.Current.LocalSettings; | |
public CredentialsService() | |
{ | |
if (ApiInformation.IsMethodPresent(typeof(PasswordCredential).FullName, "Retrieve")) | |
{ | |
_passwordVault = new PasswordVault(); | |
} | |
} | |
public (string? handle, string? appPassword) RetrieveCredentials() | |
{ | |
string? appPassword = null; | |
if (_localSettings.Values.TryGetValue("UserHandle", out var value) && value is string handle) | |
{ | |
if (_passwordVault is not null) | |
{ | |
var credential = _passwordVault.Retrieve(AppName, handle); | |
appPassword = credential.Password; | |
return (handle, appPassword); | |
} | |
else if (_localSettings.Values.TryGetValue("UserPassword", out var password) && password is string userPassword) | |
{ | |
appPassword = userPassword; | |
return (handle, appPassword); | |
} | |
} | |
return (null, null); | |
} | |
public void SaveCredentials(string handle, string appPassword) | |
{ | |
_localSettings.Values["UserHandle"] = handle; | |
if (_passwordVault is not null) | |
{ | |
_passwordVault.Add(new PasswordCredential(AppName, handle, appPassword)); | |
} | |
else | |
{ | |
_localSettings.Values["UserPassword"] = appPassword; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment