Portable.Licensing 如何将许可证绑定到 PC

Portable.Licensing how to tie a license to a PC

我们有一个 C# 应用程序,需要保护它免遭非法复制。所以我们决定使用 Portable.Licensing 库来保护我们的系统。

如何将许可证绑定到 Portable.Licensing 中的硬件 ID,以便只有特定 PC 可以使用许可证?

您可以根据 PC 的名称、硬件信息等生成一个唯一的哈希值,并在创建许可证期间将此哈希值添加为 Additional Attribute

许可证创建示例:

var license = License.New()  
    .WithUniqueIdentifier(Guid.NewGuid())  
    .As(LicenseType.Standard)    
    .WithMaximumUtilization(1)  
    .WithAdditionalAttributes(new Dictionary<string, string>  
                              {  
                                  {"HardwareId", "........"}  
                              })  
    .LicensedTo("John Doe", "john.doe@yourmail.here")  
    .CreateAndSignWithPrivateKey(privateKey, passPhrase);

要验证属性,您可以实施自己的验证扩展方法或只使用现有的 AssertThat()。示例:[1]

唯一硬件 ID 的生成超出了便携式许可的范围。

[1] https://github.com/dnauck/Portable.Licensing/blob/develop/src/Portable.Licensing/Validation/LicenseValidationExtensions.cs#L100

您可以调用AsserThat方法:

license.Validate()
.AssertThat(lic => lic.ProductFeatures.Get("HardwareId") == "133456", new GeneralValidationFailure() { Message="Invalid Hardware.", HowToResolve="Contact administrator"});

使用ProtectedData.Protect和DataProtectionScope.LocalMachine来加密许可证文件中的一些键值对。 那么只有当该值可以在同一台机器上成功解密时,许可证才有效。

ProtectedData.UnProtect 只会在加密的同一台机器上解密。 不过,这将需要 desktop/client 在注册过程中进行服务器交互才能实现。