Esempio n. 1
0
        public void ShowLicenseInfo(LicenseEntity license, string additionalInfo)
        {
            try
            {
                StringBuilder _sb = new StringBuilder(512);

                Type           _typeLic = license.GetType();
                PropertyInfo[] _props   = _typeLic.GetProperties();

                object _value         = null;
                string _formatedValue = string.Empty;
                foreach (PropertyInfo _p in _props)
                {
                    try
                    {
                        ShowInLicenseInfoAttribute _showAttr = (ShowInLicenseInfoAttribute)Attribute.GetCustomAttribute(_p, typeof(ShowInLicenseInfoAttribute));
                        if (_showAttr != null && _showAttr.ShowInLicenseInfo)
                        {
                            _value = _p.GetValue(license, null);
                            _sb.Append(_showAttr.DisplayAs);
                            _sb.Append(": ");

                            //Append value and apply the format
                            if (_value != null)
                            {
                                switch (_showAttr.DataFormatType)
                                {
                                case ShowInLicenseInfoAttribute.FormatType.String:
                                    _formatedValue = _value.ToString();
                                    break;

                                case ShowInLicenseInfoAttribute.FormatType.Date:
                                    if (_p.PropertyType == typeof(DateTime) && !string.IsNullOrWhiteSpace(DateFormat))
                                    {
                                        _formatedValue = ((DateTime)_value).ToString(DateFormat);
                                    }
                                    else
                                    {
                                        _formatedValue = _value.ToString();
                                    }
                                    break;

                                case ShowInLicenseInfoAttribute.FormatType.DateTime:
                                    if (_p.PropertyType == typeof(DateTime) && !string.IsNullOrWhiteSpace(DateTimeFormat))
                                    {
                                        _formatedValue = ((DateTime)_value).ToString(DateTimeFormat);
                                    }
                                    else
                                    {
                                        _formatedValue = _value.ToString();
                                    }
                                    break;

                                case ShowInLicenseInfoAttribute.FormatType.EnumDescription:
                                    string _name = Enum.GetName(_p.PropertyType, _value);
                                    if (_name != null)
                                    {
                                        FieldInfo            _fi  = _p.PropertyType.GetField(_name);
                                        DescriptionAttribute _dna = (DescriptionAttribute)Attribute.GetCustomAttribute(_fi, typeof(DescriptionAttribute));
                                        if (_dna != null)
                                        {
                                            _formatedValue = _dna.Description;
                                        }
                                        else
                                        {
                                            _formatedValue = _value.ToString();
                                        }
                                    }
                                    else
                                    {
                                        _formatedValue = _value.ToString();
                                    }
                                    break;
                                }

                                _sb.Append(_formatedValue);
                            }

                            _sb.Append("\r\n");
                        }
                    }
                    catch
                    {
                        //Ignore exeption
                    }
                }


                if (string.IsNullOrWhiteSpace(additionalInfo))
                {
                    _sb.Append(additionalInfo.Trim());
                }

                txtLicInfo.Text = _sb.ToString();
            }
            catch (Exception ex)
            {
                txtLicInfo.Text = ex.Message;
            }
        }
Esempio n. 2
0
        private void but_Save_Click(object sender, EventArgs e)
        {
            if (txt_Name.Text.Length == 0)
            {
                MessageBox.Show("许可证名称不能为空!");
                return;
            }
            if (txt_AssemblyName.Text.Length == 0)
            {
                MessageBox.Show("程序集名称不能为空!");
                return;
            }
            but_Save.Enabled = false;
            LicenseEntity license = new LicenseEntity();

            license.ID             = Guid.NewGuid();
            license.Name           = txt_Name.Text;
            license.AssemblyName   = txt_AssemblyName.Text;
            license.PastDate       = txt_PastDate.Value.ToString("yyyy-MM-dd");
            license.UseNumber      = int.Parse(txt_UseNumber.Text);
            license.InstallNumber  = int.Parse(txt_InstallNumber.Text);
            license.VersionUpgrade = txt_VersionUpgrade.Checked;

            string context = DESEncrypt.Encrypt(XmlFormatterSerializer.SerializeToXml(license, license.GetType()));

            string licenseFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "LCL.lic");

            if (!File.Exists(licenseFile) == true)
            {
                File.Delete(licenseFile);
            }
            File.WriteAllText(licenseFile, context);

            MessageBox.Show("写入成功");
            but_Save.Enabled = true;
        }
