using Rhea.Common;
using Rhea.Common.Authorization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tiger.IBusiness;
namespace Tiger.Business
{
///
/// Tiger 项目激活类,包括以下文件:
///
"项目名.cer"(Certification):包含有公钥的数字证书,以Xml格式存储
///
"项目名.act"(ActivationCode):包含有激活码字符的文档
///
"项目名.req"(RequestCode):包含有请求码字符的文档
///
public class TigerActive : Activation, ITigerActive
{
///
/// 使用激活码激活程序
///
/// 激活码
///
public override Result Active(string activationCode)
{
Result result = new(Result.Flags.Success, "软件激活成功!");
try
{
License license = DecryptLicense(activationCode, GetCertification());
if (license.IsNullOrEmpty())
{
result.Flag = Result.Flags.Failed;
result.Message = $"激活失败,验证码解析出错。";
}
else
{
//项目名 ProjectName
if (license.ProjectName != BizContext.CurrentProject)
{
result.Flag = Result.Flags.Failed;
result.Message = $"激活失败,验证码不适用于本项目。";
}
//机器码 MachineCode
if (license.MachineCode != BizContext.MachineCode)
{
result.Flag = Result.Flags.Failed;
result.Message = $"激活失败,验证码不适用于本软件。";
}
//有效期截至日期 ExpiryDate
if ((license.ExpiryDate.Date - DateTime.Now.Date).TotalMilliseconds < 0)
{
result.Flag = Result.Flags.Warning;
result.Message = $"激活失败,验证码已过期。";
}
}
if (!result.IsFailed)
{
//保存激活码到注册表
SetActivationCode(activationCode);
}
if (result.IsSuccessed)
{
Verify();
}
}
catch (System.UnauthorizedAccessException ex)
{
result.CatchExceptionWithLog(ex, $"权限不足,请使用管理员运行后再激活");
}
catch (System.Exception ex)
{
result.CatchExceptionWithLog(ex, $"软件激活异常");
}
return result;
}
///
/// 验证当前程序是否已激活
///
///
public override Result Verify()
{
Result result = new(Result.Flags.Success, "授权验证通过!");
try
{
var license = GetCurLicense();
if (license.IsNullOrEmpty())
{
result.Flag = Result.Flags.Failed;
result.Message = $"授权验证失败,未找到许可信息。";
}
else
{
//项目名 ProjectName
if (!license.ProjectName.IsNullOrEmpty() && license.ProjectName != BizContext.CurrentProject)
{
result.Flag = Result.Flags.Failed;
result.Message = $"授权验证失败,项目未授权";
}
//机器码 MachineCode
if (!license.MachineCode.IsNullOrEmpty() && license.MachineCode != BizContext.MachineCode)
{
result.Flag = Result.Flags.Failed;
result.Message = $"授权验证失败,电脑未授权。";
}
//有效期截至日期 ExpiryDate
if (license.ExpiryDate != DateTime.MaxValue && (license.ExpiryDate.Date - DateTime.Now.Date).TotalMilliseconds < 0)
{
result.Flag = Result.Flags.Failed;
result.Message = $"授权验证失败,授权已过期。";
}
//工站字典,key:工站ID,value:允许同时开启的数量 StationDict
if (!license.StationDict.IsNullOrEmpty())
{
}
//试用期截至日期 TrialExpiryDate
if (!result.IsSuccessed && (license.TrialExpiryDate - DateTime.Now).TotalMilliseconds > 0)
{
result.Flag = Result.Flags.Success;
result.Message = $"授权验证通过,在试用期内。";
}
}
}
catch (System.Exception ex)
{
result.CatchExceptionWithLog(ex, $"授权验证异常");
}
return result;
}
///
/// 在注册表HKEY_LOCAL_MACHINE\SOFTWARE\TigerClouds节点中读取密钥证书
///
///
public string GetCertification()
{
Base.Regedit tc = new Base.Regedit(Base.Regedit.BaseKeys.LocalMachine, @"SOFTWARE\TigerClouds");
return tc.Read("Certification");
}
///
/// 在注册表HKEY_LOCAL_MACHINE\SOFTWARE\TigerClouds节点中写入密钥证书
///
/// 公钥
public void SetCertification(string key)
{
Base.Regedit tc = new Base.Regedit(Base.Regedit.BaseKeys.LocalMachine, @"SOFTWARE\TigerClouds");
tc.Write("Certification", key);
}
///
/// 在注册表HKEY_LOCAL_MACHINE\SOFTWARE\TigerClouds节点中读取激活码
///
///
public string GetActivationCode()
{
Base.Regedit tc = new Base.Regedit(Base.Regedit.BaseKeys.LocalMachine, @"SOFTWARE\TigerClouds");
return tc.Read("ActivationCode");
}
///
/// 在注册表HKEY_LOCAL_MACHINE\SOFTWARE\TigerClouds节点中写入激活码
///
/// 激活码
public void SetActivationCode(string code)
{
Base.Regedit tc = new Base.Regedit(Base.Regedit.BaseKeys.LocalMachine, @"SOFTWARE\TigerClouds");
tc.Write("ActivationCode", code);
}
///
/// 获取当前许可信息
///
///
public License GetCurLicense()
{
return DecryptLicense(GetActivationCode(), GetCertification());
}
}
}