授权更改
This commit is contained in:
397
CapMachine.Wpf/SoftAuthorizeCore/SoftAuthorizeHelper.cs
Normal file
397
CapMachine.Wpf/SoftAuthorizeCore/SoftAuthorizeHelper.cs
Normal file
@@ -0,0 +1,397 @@
|
||||
using HslCommunication.BasicFramework;
|
||||
using HslCommunication;
|
||||
using HslCommunication.Profinet.Melsec;
|
||||
using HslCommunication.Profinet.Siemens;
|
||||
using System.IO;
|
||||
using System.Windows.Forms;
|
||||
using SixLabors.ImageSharp.Drawing;
|
||||
using System.ComponentModel;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Windows.Documents;
|
||||
using SharpCompress.Common;
|
||||
|
||||
namespace CapMachine.Wpf.SoftAuthorizeCore
|
||||
{
|
||||
/// <summary>
|
||||
/// 程序的软授权
|
||||
/// </summary>
|
||||
public class SoftAuthorizeHelper
|
||||
{
|
||||
//private HslCommunication.BasicFramework.SoftAuthorize softAuthorize = null;
|
||||
|
||||
// 定义一个静态变量来保存类的实例
|
||||
private static SoftAuthorizeHelper Instance;
|
||||
|
||||
/// <summary>
|
||||
/// 定义公有方法提供一个全局访问点,同时你也可以定义公有属性来提供全局访问点
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static SoftAuthorizeHelper GetInstance()
|
||||
{
|
||||
// 如果类的实例不存在则创建,否则直接返回
|
||||
if (Instance == null)
|
||||
{
|
||||
Instance = new SoftAuthorizeHelper();
|
||||
}
|
||||
return Instance;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 初始化
|
||||
/// </summary>
|
||||
public void Init()
|
||||
{
|
||||
// 或者使用类型的完全限定名
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取机器码
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string GetMachineCode()
|
||||
{
|
||||
return LicenseClient.GenSerialNo();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写机器码到指定的文件中
|
||||
/// </summary>
|
||||
/// <param name="MachineCode"></param>
|
||||
/// <param name="Path">路径</param>
|
||||
/// <returns></returns>
|
||||
public bool WriteMachineCodeToFile(string MachineCode, string TxtFilePath)
|
||||
{
|
||||
//暂时是固定的
|
||||
TxtFilePath = MachineCodePath;
|
||||
|
||||
// 参数验证
|
||||
if (string.IsNullOrEmpty(MachineCode) || string.IsNullOrEmpty(TxtFilePath))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// 确保目录存在
|
||||
string directoryPath = System.IO.Path.GetDirectoryName(TxtFilePath);
|
||||
if (!Directory.Exists(directoryPath))
|
||||
{
|
||||
Directory.CreateDirectory(directoryPath);
|
||||
}
|
||||
|
||||
// 写入文件
|
||||
using (StreamWriter writer = new StreamWriter(TxtFilePath))
|
||||
{
|
||||
writer.WriteLine(MachineCode);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Key文件的存放路径
|
||||
/// </summary>
|
||||
private string KeyFile { get; set; } = System.AppDomain.CurrentDomain.BaseDirectory + @"Key";
|
||||
|
||||
/// <summary>
|
||||
/// Lience文件的存放路径
|
||||
/// </summary>
|
||||
private string LiencePath { get; set; } = System.AppDomain.CurrentDomain.BaseDirectory + @"Key\License.txt";
|
||||
|
||||
/// <summary>
|
||||
/// 机器码文件的存放路径
|
||||
/// </summary>
|
||||
private string MachineCodePath { get; set; } = System.AppDomain.CurrentDomain.BaseDirectory + @"Key\MachineCode.txt";
|
||||
|
||||
/// <summary>
|
||||
/// 当前的授权信息
|
||||
/// </summary>
|
||||
public AuthorizeInfo CurAuthorizeInfo { get; set; } = new AuthorizeInfo();
|
||||
|
||||
/// <summary>
|
||||
/// 获取Xml Key 文件
|
||||
/// </summary>
|
||||
public void GetXmlKey()
|
||||
{
|
||||
var key = LicenseServer.GenXmlKey();
|
||||
if (key == null)
|
||||
{
|
||||
//MessageBox.Show("生成密钥失败!", "提示");
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
////写入文件
|
||||
//using (FolderBrowserDialog dialog = new FolderBrowserDialog())
|
||||
//{
|
||||
// if (dialog.ShowDialog() == DialogResult.OK)
|
||||
// {
|
||||
|
||||
string savePath = KeyFile;
|
||||
using (StreamWriter writer = new StreamWriter(System.IO.Path.Combine(savePath, "PublicKey.xml")))
|
||||
{
|
||||
writer.WriteLine(key[0]);
|
||||
}
|
||||
using (StreamWriter writer = new StreamWriter(System.IO.Path.Combine(savePath, "PrivateKey.xml")))
|
||||
{
|
||||
writer.WriteLine(key[1]);
|
||||
}
|
||||
|
||||
// }
|
||||
// else
|
||||
// return;
|
||||
//}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
MessageBox.Show("保存密钥失败!", "提示");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 授权检验
|
||||
/// 需要:
|
||||
/// 1)机器码
|
||||
/// 2)公钥
|
||||
/// 3)授权文件
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public bool CheckLience(string MachineCode, string PublicKey, string Lience)
|
||||
{
|
||||
//授权测试需要 机器序列号+公钥
|
||||
if (MachineCode == string.Empty)
|
||||
{
|
||||
//MessageBox.Show("授权测试需要目标机器的序列号!", "提示");
|
||||
return false;
|
||||
}
|
||||
if (PublicKey == string.Empty)
|
||||
{
|
||||
//MessageBox.Show("授权测试需要使用公钥!", "提示");
|
||||
return false;
|
||||
}
|
||||
|
||||
//无论授权是否通过,都更新校验时间
|
||||
if (LicenseClient.VerifyLicense(Lience, PublicKey) == false)
|
||||
{
|
||||
LicenseClient.UpdateVerifyTime(Lience, LiencePath);
|
||||
//MessageBox.Show("授权文件无效!", "提示");
|
||||
return false;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
LicenseClient.UpdateVerifyTime(Lience, LiencePath);
|
||||
//MessageBox.Show("授权文件有效!", "提示");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取公匙详细信息
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string GetPublicKeyDataByFilePath(string Path)
|
||||
{
|
||||
try
|
||||
{
|
||||
//using (OpenFileDialog dialog = new OpenFileDialog())
|
||||
//{
|
||||
// if (dialog.ShowDialog() == DialogResult.OK)
|
||||
// {
|
||||
|
||||
//string filePath = dialog.FileName;
|
||||
|
||||
var filePath = KeyFile + @"\PublicKey.xml";
|
||||
|
||||
using (StreamReader reader = new StreamReader(filePath))
|
||||
{
|
||||
return reader.ReadToEnd();
|
||||
}
|
||||
|
||||
// }
|
||||
//}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return "";
|
||||
//MessageBox.Show("配置公钥失败!", "提示");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取授权文件详细信息
|
||||
/// </summary>
|
||||
/// <param name="Path"></param>
|
||||
/// <returns></returns>
|
||||
public string GetLienceDataByFilePath(string Path)
|
||||
{
|
||||
try
|
||||
{
|
||||
//using (OpenFileDialog dialog = new OpenFileDialog())
|
||||
//{
|
||||
// if (dialog.ShowDialog() == DialogResult.OK)
|
||||
// {
|
||||
|
||||
//string filePath = dialog.FileName;
|
||||
|
||||
var filePath = KeyFile + @"\License.txt";
|
||||
|
||||
using (StreamReader reader = new StreamReader(filePath))
|
||||
{
|
||||
return reader.ReadToEnd();
|
||||
}
|
||||
|
||||
// }
|
||||
//}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return "";
|
||||
//MessageBox.Show("配置公钥失败!", "提示");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载授权文件
|
||||
/// </summary>
|
||||
public AuthorizeInfo LoadLienceFile(string LiencePath)
|
||||
{
|
||||
try
|
||||
{
|
||||
//using (OpenFileDialog dialog = new OpenFileDialog())
|
||||
//{
|
||||
// if (dialog.ShowDialog() == DialogResult.OK)
|
||||
// {
|
||||
|
||||
//string filePath = dialog.FileName;
|
||||
//licPath = filePath;
|
||||
|
||||
var LienceData = "";
|
||||
AuthorizeInfo authorizeInfo = new AuthorizeInfo();
|
||||
|
||||
using (StreamReader reader = new StreamReader(LiencePath))
|
||||
{
|
||||
//显示授权码
|
||||
LienceData = reader.ReadToEnd();
|
||||
|
||||
//显示机器码和时间
|
||||
var strs = LicenseServer.GetLicenseDetail(LienceData);
|
||||
if (strs == null)
|
||||
return null;
|
||||
|
||||
try
|
||||
{
|
||||
authorizeInfo.MachineCode = strs[0];
|
||||
|
||||
//北京为东8区,比UTC快8小时,转换时要加上这8小时
|
||||
authorizeInfo.StartTime = new DateTime(1970, 1, 1, 8, 0, 0).AddSeconds(Convert.ToInt64(strs[1]));
|
||||
authorizeInfo.EndTime = new DateTime(1970, 1, 1, 8, 0, 0).AddSeconds(Convert.ToInt64(strs[2]));
|
||||
authorizeInfo.LastTime = new DateTime(1970, 1, 1, 8, 0, 0).AddSeconds(Convert.ToInt64(strs[3]));
|
||||
|
||||
return authorizeInfo;
|
||||
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
MessageBox.Show("授权文件无效!", "提示");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// }
|
||||
//}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
MessageBox.Show("加载授权文件失败!", "提示");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#region 生成Lience文件
|
||||
|
||||
/// <summary>
|
||||
/// 生成Lience文件
|
||||
/// 生成授权文件需要:机器序列号(用户提供)+私钥(自己保密信息)+授权时间(自己控制信息)
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string GetLience(string MachineCode, string PrivateKey, DateTime startTime, DateTime endTime)
|
||||
{
|
||||
//生成授权文件需要:机器序列号+私钥+授权时间
|
||||
if (MachineCode == string.Empty)
|
||||
{
|
||||
MessageBox.Show("生成授权文件需要目标机器的序列号!", "提示");
|
||||
return "";
|
||||
}
|
||||
if (PrivateKey == string.Empty)
|
||||
{
|
||||
MessageBox.Show("生成授权文件需要使用私钥!", "提示");
|
||||
return "";
|
||||
}
|
||||
|
||||
//new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds().ToString();
|
||||
long start = new DateTimeOffset(startTime).ToUnixTimeSeconds();
|
||||
long end = new DateTimeOffset(endTime).ToUnixTimeSeconds();
|
||||
if (end <= start)
|
||||
{
|
||||
MessageBox.Show("授权的终止时间必须大于起始时间!", "提示");
|
||||
return "";
|
||||
}
|
||||
|
||||
string lic = string.Empty;
|
||||
|
||||
lic = LicenseServer.GenLicense(MachineCode, PrivateKey, start, end);
|
||||
if (lic == null)
|
||||
{
|
||||
MessageBox.Show("生成授权文件失败!", "提示");
|
||||
return "";
|
||||
}
|
||||
|
||||
return lic;
|
||||
|
||||
//try
|
||||
//{
|
||||
// using (FolderBrowserDialog dialog = new FolderBrowserDialog())
|
||||
// {
|
||||
// if (dialog.ShowDialog() == DialogResult.OK)
|
||||
// {
|
||||
// string savePath = dialog.SelectedPath;
|
||||
// using (StreamWriter writer = new StreamWriter(Path.Combine(savePath, "License.txt")))
|
||||
// {
|
||||
// writer.WriteLine(lic);
|
||||
// }
|
||||
// licPath = Path.Combine(savePath, "License.txt");
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
//catch (Exception)
|
||||
//{
|
||||
// MessageBox.Show("保存授权文件失败!", "提示");
|
||||
// return;
|
||||
//}
|
||||
|
||||
//licence = lic;
|
||||
//tbLic.Text = lic;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user