Esempio n. 3
0
        public static string GenerateLicense(LicenseEntity lic, byte[] certPrivateKeyData, SecureString certFilePwd)
        {
            //Serialize license object into XML
            XmlDocument licenseObject = new XmlDocument();

            using (StringWriter writer = new StringWriter())
            {
                XmlSerializer serializer = new XmlSerializer(typeof(LicenseEntity), new[] { lic.GetType() });

                serializer.Serialize(writer, lic);

                licenseObject.LoadXml(writer.ToString());
            }

            //Get RSA key from certificate
            X509Certificate2 cert = new X509Certificate2(certPrivateKeyData, certFilePwd);

            RSACryptoServiceProvider rsaKey = (RSACryptoServiceProvider)cert.PrivateKey;

            //Sign the XML
            SignXml(licenseObject, rsaKey);

            //Convert the signed XML into BASE64 string
            return(Convert.ToBase64String(Encoding.UTF8.GetBytes(licenseObject.OuterXml)));
        }
Esempio n. 4
0
        public void ShowLicenseInfo(LicenseEntity license, string additionalInfo)
        {
            try
            {
                StringBuilder _sb = new StringBuilder(512);

                Type _typeLic = license.GetType();
                PropertyInfo[] _props = _typeLic.GetProperties();

                object _value = null;
                string _formatedValue = string.Empty;
                foreach (PropertyInfo _p in _props)
                {
                    try
                    {
                        ShowInLicenseInfoAttribute _showAttr = (ShowInLicenseInfoAttribute)Attribute.GetCustomAttribute(_p, typeof(ShowInLicenseInfoAttribute));
                        if (_showAttr != null && _showAttr.ShowInLicenseInfo)
                        {
                            _value = _p.GetValue(license, null);
                            _sb.Append(_showAttr.DisplayAs);
                            _sb.Append(": ");

                            //Append value and apply the format   
                            if (_value != null)
                            {
                                switch (_showAttr.DataFormatType)
                                {
                                    case ShowInLicenseInfoAttribute.FormatType.String:
                                        _formatedValue = _value.ToString();
                                        break;
                                    case ShowInLicenseInfoAttribute.FormatType.Date:
                                        if (_p.PropertyType == typeof(DateTime) && !string.IsNullOrWhiteSpace(DateFormat))
                                        {
                                            _formatedValue = ((DateTime)_value).ToString(DateFormat);
                                        }
                                        else
                                        {
                                            _formatedValue = _value.ToString();
                                        }
                                        break;
                                    case ShowInLicenseInfoAttribute.FormatType.DateTime:
                                        if (_p.PropertyType == typeof(DateTime) && !string.IsNullOrWhiteSpace(DateTimeFormat))
                                        {
                                            _formatedValue = ((DateTime)_value).ToString(DateTimeFormat);
                                        }
                                        else
                                        {
                                            _formatedValue = _value.ToString();
                                        }
                                        break;
                                    case ShowInLicenseInfoAttribute.FormatType.EnumDescription:
                                        string _name = Enum.GetName(_p.PropertyType, _value);
                                        if (_name != null)
                                        {
                                            FieldInfo _fi = _p.PropertyType.GetField(_name);
                                            DescriptionAttribute _dna = (DescriptionAttribute)Attribute.GetCustomAttribute(_fi, typeof(DescriptionAttribute));
                                            if (_dna != null)
                                                _formatedValue = _dna.Description;
                                            else
                                                _formatedValue = _value.ToString();
                                        }
                                        else
                                        {
                                            _formatedValue = _value.ToString();
                                        }
                                        break;
                                }

                                _sb.Append(_formatedValue);
                            }

                            _sb.Append("\r\n");
                        }
                    }
                    catch
                    {
                        //Ignore exeption
                    }
                }


                if (string.IsNullOrWhiteSpace(additionalInfo))
                {
                    _sb.Append(additionalInfo.Trim());
                }

                txtLicInfo.Text = _sb.ToString();
            }
            catch (Exception ex)
            {
                txtLicInfo.Text = ex.Message;
            }
        }