コード例 #1
0
ファイル: MainWindow.xaml.cs プロジェクト: edcastanha/simf4r
        private void generateLicense()
        {
            /*
             *  1. define a new Dictionary for the addition attributes and add the attributes.
             *  2. define a new Dictionary for the product features and add the features.
             *  3. create new guid as unique identifier.
             *  4. define the license with the required parameters.
             *  5. select Path and filename to write the license.
             */
            var attributesDictionarty = new Dictionary <string, string>
            {
                {
                    "HID", productid.Text
                }
            };


            var licenseId = Guid.NewGuid();


            ulicense =
                License.New().WithUniqueIdentifier(licenseId)
                .As(GetLicenseType())
                .WithMaximumUtilization(1)
                .WithAdditionalAttributes(attributesDictionarty)
                .LicensedTo(customer.Text, email.Text)
                //.CreateAndSignWithPrivateKey(privateKey, pass.Text);
                .CreateAndSignWithPrivateKey(privatekey.Text, pass.Text);

            var sfdlicense = new SaveFileDialog
            {
                Filter           = "License (*.lic)|*.lic",
                FilterIndex      = 1,
                RestoreDirectory = true,
                FileName         = "lic",
                InitialDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
            };
            var result = sfdlicense.ShowDialog();

            if (result.HasValue && result.Value)
            {
                var sw = new StreamWriter(sfdlicense.FileName, false, Encoding.UTF8);
                sw.Write(ulicense.ToString());
                sw.Close();
                sw.Dispose();
            }
        }
コード例 #2
0
ファイル: MainWindow.xaml.cs プロジェクト: edcastanha/simf4r
        private string ValidateLicense(License license)
        {
            //// validate license and define return value.
            const string ReturnValue = "License is Valid";

            var validationFailures =
                license.Validate()
                .ExpirationDate()
                .When(LicenseException)
                .And()
                .Signature(publickey.Text)
                .AssertValidLicense();

            var failures = validationFailures as IValidationFailure[] ?? validationFailures.ToArray();

            return(!failures.Any() ? ReturnValue : failures.Aggregate(string.Empty, (current, validationFailure) => current + validationFailure.HowToResolve + ": " + "\r\n" + validationFailure.Message + "\r\n"));
        }
コード例 #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="LicenseBuilder"/> class.
 /// </summary>
 public LicenseBuilder()
 {
     license = new License();
 }
コード例 #4
0
ファイル: MainWindow.xaml.cs プロジェクト: edcastanha/simf4r
 private static bool LicenseException(License license)
 {
     //// check licensetype.
     return(license.Type == LicenseType.Trial);
 }
コード例 #5
0
ファイル: MainWindow.xaml.cs プロジェクト: edcastanha/simf4r
        private void Testlicense_Click(object sender, RoutedEventArgs e)
        {
            /*
             * 1. clear validation listbox.
             * 2. define a new stream and select and read the license.
             * 3. read the values from the license.
             * 4. validate the license (expiration date and signature).
             */
            ListBoxValidation.Items.Clear();

            var ofdlicense = new OpenFileDialog();
            ////Dim LicStreamReader As StreamReader
            Stream myStream = null;

            ////OfDLicense.InitialDirectory = "c:\"
            ofdlicense.Filter           = "License (*.lic)|*.lic";
            ofdlicense.FilterIndex      = 1;
            ofdlicense.InitialDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            var result = ofdlicense.ShowDialog();

            if (!result.HasValue || !result.Value)
            {
                return;
            }

            try
            {
                myStream = ofdlicense.OpenFile();
                if (myStream.Length <= 0)
                {
                    return;
                }

                ulicense = License.Load(myStream);
                string username    = "******" + ulicense.Customer.Name;
                string email       = "Email: " + ulicense.Customer.Email;
                string quantity    = "Quantity: " + ulicense.Quantity.ToString(CultureInfo.InvariantCulture);
                string HID         = "HardwareID: " + ulicense.AdditionalAttributes.Get("HID");
                string licenseType = ulicense.Type.ToString();
                var    str         = ValidateLicense(ulicense);
                var    lbi         = new ListBoxItem {
                    Content = str
                };
                ListBoxValidation.Items.Add(username);
                ListBoxValidation.Items.Add(email);
                ListBoxValidation.Items.Add(quantity);
                ListBoxValidation.Items.Add(HID);
                ListBoxValidation.Items.Add(licenseType);
                ListBoxValidation.Items.Add(lbi);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Cannot read file from disk. Original error: " + ex.Message);
            }
            finally
            {
                //// Check this again, since we need to make sure we didn't throw an exception on open.
                if (myStream != null)
                {
                    myStream.Close();
                }
            }
        }
