source: chomnr/licensing.net: in heavy development, don't use. (github.com)
About nine months ago, I wrote a pretty simple licensing system in C#, and I never got around to finishing it, but man, it's probably the only project where I like how the code looks. Personally, I think it's just really clean and straightforward. Even after nine months, I still understand why I put that there and I don't get lost anywhere.
The cool part of the project is that I wrote an "authority" system for my CRUD functions that manage the license. Basically, I can apply a set of rules to each CRUD function, and the function must follow these "rules" in order to successfully execute.
public async Task LicenseActivateEvent(string key)
{
LicenseLookUp lookUp = new LicenseLookUp(null, null, key);
LICENSE_STATUS[] statusToLookFor = new LICENSE_STATUS[] { LICENSE_STATUS.SUSPENDED, LICENSE_STATUS.UNCLAIMED };
IAuthorityRule[] rules = { new RequireStatusRole(statusToLookFor), new RequireOwnershipRule(), new RequireDurationRule() };
LicenseResult result = await Authority
.SetErrorMessage("Activation has failed either because the license does not exist, is deactivated, is currently activated, has no owner, or the duration is set to 0.")
.AddRules(rules)
.RunOn(lookUp);
if (result.AuthorityState == AUTHORITY_STATE.APPROVED)
{
var status = result.License.Status;
if (status == LICENSE_STATUS.SUSPENDED)
{
result.License.PurchaseDate = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
}
result.License.Status = LICENSE_STATUS.ACTIVATED;
}
return result;
}
To activate a license, you need to ensure that it has a Status, an owner, and a duration. If the AuthorityState == APPROVED, then it will grab the current status of the license. If it's suspended, it will reset the purchase date to ensure the user gets their entire duration.
I built this system to avoid using if statements everywhere. I thought this was a pretty clever way of circumventing that, and I'm proud of myself.
I might continue this project in the future, but I am not sure why I abandoned it? I think it was to build my security monitor, as you can see chomnr/live-security-monitor: A real-time brute force logger, this system meticulously logs and publicly exposes unauthorized access attempts. (github.com).