天天看点

开源 .net license tool, EasyLicense !

Easy License 非常容易使用,为了验证一个软件,你需要下面3个步骤。

1: Create a public/private Key.

if (File.Exists("privateKey.xml") || File.Exists("publicKey.xml"))

            {

                var result = MessageBox.Show("The key is existed, override it?", "Warning", MessageBoxButton.YesNo);

                if (result == MessageBoxResult.No)

                {

                    return;

                }

            }

            var privateKey = "";

            var publicKey = "";

            LicenseGenerator.GenerateLicenseKey(out privateKey, out publicKey);

            File.WriteAllText("privateKey.xml", privateKey);

            File.WriteAllText("publicKey.xml", publicKey);

            MessageBox.Show("The Key is created, please backup it.");

  

2:  Use private key to create a license

if (!File.Exists("privateKey.xml"))

                MessageBox.Show("Please create a license key first");

                return;

            var privateKey = File.ReadAllText(@"privateKey.xml");

            var generator = new LicenseGenerator(privateKey);

            var dictionary = new Dictionary<string, string>();

            // generate the license

            var license = generator.Generate("EasyLicense", Guid.NewGuid(), DateTime.UtcNow.AddYears(1), dictionary,

                LicenseType.Standard);

            txtLicense.Text = license;

            File.WriteAllText("license.lic", license);

3:  Use public key to validate the license

private static void ValidateLicense()

        {

            if (!File.Exists("publicKey.xml"))

            var publicKey = File.ReadAllText(@"publicKey.xml");

            var validator = new LicenseValidator(publicKey, @"license.lic");

            try

                validator.AssertValidLicense();

            catch (Exception ex)

                Console.WriteLine(ex.Message);

EasyLicense 内部有一个叫 LicenseTool 的工具,你可以下载源代码,运行,来看看它是怎样的创建Key,创建Licens 和验证License 的。

开源 .net license tool, EasyLicense !

并且系统还有一个Demo 的项目,可以帮助你。

开源 .net license tool, EasyLicense !

本文转自lzwxx 51CTO博客,原文链接:http://blog.51cto.com/13064681/1944337

继续阅读