コード例 #6
0
        /// <summary>
        /// The button open license click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void ButtonOpenLicClick(object sender, RoutedEventArgs e)
        {
            /*
             *  1. clear validation listbox.
             *  2. define a new stream and select and read the license.
             *  3. read the values from the license.
             *  4. validate the license (expiration date and signature).
             */
            ListBoxValidation.Items.Clear();

            var ofdlicense = new OpenFileDialog();
            ////Dim LicStreamReader As StreamReader
            Stream myStream = null;

            ////OfDLicense.InitialDirectory = "c:\"
            ofdlicense.Filter           = "License (*.lic)|*.lic";
            ofdlicense.FilterIndex      = 1;
            ofdlicense.InitialDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            var result = ofdlicense.ShowDialog();

            if (!result.HasValue || !result.Value)
            {
                return;
            }

            try
            {
                myStream = ofdlicense.OpenFile();
                if (myStream.Length <= 0)
                {
                    return;
                }

                this.ulicense = License.Load(myStream);
                this.TextBoxCustomerRo.Text       = this.ulicense.Customer.Name;
                this.TextBoxEMailRo.Text          = this.ulicense.Customer.Email;
                this.TextBoxUsersRo.Text          = this.ulicense.Quantity.ToString(CultureInfo.InvariantCulture);
                this.TextBoxAttributeNameRo.Text  = "Software";
                this.TextBoxAttributeValueRo.Text = this.ulicense.AdditionalAttributes.Get(this.TextBoxAttributeNameRo.Text);
                this.CheckBoxSalesRo.IsChecked    = this.ulicense.ProductFeatures.Get("Sales") == "True";
                this.CheckBoxBillingRo.IsChecked  = this.ulicense.ProductFeatures.Get("Billing") == "True";
                this.TextBoxLicenseType.Text      = this.ulicense.Type.ToString();
                var str = this.ValidateLicense(this.ulicense);
                var lbi = new ListBoxItem {
                    Content = str
                };
                this.ListBoxValidation.Items.Add(lbi);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Cannot read file from disk. Original error: " + ex.Message);
            }
            finally
            {
                //// Check this again, since we need to make sure we didn't throw an exception on open.
                if (myStream != null)
                {
                    myStream.Close();
                }
            }
        }
コード例 #7
0
        /// <summary>
        /// The button create license click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void ButtonCreateLicClick(object sender, RoutedEventArgs e)
        {
            /*
             *  1. define a new Dictionary for the addition attributes and add the attributes.
             *  2. define a new Dictionary for the product features and add the features.
             *  3. create new guid as unique identifier.
             *  4. define the license with the required parameters.
             *  5. select Path and filename to write the license.
             */

            var attributesDictionarty = new Dictionary <string, string>
            {
                {
                    this.TextBoxAttributeName.Text,
                    this.TextBoxAttributeValue.Text
                }
            };

            var productFeatureDictionarty = new Dictionary <string, string>
            {
                {
                    "Sales",
                    this.CheckBoxSales.IsChecked.ToString()
                },
                {
                    "Billing", this.CheckBoxBilling.IsChecked.ToString()
                }
            };

            var licenseId = Guid.NewGuid();

            var datetime = DatePickerExpiration.SelectedDate;

            if (datetime != null)
            {
                this.ulicense =
                    License.New().WithUniqueIdentifier(licenseId)
                    .As(this.GetLicenseType())
                    .WithMaximumUtilization(int.Parse(this.TextBoxUsers.Text))
                    .WithAdditionalAttributes(attributesDictionarty)
                    .WithProductFeatures(productFeatureDictionarty)
                    .LicensedTo(this.TextBoxCustomer.Text, this.TextBoxEMail.Text)
                    .ExpiresAt(datetime.Value.AddDays(int.Parse(TextBoxDays.Text)))
                    .CreateAndSignWithPrivateKey(this.privateKey, this.TextBoxPassword.Text);
            }

            var sfdlicense = new SaveFileDialog
            {
                Filter           = "License (*.lic)|*.lic",
                FilterIndex      = 1,
                RestoreDirectory = true,
                FileName         = "lic",
                InitialDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
            };
            var result = sfdlicense.ShowDialog();

            if (result.HasValue && result.Value)
            {
                var sw = new StreamWriter(sfdlicense.FileName, false, Encoding.UTF8);
                sw.Write(this.ulicense.ToString());
                sw.Close();
                sw.Dispose();
            }

            ButtonOpenLic.IsEnabled = true;

            TestApp.IsEnabled = true;
        }