Skip to content

Instantly share code, notes, and snippets.

@swaters86
Created February 19, 2026 17:17
Show Gist options
  • Select an option

  • Save swaters86/88df09be29a31de9bdd807b6667c9666 to your computer and use it in GitHub Desktop.

Select an option

Save swaters86/88df09be29a31de9bdd807b6667c9666 to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
public static class SecretVault
{
private static readonly string Dir =
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
"YourCompany", "YourService");
private static readonly string ClientIdPath = Path.Combine(Dir, "clientid.txt");
private static readonly string SecretPath = Path.Combine(Dir, "clientsecret.bin");
public static void Save(string clientId, string clientSecret)
{
Directory.CreateDirectory(Dir);
File.WriteAllText(ClientIdPath, clientId ?? "", Encoding.UTF8);
var secretBytes = Encoding.UTF8.GetBytes(clientSecret ?? "");
var protectedBytes = ProtectedData.Protect(secretBytes, null, DataProtectionScope.LocalMachine);
File.WriteAllBytes(SecretPath, protectedBytes);
}
public static (string clientId, string clientSecret) Load()
{
if (!File.Exists(ClientIdPath) || !File.Exists(SecretPath))
throw new InvalidOperationException("Not provisioned. Missing clientid/secret files.");
var clientId = File.ReadAllText(ClientIdPath, Encoding.UTF8).Trim();
var protectedBytes = File.ReadAllBytes(SecretPath);
var secretBytes = ProtectedData.Unprotect(protectedBytes, null, DataProtectionScope.LocalMachine);
var secret = Encoding.UTF8.GetString(secretBytes);
return (clientId, secret);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment