Requirements
- .NET 6.0 or later
- No external NuGet packages required (uses only the BCL)
Installation
Download AuthForgeClient.cs and add it to your project:
Direct download
curl / PowerShell
Git clone
Download AuthForgeClient.cs and add it to your project. curl -O https://raw.githubusercontent.com/AuthForgeCC/authforge-csharp/main/AuthForgeClient.cs
Invoke-WebRequest -Uri "https://raw.githubusercontent.com/AuthForgeCC/authforge-csharp/main/AuthForgeClient.cs" -OutFile "AuthForgeClient.cs"
git clone https://github.com/AuthForgeCC/authforge-csharp.git
cp authforge-csharp/AuthForgeClient.cs ./YourProject/
Quick start
using AuthForge;
var client = new AuthForgeClient(
appId: "YOUR_APP_ID",
appSecret: "YOUR_APP_SECRET",
heartbeatMode: "SERVER"
);
Console.Write("Enter license key: ");
var key = Console.ReadLine() ?? "";
if (client.Login(key))
{
Console.WriteLine("Authenticated!");
// Your app logic here — heartbeats run in the background
}
else
{
Console.WriteLine("Invalid license key.");
Environment.Exit(1);
}
Constructor parameters
var client = new AuthForgeClient(
appId: "...", // Required — from dashboard
appSecret: "...", // Required — from dashboard
heartbeatMode: "SERVER", // Required — "SERVER" or "LOCAL"
heartbeatInterval: 900, // Optional — seconds (default: 900)
apiBaseUrl: "https://auth.authforge.cc", // Optional
onFailure: null, // Optional — Action<string, Exception?>
requestTimeout: 15 // Optional — HTTP timeout in seconds
);
Login
bool success = client.Login(licenseKey);
Returns true if authentication succeeded, false otherwise. On success, the SDK starts a background heartbeat thread automatically.
Failure callback
If authentication or a heartbeat fails, the SDK calls your OnFailure callback. If no callback is set (or the callback throws), the SDK calls Environment.Exit(1).
void HandleFailure(string reason, Exception? exception)
{
if (reason == "login_failed")
{
Console.WriteLine("Login failed — check your license key.");
}
else if (reason == "heartbeat_failed")
{
Console.WriteLine("Heartbeat failed — saving state.");
SaveApplicationState();
}
}
var client = new AuthForgeClient(
appId: "YOUR_APP_ID",
appSecret: "YOUR_APP_SECRET",
heartbeatMode: "SERVER",
onFailure: HandleFailure
);
If you don’t set onFailure, the SDK terminates the process immediately on any failure. Always set a callback in production to handle graceful shutdown.
Reading variables
After a successful login, app variables and license variables are available:
if (client.Login(licenseKey))
{
// App-wide variables
var appVars = client.AppVariables;
if (appVars.TryGetValue("maintenanceMode", out var maintenance) && maintenance is true)
{
Console.WriteLine("Server is under maintenance.");
Environment.Exit(0);
}
// Per-license variables
var plan = client.LicenseVariables.GetValueOrDefault("plan", "basic");
if (plan?.ToString() == "pro")
{
EnableProFeatures();
}
}
Heartbeat modes
// SERVER mode — pings the API every interval
var client = new AuthForgeClient(
appId: "...",
appSecret: "...",
heartbeatMode: "SERVER",
heartbeatInterval: 900
);
// LOCAL mode — verifies locally, re-validates when prepaid block expires
var client = new AuthForgeClient(
appId: "...",
appSecret: "...",
heartbeatMode: "LOCAL",
heartbeatInterval: 900
);
See Heartbeat Modes for a detailed comparison.
Full example (WPF)
using System.Windows;
using AuthForge;
public partial class App : Application
{
private AuthForgeClient? _authClient;
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
_authClient = new AuthForgeClient(
appId: "YOUR_APP_ID",
appSecret: "YOUR_APP_SECRET",
heartbeatMode: "SERVER",
onFailure: OnAuthFailure
);
var dialog = new LicenseDialog();
if (dialog.ShowDialog() != true)
{
Shutdown();
return;
}
if (!_authClient.Login(dialog.LicenseKey))
{
MessageBox.Show("Invalid license key.", "Authentication Failed",
MessageBoxButton.OK, MessageBoxImage.Error);
Shutdown();
return;
}
var mainWindow = new MainWindow();
mainWindow.Show();
}
private void OnAuthFailure(string reason, Exception? ex)
{
Dispatcher.Invoke(() =>
{
// Save user's work before shutting down
(MainWindow as MainWindow)?.SaveState();
MessageBox.Show(
"License verification failed. Your work has been saved.",
"Authentication Error",
MessageBoxButton.OK,
MessageBoxImage.Warning
);
Shutdown();
});
}
}
GitHub
Full source, changelog, and issues: AuthForgeCC/authforge-csharp