Rhino Licensing
Apparence
Vérifier la licence
var publicKey = File.ReadAllText("publicKey.xml");
var licenseValidator = new LicenseValidator(publicKey, "license.xml");
// desactivate le Timer qui vérifie la licence
LicenseValidator.DisableFutureChecks();
// l'Action LicenseInvalidated doit être branchée
// sinon une InvalidOperationException est lancée dans AbstractLicenseValidator.RaiseLicenseInvalidated()
LicenseValidator.LicenseInvalidated += LicenseValidator_LicenseInvalidated;
try
{
// throw LicenceNotFoundException
licenseValidator.AssertValidLicense();
}
catch (Exception ex)
{ }
finally
{
LicenseValidator.LicenseInvalidated -= LicenseValidator_LicenseInvalidated;
}
private void LicenseValidator_LicenseInvalidated(InvalidationType obj)
{
log.ErrorFormat("License was invalidated. {0}",
obj == InvalidationType.CannotGetNewLicense ? "Can not create a new license" : "License is expired");
}
|
Créer une licence
var id = Guid.NewGuid();
var name = "Mon Nom";
var expirationDate = new DateTime(2014, 12, 25);
var licenseType = LicenseType.Standard;
var attributes = new Dictionary<string, string>();
attributes.Add("Country", "France");
var privateKey = File.ReadAllText("privateKey.xml");
var generator = new LicenseGenerator(privateKey);
var license = generator.Generate(name, id, expirationDate, attributes, licenseType);
File.WriteAllText("license.xml", license);
|
Créer les clés privée et publique
var rsa = new RSACryptoServiceProvider(1024);
File.WriteAllText("publicKey.xml", rsa.ToXmlString(false));
File.WriteAllText("privateKey.xml", rsa.ToXmlString(true));
|
Fonctionnement
Rhino Licensing utilise un chiffrement asymétrique.
La licence est au format XML, elle contient:
- le nom de l'utilisateur
- la date d'expiration
- le type de licence
- une signature cryptographique: un hashage (SHA1) chiffré avec la clé privé du contenu du fichier, ceci afin d'éviter toutes modifications
- autant d'autres information que nécessaire
La vérification de la licence se fait par:
- déchiffrement de la signature avec la clé publique
- création d'un hashage de la licence
- comparaison du hashage obtenu avec la signature déchiffrée