/**
* 影领授权云 - C# 接入示例
* 激活 / 验证 / 心跳 / 解绑
*
* .NET 6+ / System.Text.Json
*
* 签名规则:sign = HMAC-SHA256("{action}|{product}|{code}|{machine}|{ts}", api_key)
*/
using System;
using System.Linq;
using System.Net.Http;
using System.Net.NetworkInformation;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
public class LicenseClient
{
private const string ApiUrl = "https://pay.hbylsjcm.cn/api/verify";
private const string ApiKey = "your_api_key_here";
private const string Product = "your_product_code";
private static readonly HttpClient Http = new() { Timeout = TimeSpan.FromSeconds(15) };
private static string HmacSha256(string key, string message)
{
using var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(key));
return Convert.ToHexString(hmac.ComputeHash(Encoding.UTF8.GetBytes(message))).ToLowerInvariant();
}
/// 机器码:首个已启用网卡的 MAC 哈希,生产环境建议叠加硬盘 / 主板序列号
private static string GetMachineCode()
{
var mac = NetworkInterface.GetAllNetworkInterfaces()
.Where(n => n.OperationalStatus == OperationalStatus.Up
&& n.NetworkInterfaceType != NetworkInterfaceType.Loopback)
.Select(n => n.GetPhysicalAddress().ToString())
.FirstOrDefault(a => !string.IsNullOrEmpty(a))
?? Environment.MachineName;
return Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(mac))).ToLowerInvariant();
}
public async Task CallApi(string action, string code)
{
var machine = GetMachineCode();
var ts = DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString();
var sign = HmacSha256(ApiKey, $"{action}|{Product}|{code}|{machine}|{ts}");
var json = JsonSerializer.Serialize(new
{
action,
product = Product,
code,
machine,
ts,
sign,
});
using var content = new StringContent(json, Encoding.UTF8, "application/json");
using var response = await Http.PostAsync(ApiUrl, content);
var body = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize(body);
}
public Task Activate(string code) => CallApi("activate", code);
public Task Verify(string code) => CallApi("verify", code);
public Task Heartbeat(string code) => CallApi("heartbeat", code);
public Task Unbind(string code) => CallApi("unbind", code);
}
public class Program
{
public static async Task Main()
{
var client = new LicenseClient();
const string licenseCode = "YL-XXXX-XXXX-XXXX";
var activate = await client.Activate(licenseCode);
if (!activate.GetProperty("ok").GetBoolean())
{
Console.WriteLine($"激活失败: {activate.GetProperty("error").GetString()}");
return;
}
var verify = await client.Verify(licenseCode);
if (verify.GetProperty("ok").GetBoolean())
{
var info = verify.GetProperty("verify");
Console.WriteLine($"授权有效,到期时间: {info.GetProperty("expire_at")}");
}
// 运行期每 5 分钟心跳一次
using var timer = new PeriodicTimer(TimeSpan.FromMinutes(5));
while (await timer.WaitForNextTickAsync())
{
var hb = await client.Heartbeat(licenseCode);
if (!hb.GetProperty("ok").GetBoolean())
{
Console.WriteLine("心跳失败,退出");
return;
}
}
}
}