Skip to content

Instantly share code, notes, and snippets.

@ezekg
Last active December 29, 2020 21:02
Show Gist options
  • Save ezekg/b1348b9f8f3ce9fe073c53c42552cdee to your computer and use it in GitHub Desktop.
Save ezekg/b1348b9f8f3ce9fe073c53c42552cdee to your computer and use it in GitHub Desktop.
Dead-simple license key validation using Keygen (https://keygen.sh)
using RestSharp;
using System;
using System.Collections.Generic;
public class Example
{
// This is your Keygen account ID or slug.
//
// Available at: https://app.keygen.sh/settings
const string KEYGEN_ACCOUNT_ID = "YOUR_KEYGEN_ACCOUNT_ID";
public static void Main()
{
var keygen = new RestClient(string.Format("https://api.keygen.sh/v1/accounts/{0}", KEYGEN_ACCOUNT_ID));
var request = new RestRequest("licenses/actions/validate-key", Method.POST);
request.AddHeader("Content-Type", "application/vnd.api+json");
request.AddHeader("Accept", "application/vnd.api+json");
request.AddJsonBody(new {
// Update this variable with a license key obtained from the current user.
meta = new { key = "XXXX-XXXX-XXXX-XXXX" }
});
var response = keygen.Execute<Dictionary<string, object>>(request).Data;
if (response.ContainsKey("errors"))
{
Console.WriteLine("Failed to validate license: {0}", response["errors"]);
return;
}
var license = (Dictionary<string, object>) response["data"];
var meta = (Dictionary<string, object>) response["meta"];
if ((bool) meta["valid"])
{
// … do something with the valid license
}
else
{
// … do something else
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment