如何将WinDev函数代码转换为C# .NET Core 6 Web API控制器函数
WinDev许可证验证逻辑转.NET Core 6 Web API控制器实现
原WinDev代码核心逻辑梳理
- 拼接
APPLICENSEID与用户输入的许可证编号,生成待加密字符串 - 加密该字符串后调用许可证校验接口
- 校验返回错误码
E100/E101/E102/E103时标记许可证无效并提示错误 - 校验通过则解密返回结果,提取许可证所有者、过期日期、用户数等信息
- 将验证结果写入本地INI文件,切换数据库连接并在非测试模式下检查数据库状态
.NET Core 6 Web API控制器实现
首先定义许可证信息模型:
public class LicenseInfo { public bool IsValid { get; set; } public string Owner { get; set; } public DateTime LimitDate { get; set; } public int NumberOfUsers { get; set; } public string Description { get; set; } }
然后实现控制器,假设加密、校验、解密等操作封装在LicenseService中(需自行实现对应逻辑):
using Microsoft.AspNetCore.Mvc; using System.IO; [ApiController] [Route("api/[controller]")] public class LicenseController : ControllerBase { private readonly LicenseService _licenseService; private readonly IConfiguration _configuration; private readonly bool _inTestMode; // 注入依赖,测试模式可通过配置文件设置 public LicenseController(LicenseService licenseService, IConfiguration configuration) { _licenseService = licenseService; _configuration = configuration; _inTestMode = configuration.GetValue<bool>("LicenseSettings:InTestMode"); } [HttpPost("validate")] public IActionResult ValidateLicense([FromBody] LicenseValidationRequest request) { // 1. 拼接待加密字符串(对应原代码sLic) var appLicenseId = _configuration.GetValue<string>("LicenseSettings:AppLicenseId"); string rawLicense = $"{appLicenseId};{request.LicenseNumber}"; // 2. 加密字符串 string encryptedLicense = _licenseService.CryptString(rawLicense); // 3. 调用许可证校验 string checkResult = _licenseService.CheckLicense(encryptedLicense); // 4. 处理错误码 var errorCodes = new[] { "E100", "E101", "E102", "E103" }; if (errorCodes.Contains(checkResult)) { return BadRequest(new { IsValid = false, Message = $"错误:{checkResult}" }); } // 5. 清理结果并解密 string cleanedResult = checkResult.Replace(Environment.NewLine, string.Empty); LicenseInfo licenseInfo = _licenseService.DecryptLicense(cleanedResult); licenseInfo.IsValid = true; // 6. 存储验证结果(原代码写入INI,Web环境建议用配置/数据库替代) // 示例:写入用户目录下的login.ini(Web环境需注意权限) string userDataDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); string iniPath = Path.Combine(userDataDir, "login.ini"); WriteIniValue(iniPath, "<LS>", "<info>", cleanedResult); // 7. 切换数据库连接并检查(需自行实现数据库操作逻辑) _licenseService.ChangeDatabaseConnection(); if (!_inTestMode) { _licenseService.CheckDatabase(); } return Ok(new { IsValid = true, LicenseInfo = licenseInfo, Message = "许可证验证成功" }); } // 简易INI写入实现(可替换为第三方库如Nini) private void WriteIniValue(string filePath, string section, string key, string value) { // 自行实现INI文件写入逻辑,或使用现成库 // 示例逻辑仅作参考,实际需处理文件不存在、section不存在等情况 if (!File.Exists(filePath)) File.Create(filePath).Close(); var lines = File.ReadAllLines(filePath).ToList(); int sectionIndex = lines.FindIndex(line => line.Trim() == $"[{section}]"); if (sectionIndex == -1) { lines.Add($"[{section}]"); sectionIndex = lines.Count - 1; } int keyIndex = lines.FindIndex(sectionIndex + 1, line => line.TrimStart().StartsWith($"{key}=")); if (keyIndex != -1) { lines[keyIndex] = $"{key}={value}"; } else { lines.Insert(sectionIndex + 1, $"{key}={value}"); } File.WriteAllLines(filePath, lines); } } // 验证请求模型 public class LicenseValidationRequest { public string LicenseNumber { get; set; } } // 许可证服务接口(需自行实现具体加密/校验/解密逻辑) public class LicenseService { public string CryptString(string rawString) { // 实现原WinDev的License_Crypt_String逻辑 throw new NotImplementedException(); } public string CheckLicense(string encryptedLicense) { // 实现原WinDev的License_Check逻辑(可能调用外部接口或本地校验) throw new NotImplementedException(); } public LicenseInfo DecryptLicense(string encryptedResult) { // 实现原WinDev的License_Decrypt逻辑 throw new NotImplementedException(); } public void ChangeDatabaseConnection() { // 实现原WinDev的Database_Change_Connexion逻辑 throw new NotImplementedException(); } public void CheckDatabase() { // 实现原WinDev的Check_DB逻辑 throw new NotImplementedException(); } }
关键说明
- 原WinDev中的全局变量(如
SAI_License_Owner)在Web API中替换为LicenseInfo模型属性,可通过API返回给客户端或存入会话/数据库 - INI文件写入在Web环境下需注意权限问题,建议改用配置文件(如
appsettings.json)或数据库存储验证结果 LicenseService中的加密、校验、解密等方法需要根据原WinDev代码的具体逻辑实现,确保与原逻辑一致
内容的提问来源于stack exchange,提问作者Dinushe Jayasekera
相关产品推荐
相关产品推荐

