Exemplo n.º 1
0
        private void checkButton_Click(object sender, EventArgs e)
        {
            try
            {
                checkButton.Enabled = true;
                XmlLicense l = Manager.CheckLicense(licenseFileNameBox.Text, keyFileNameBox.Text);

                licenseInfoBox.Text = "The license is valid\r\n" +
                                      "User: "******"\r\n" +
                                      "License Type: " + l.LicenseType + "\r\n" +
                                      "Expired: " + l.Expired + "\r\n" +
                                      "S/N: " + l.SerialNumber + "\r\n" +
                                      "Issued: " + l.Issued;
            }
            catch (BadLicense ex)
            {
                licenseInfoBox.Text = "BAD LICENSE: " + ex.Message;
            }
        }
Exemplo n.º 2
0
        public IActionResult CreateLicense(string format, string sign, [FromBody] LicenseModel model)
        {
            ILicense license = null;

            format = format.ToUpperInvariant();
            switch (format)
            {
            case "XML":
                license = new XmlLicense();
                break;

            case "ASCII":
            case "BASE32":
                license = new StringLicense();
                break;

            default:
                return(BadRequest());
            }

            ISignatureProvider signature = null;

            switch (sign.ToUpperInvariant())
            {
            case "ECDSA":
                var ecdsa = new ECDsaSignature(ECDsaKeySize.KeySize112bit);
                ecdsa.CreateKeyPair();
                // Embed public key information in the license
                ecdsa.GenerateKeyInfo = true;
                model.PublicKey       = ecdsa.PublicKey;
                signature             = ecdsa;
                break;

            case "RSA":
                var rsa = new RSASignature(1024);
                rsa.CreateKeyPair();
                signature = rsa;
                break;

            default:
                return(BadRequest());
            }

            // Fill license
            license.WithId(model.Id ?? "1")
            .IssuedAt(DateTime.UtcNow)
            .WithType(model.Type ?? string.Empty)
            .ForProduct(model.Product ?? string.Empty, model.Version ?? string.Empty)
            .LicensedTo(model.Licensee ?? string.Empty);

            if (model.ExpireDate.HasValue)
            {
                license.ExpiresAt(model.ExpireDate.Value.ToUniversalTime());
            }

            if (model.SupportExpireDate.HasValue)
            {
                license.SupportExpiresAt(model.SupportExpireDate.Value.ToUniversalTime());
            }

            // Sign license
            var licenseBuilder = license.SignWith(signature);

            var stringBuilder = licenseBuilder as StringLicenseBuilder;

            if (stringBuilder != null)
            {
                switch (format)
                {
                case "ASCII":
                    stringBuilder.Format = StringFormat.Ascii;
                    break;

                case "BASE32":
                    stringBuilder.Format = StringFormat.Base32;
                    break;
                }
            }

            // Get license key
            model.LicenseKey = licenseBuilder.ToReadableString();

            return(Ok(model));
        }