AppConfigHelper.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System;
  2. using System.Configuration;
  3. namespace ivf_tl_Operate.Helpers
  4. {
  5. /// <summary>
  6. /// M5-01-2 / M5-02:统一配置读写封装(operate 单一数据源 App.config 的集中接管点)。
  7. /// - 强类型容错读取(缺键返回默认值而非 NPE,缓解 R8"改一处漏一处起不来")。
  8. /// - 加密项读写经 <see cref="CryptoHelper"/>(passWord / engineerPwd 等本机凭据)。
  9. /// - <see cref="MigratePlaintextCredentials"/>:启动一次性明文→密文迁移(幂等)。
  10. /// 分层标注([D8] 待确认):
  11. /// 本地层:urlIp/urlPort/mqttIp/mqttPort/kfkaIP/kfkaPort/cacheDisk/Language/outInter/tlNum/houseEnabled/userName/passWord(加密)/engineerPwd(加密)
  12. /// 数据库层(TLSetting,不在此封装):autoFocusTime/videoFps/cropNum/heapDate/keepDate/cleanSurfaceData
  13. /// 暂留本地、待 D8 定归属:csTime/gbTime/VentNum/... 换气/CCD 类业务键
  14. /// [M7] 容错与迁移随合并进程运行验证(本地不可构建/运行)。
  15. /// </summary>
  16. public static class AppConfigHelper
  17. {
  18. /// <summary>工程师口令的 config 键(M5-02-3 治理 tl13579)。</summary>
  19. public const string EngineerPwdKey = "engineerPwd";
  20. /// <summary>工程师口令缺省值(首次无值回退;迁移时加密回写)。</summary>
  21. public const string DefaultEngineerPwd = "tl13579";
  22. /// <summary>容错读取字符串,缺键/空返回默认值。</summary>
  23. public static string GetString(string key, string defaultValue = "")
  24. {
  25. try
  26. {
  27. var v = ConfigurationManager.AppSettings[key];
  28. return string.IsNullOrEmpty(v) ? defaultValue : v;
  29. }
  30. catch
  31. {
  32. return defaultValue;
  33. }
  34. }
  35. /// <summary>容错读取整数,缺键/解析失败返回默认值。</summary>
  36. public static int GetInt(string key, int defaultValue = 0)
  37. {
  38. return int.TryParse(GetString(key, null), out var r) ? r : defaultValue;
  39. }
  40. /// <summary>
  41. /// 读取加密项并解密(旧明文原样返回,由迁移逻辑负责回写)。缺键返回默认值。
  42. /// </summary>
  43. public static string GetDecrypted(string key, string defaultValue = "")
  44. {
  45. var raw = GetString(key, null);
  46. if (string.IsNullOrEmpty(raw)) return defaultValue;
  47. return CryptoHelper.Decrypt(raw);
  48. }
  49. /// <summary>
  50. /// 写回普通键(沿用 OpenExeConfiguration + RefreshSection,对齐 AppData.SetApp 写法)。
  51. /// </summary>
  52. public static void Save(string key, string value)
  53. {
  54. try
  55. {
  56. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  57. if (config.AppSettings.Settings[key] == null)
  58. config.AppSettings.Settings.Add(key, value);
  59. else
  60. config.AppSettings.Settings[key].Value = value;
  61. config.Save(ConfigurationSaveMode.Modified);
  62. ConfigurationManager.RefreshSection("appSettings");
  63. }
  64. catch
  65. {
  66. // 写失败不抛出(避免阻断 UI/启动);[M7] 运行环境核查落盘。
  67. }
  68. }
  69. /// <summary>加密后写回(用于 passWord / engineerPwd 等凭据)。</summary>
  70. public static void SaveEncrypted(string key, string plain)
  71. {
  72. Save(key, CryptoHelper.Encrypt(plain));
  73. }
  74. /// <summary>
  75. /// M5-02-5:启动一次性凭据迁移(幂等)。
  76. /// (a) passWord:明文(旧值,如 123456)→密文回写;已是密文则跳过。
  77. /// (b) engineerPwd:键缺失→以默认 tl13579 加密建键;明文→密文回写。
  78. /// 仅当存在需迁移的明文时才写盘,已全部加密则不触碰文件(幂等)。
  79. /// </summary>
  80. public static void MigratePlaintextCredentials()
  81. {
  82. try
  83. {
  84. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  85. bool changed = false;
  86. // (a) passWord 明文 → 密文
  87. var pw = config.AppSettings.Settings["passWord"];
  88. if (pw != null && !string.IsNullOrEmpty(pw.Value) && !CryptoHelper.IsEncrypted(pw.Value))
  89. {
  90. pw.Value = CryptoHelper.Encrypt(pw.Value);
  91. changed = true;
  92. }
  93. // (b) engineerPwd 缺键或明文 → 加密建键/回写(治理 tl13579)
  94. var ep = config.AppSettings.Settings[EngineerPwdKey];
  95. if (ep == null)
  96. {
  97. config.AppSettings.Settings.Add(EngineerPwdKey, CryptoHelper.Encrypt(DefaultEngineerPwd));
  98. changed = true;
  99. }
  100. else if (!string.IsNullOrEmpty(ep.Value) && !CryptoHelper.IsEncrypted(ep.Value))
  101. {
  102. ep.Value = CryptoHelper.Encrypt(ep.Value);
  103. changed = true;
  104. }
  105. if (changed)
  106. {
  107. config.Save(ConfigurationSaveMode.Modified);
  108. ConfigurationManager.RefreshSection("appSettings");
  109. }
  110. }
  111. catch
  112. {
  113. // 迁移失败不阻断启动;[M7] 运行环境核查首次迁移。
  114. }
  115. }
  116. /// <summary>读取工程师口令明文(解密;键缺失/解密失败回退默认 tl13579)。</summary>
  117. public static string GetEngineerPwd()
  118. {
  119. var v = GetDecrypted(EngineerPwdKey, DefaultEngineerPwd);
  120. return string.IsNullOrEmpty(v) ? DefaultEngineerPwd : v;
  121. }
  122. }
  123. }