XP.Common 类库中新增授权管理(License Management)功能模块,支持两种授权模式:CLMS 正式授权和临时测试模式。开发统一的授权服务接口,并在主项目中完成集成。
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
using System.Configuration;
|
||||
|
||||
namespace XP.Common.License.Configs
|
||||
{
|
||||
/// <summary>
|
||||
/// 授权配置加载器,从 App.config 读取授权相关配置项 | License configuration loader, reads license-related configuration from App.config
|
||||
/// </summary>
|
||||
public static class ConfigLoader
|
||||
{
|
||||
/// <summary>
|
||||
/// 配置键前缀 | Configuration key prefix
|
||||
/// </summary>
|
||||
private const string KeyPrefix = "License:";
|
||||
|
||||
/// <summary>
|
||||
/// LicenseMode 有效值集合 | Valid values for LicenseMode
|
||||
/// </summary>
|
||||
private static readonly int[] ValidLicenseModes = { 0, 885 };
|
||||
|
||||
/// <summary>
|
||||
/// LicenseState 有效值集合 | Valid values for LicenseState
|
||||
/// </summary>
|
||||
private static readonly int[] ValidLicenseStates = { 10, 20 };
|
||||
|
||||
/// <summary>
|
||||
/// 从 App.config 加载授权配置 | Load license configuration from App.config
|
||||
/// </summary>
|
||||
/// <returns>授权配置实体,缺失或无效配置项使用默认值 | License configuration entity, uses default values for missing or invalid items</returns>
|
||||
public static LicenseConfig LoadLicenseConfig()
|
||||
{
|
||||
var config = new LicenseConfig();
|
||||
|
||||
// 加载 LicenseMode | Load LicenseMode
|
||||
var licenseModeStr = ConfigurationManager.AppSettings[KeyPrefix + "LicenseMode"];
|
||||
if (int.TryParse(licenseModeStr, out var licenseMode) && IsValidLicenseMode(licenseMode))
|
||||
{
|
||||
config.LicenseMode = licenseMode;
|
||||
}
|
||||
|
||||
// 加载 ModuleId | Load ModuleId
|
||||
var moduleIdStr = ConfigurationManager.AppSettings[KeyPrefix + "ModuleId"];
|
||||
if (ushort.TryParse(moduleIdStr, out var moduleId) && IsValidModuleId(moduleId))
|
||||
{
|
||||
config.ModuleId = moduleId;
|
||||
}
|
||||
|
||||
// 加载 UseSma | Load UseSma
|
||||
var useSmaStr = ConfigurationManager.AppSettings[KeyPrefix + "UseSma"];
|
||||
if (bool.TryParse(useSmaStr, out var useSma))
|
||||
{
|
||||
config.UseSma = useSma;
|
||||
}
|
||||
|
||||
// 加载 LicenseState | Load LicenseState
|
||||
var licenseStateStr = ConfigurationManager.AppSettings[KeyPrefix + "LicenseState"];
|
||||
if (int.TryParse(licenseStateStr, out var licenseState) && IsValidLicenseState(licenseState))
|
||||
{
|
||||
config.LicenseState = licenseState;
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 验证 LicenseMode 值是否有效 | Validate whether LicenseMode value is valid
|
||||
/// </summary>
|
||||
/// <param name="value">待验证的值 | Value to validate</param>
|
||||
/// <returns>true 表示有效,false 表示无效 | true if valid, false if invalid</returns>
|
||||
private static bool IsValidLicenseMode(int value)
|
||||
{
|
||||
foreach (var valid in ValidLicenseModes)
|
||||
{
|
||||
if (value == valid) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 验证 ModuleId 值是否在有效范围内 | Validate whether ModuleId value is within valid range
|
||||
/// </summary>
|
||||
/// <param name="value">待验证的值 | Value to validate</param>
|
||||
/// <returns>true 表示有效,false 表示无效 | true if valid, false if invalid</returns>
|
||||
private static bool IsValidModuleId(ushort value)
|
||||
{
|
||||
return value >= 1 && value <= 65535;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 验证 LicenseState 值是否有效 | Validate whether LicenseState value is valid
|
||||
/// </summary>
|
||||
/// <param name="value">待验证的值 | Value to validate</param>
|
||||
/// <returns>true 表示有效,false 表示无效 | true if valid, false if invalid</returns>
|
||||
private static bool IsValidLicenseState(int value)
|
||||
{
|
||||
foreach (var valid in ValidLicenseStates)
|
||||
{
|
||||
if (value == valid) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user