Exemplo n.º 1
0
        private async Task <Tuple <bool, int> > CheckLicensesByServiceAsync(LicenseInfo lic)
        {
            log.Debug("CheckLicensesByServiceAsync;");
            LicensingClient client = new LicensingClient(
                LicensingClient.EndpointConfiguration.BasicHttpBinding_ILicensing,
                ServiceEndpoint)
            {
                RsaPublicKey = RsaPublicKey,
                Password     = Password,
            };

            foreach (var key in lic.License.Where(l => !l.Retired)
                     .OrderByDescending(l => l.ExpireDate))
            {
                var response = await client.CheckLicenseAsync(
                    new LicenseCheckRequest { Hardware = hardware, Key = key.Key, Product = Product });

                if (response.State == LicenseCheckResponse.LicenseState.OK)
                {
                    key.Amount = response.Amount;

                    return(Tuple.Create(true, response.TotalAmount));
                }
                else
                {
                    key.Retired      = true; // retire keys expired, invalid, etc.
                    key.RetireReason = response.State.ToString() + ": " + response.Message;
                }
            }

            return(Tuple.Create(false, 0));
        }
Exemplo n.º 2
0
        public async Task <LicenseRegisterResult> RegisterLicenseAsync(string licenseKey)
        {
            LicensingClient client = new LicensingClient(
                LicensingClient.EndpointConfiguration.BasicHttpBinding_ILicensing,
                ServiceEndpoint)
            {
                Password     = Password,
                RsaPublicKey = RsaPublicKey,
            };

            var response = await client.RegisterLicenseAsync(
                new RegisterRequest { Hardware = hardware, Key = licenseKey, Product = Product });

            if (response.State == RegisterResponse.RegisterState.OK ||
                response.State == RegisterResponse.RegisterState.OKExisted)
            {
                LicenseInfo lic = GetStoredLicenseInfo() ??
                                  new LicenseInfo
                {
                    Counter  = this.MaxCheckPostponeCount,
                    Hardware = hardware,
                    License  = new License[] { },
                };

                var lickey = new License
                {
                    Key        = licenseKey,
                    Retired    = false,
                    Type       = 1, // dummy
                    Amount     = response.Amount,
                    IssueDate  = response.IssueDate.Value,
                    ExpireDate = response.ExpireDate.Value,
                };
                log.Debug(string.Format("RegisterLicenseAsync;IssueDate:{0},ExpireDate:{1}", response.IssueDate.Value, response.ExpireDate.Value));
                lic.License =
                    new[] { lickey }.Concat(
                    lic.License.Where(k => string.Compare(k.Key, licenseKey, StringComparison.OrdinalIgnoreCase) != 0)
                    ).ToArray();

                lic.Counter = this.MaxCheckPostponeCount;

                StoreLicenseInfo(lic);
                return(new LicenseRegisterResult
                {
                    State = response.State == RegisterResponse.RegisterState.OK ?
                            LicenseRegisterState.OK : LicenseRegisterState.AlreadyLicensed,
                    Amount = response.TotalAmount,
                    Message = response.Message,
                });
            }

            return(new LicenseRegisterResult
            {
                State =
                    response.State == RegisterResponse.RegisterState.AlreadyUsed ? LicenseRegisterState.AlreadyUsed :
                    response.State == RegisterResponse.RegisterState.Invalid ? LicenseRegisterState.InvalidKey :
                    response.State == RegisterResponse.RegisterState.Expired ? LicenseRegisterState.Expired :
                    LicenseRegisterState.StateError,
                Message = response.Message,
            });
        }
Exemplo n.º 3
0
 private void StoreLicenseInfo(LicenseInfo info)
 {
     log.Debug("StoreLicenseInfo;");
     byte[] data = info.AesEncrypt(AesKey, AesIv);
     KeyStorage.Store(data);
 }
Exemplo n.º 4
0
        public async Task <LicenseCheckResult> CheckLicenseStateAsync()
        {
            log.Debug("CheckLicenseStateAsync;");
            // Get stored license info
            LicenseInfo lic = GetStoredLicenseInfo();

            if (lic == null) // newly installed, not registered
            {
                return new LicenseCheckResult
                       {
                           State   = LicenseState.Unregistered,
                           Message = "未注册 License 或注册数据损坏。",
                       }
            }
            ;
            if (!lic.Hardware.Equals(hardware)) // stored license not match current machine
            {
                return new LicenseCheckResult
                       {
                           State   = LicenseState.MachineNotMatch,
                           Message = "License 注册信息与设备不符。",
                       }
            }
            ;

            try
            {
                var tuple = await CheckLicensesByServiceAsync(lic);

                bool foundActiveKey = tuple.Item1;
                int  amount         = tuple.Item2;

                lic.Counter = this.MaxCheckPostponeCount;

                if (!foundActiveKey)
                {
                    return new LicenseCheckResult
                           {
                               State   = LicenseState.NoValidLicense,
                               Message = "未找到有效期内的 License。",
                           }
                }
                ;

                return(new LicenseCheckResult
                {
                    State = LicenseState.OK,
                    Amount = amount,
                    Message = "License 正常。",
                });
            }
            catch (EndpointNotFoundException ex)
            {
                log.Warn(ex.Message, ex);
                if (!lic.CountDown())
                {
                    return new LicenseCheckResult
                           {
                               State   = LicenseState.CheckByServiceRequired,
                               Message = "无法连接 License 服务。",
                           }
                }
                ;

                if (lic.IsExpired(DateTime.Today))
                {
                    return new LicenseCheckResult
                           {
                               State   = LicenseState.NoValidLicense,
                               Message = "无法连接 License 服务,本机 License 已过期。",
                           }
                }
                ;

                return(new LicenseCheckResult
                {
                    State = LicenseState.CheckByServicePostponed,
                    Message = "无法连接 License 服务,本机 License 尚未过期,剩余 " + lic.Counter + " 次检查。",
                });
            }
            finally
            {
                StoreLicenseInfo(lic);
            }
        }