我很想知道是否有任何标准或资源可以推荐用于在 C# 中开发许可证模型?
2 回答
在 C# 中,您可以使用Microsoft 提供的Licensing 类。可通过链接获得示例。
基本上,您创建一个继承自 LicenseProvider 的类并键入
[LicenseProvider(typeof(MyControlLicenseProvider))]
作为您希望获得许可的类的属性。在您的实现 ( MyControlLicenseProvider
) 中,您可以编写适当的方法来验证您需要的许可证,然后让您的代码调用
License license = LicenseManager.Validate(typeof(MyControl), this);
如果 license 不为空,则您的应用程序/控件已获得许可。
正如 Graystar 爵士所说,没有系统是万无一失的,具有所需技能的人可以围绕您的验证进行设计。不要花太多时间来实现一个坚如磐石的系统,但也不要让它容易被规避。:-)
使用 SHA 之类的散列或其中一个 MD 散列函数并向它们提供一些关键数据可用于创建简单的验证检查。您的验证方法可以做一些类似的事情
public override License GetLicense(LicenseContext context,
Type type,
object instance,
bool allowExceptions)
if (context.UsageMode == LicenseUsageMode.Designtime) {
// Creating a special DesigntimeLicense will able you to design your
// control without breaking Visual Studio in the process
return new DesigntimeLicense();
}
byte[] existingSerialKey = getExistingSerial();
// Algorithm can be SHA1CryptoServiceProvider for instance
byte[] data = HashAlgorithm.Create().ComputeHash(
username,
dateRequested,
validUntilDate,
// any other data you would like to validate
);
// todo: also check if licensing period is over here. ;-)
for (int l = 0; l < existingSerialKey.Length; ++l) {
if (existingSerialKey[i] != data[i]) {
if (allowExceptions){
throw new LicenseException(type, instance, "License is invalid");
}
return null;
}
}
// RuntimeLicense can be anything inheriting from License
return new RuntimeLicense();
}
此方法返回一个自定义License,例如,它具有关于许可的属性,例如许可用完时的DateTime 。设置它应该不会花很长时间,它适用于 WinForms 和 ASP.NET 站点,并且会阻止(不保证暗示)破坏您的许可的简单尝试。
我从来没有遇到过这样的标准。我个人会做的是创建一个记录或类似的东西,其中包含有关许可证的所有信息以及在输入产品密钥之前可用的功能,以及试用期或任何到期之前的时间(这可能会很棘手,因为你不'不想与系统日期进行比较,因为这可以由用户更改)。然后,您希望加密此记录(可能通过对其进行加盐和散列),以便只有在输入特定产品密钥时才能解锁这些功能。您可能还想随机生成所述密钥,尽管我会以特定格式执行此操作,以便用户在获得产品密钥时始终可以识别产品密钥,并且不要将其与发票号或其他内容混淆。
请记住,没有系统是万无一失的,并且某些操作系统上的某些人会以某种方式找到解决方法。这可能是我从来没有遇到过任何标准的原因,因为没有一种方法是完全安全的。你只需要尽力而为。