用C#开发许可证 - 我从哪里开始?

17 .net c# security licensing license-key

我有兴趣了解是否有任何标准或资源可以推荐用于在C#中开发许可证模型?

Pat*_*ick 21

在C#中,您可以使用Microsoft提供的Licensing类.可以通过链接获得样本.

基本上,您创建一个继承自LicenseProvider的类并键入

[LicenseProvider(typeof(MyControlLicenseProvider))]
Run Code Online (Sandbox Code Playgroud)

作为您希望获得许可的类的属性.在您的实现(MyControlLicenseProvider)中,您可以编写适当的方法来根据需要验证许可证,然后调用代码

License license = LicenseManager.Validate(typeof(MyControl), this);
Run Code Online (Sandbox Code Playgroud)

如果许可证不为null,则您的应用程序/控件被许可.

正如格雷斯塔爵士所说,没有一个系统是万无一失的,而且具备所需技能的人可以在验证过程中自行设计.不要花费太多时间来实施岩石硬系统,但也不要轻易绕过它们.:-)

使用类似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();
}
Run Code Online (Sandbox Code Playgroud)

此方法返回自定义许可证,例如,具有许可证属性的许可证,例如许可证用完时的DateTime.设置它不应该花费很长时间,它适用于WinForms和ASP.NET站点,并且会阻止(不保证暗示)简单的尝试来破坏您的许可.