public interface IDesktopSecretStore Secrets, such as access tokens, kept by the user's credential store: the Secret Service on Linux, the Keychain on macOS and Credential Manager on Windows. Each secret is identified by a service and an account.
Remarks
Services and accounts are application-chosen names, such as com.example.Mail and user@example.com, each 1 to 256 characters without NUL. Text secrets are stored as UTF-8, of at most 2560 bytes on every platform, the Credential Manager limit. Binary secrets (IDesktopSecretStore.GetBytesAsync, IDesktopSecretStore.SetBytesAsync) are at most DesktopSecretStore.MaximumBinarySecretBytes bytes on every platform. One item exists per service and account in either form: IDesktopSecretStore.GetAsync reads a binary item that is UTF-8 text, and IDesktopSecretStore.GetBytesAsync reads a text item as its UTF-8 bytes.
DesktopSecretStoreOptions chooses non-interactive requests, a required encrypted transfer on Linux and the macOS data-protection keychain; the providers' CreateSecretStore(DesktopSecretStoreOptions) overloads take them.
Outcomes common to every platform: no reachable store is Unavailable with PlatformUnavailableReason.BackendUnavailable; a locked store that cannot ask the user to unlock it is PlatformFailureCode.Locked; a dismissed unlock or access prompt is PlatformFailureCode.UserDismissed; access refused by the store is PlatformFailureCode.PermissionDenied. A request may wait while the store asks the user; cancellation dismisses that prompt.
Methods
DeleteAsync
ValueTask<PlatformResult<bool>> DeleteAsync(string service, string account, CancellationToken cancellationToken = default) Removes the secret stored for service and account.
Returns
Whether a secret was removed; false when none was stored.
GetAsync
ValueTask<PlatformResult<string?>> GetAsync(string service, string account, CancellationToken cancellationToken = default) Reads the secret stored for service and account.
Returns
The secret, or a successful null when none is stored.
Remarks
A stored value that is not UTF-8 text is PlatformFailureCode.InvalidData.
GetBytesAsync
ValueTask<PlatformResult<byte[]?>> GetBytesAsync(string service, string account, CancellationToken cancellationToken = default) Reads the secret stored for service and account as bytes.
Returns
The secret, or a successful null when none is stored. A text secret is its UTF-8 bytes.
Remarks
The returned array belongs to the caller, which should clear it after use; the store clears its own buffers. The default implementation checks the arguments and returns Unavailable with PlatformUnavailableReason.BackendUnavailable.
SetAsync
ValueTask<PlatformResult<PlatformUnit>> SetAsync(string service, string account, string secret, CancellationToken cancellationToken = default) Stores secret for service and account, replacing an earlier one.
Remarks
A secret longer than 2560 UTF-8 bytes is PlatformFailureCode.TooLarge, and nothing is stored.
SetBytesAsync
ValueTask<PlatformResult<PlatformUnit>> SetBytesAsync(string service, string account, ReadOnlyMemory<byte> secret, CancellationToken cancellationToken = default) Stores the bytes of secret for service and account, replacing an earlier secret in either form.
Remarks
A secret longer than DesktopSecretStore.MaximumBinarySecretBytes bytes is PlatformFailureCode.TooLarge, and nothing is stored. The store copies the bytes and clears its copies; the caller's memory is not changed. The default implementation checks the arguments and returns Unavailable with PlatformUnavailableReason.BackendUnavailable.