c# - 在 C# 中开发许可证 - 我从哪里开始?

标签 c# .net security licensing license-key

<分区>

我很想知道是否有任何标准或资源可以推荐用于在 C# 中开发许可模型?

最佳答案

在 C# 中,您可以使用 Licensing class由微软提供。可通过链接获得示例。

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

[LicenseProvider(typeof(MyControlLicenseProvider))]

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

License license = LicenseManager.Validate(typeof(MyControl), this);

如果许可证不为空,则您的应用程序/控件已获得许可。

正如 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 站点,并且会阻止(不保证暗示)破坏许可的简单尝试。

关于c# - 在 C# 中开发许可证 - 我从哪里开始?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2896888/

相关文章:

C# 非常简单的图像缩放器

c# - 快速找到最近浮点值的数据结构

c# - PrincipalContext.ValidateCredentials 在 XP 上始终使用 Machine ContextType 返回 False

c# - 是否可以让一个参数实现两个接口(interface)?

c# - Asp.Net 身份本地化 PublicKeyToken

security - 如果从 Windows 8 RTM 中设置的提升进程和非默认浏览器运行,ShellExecute 会失败

c# - 从当前加载的记录中更新一个字段

.net - RDLC:如何在一份报告中打印多个表格

security - 为什么加密哈希函数中的冲突检测可以更轻松地发现其他冲突?

php 安全 - 防止加载可写的 php 文件