{"record":{"id":"c0f406a845fd2a0c","repo":"JeffreySu/WeiXinMPSDK","slug":"sessionid","errorCode":null,"errorMessage":"SessionId无效","messagePattern":"SessionId无效","errorType":"exception","errorClass":"WxOpenException","httpStatus":null,"severity":"error","filePath":"src/Senparc.Weixin.WxOpen/src/Senparc.Weixin.WxOpen/Senparc.Weixin.WxOpen/Helpers/EncryptHelper.cs","lineNumber":251,"sourceCode":"            var result = AES_Decrypt(encryptedData, aesIV, aesKey, keySize);\n            var resultStr = Encoding.UTF8.GetString(result);\n            return resultStr;\n        }\n\n        /// <summary>\n        /// 解密消息（通过SessionId获取）\n        /// </summary>\n        /// <param name=\"sessionId\"></param>\n        /// <param name=\"encryptedData\"></param>\n        /// <param name=\"iv\"></param>\n        /// <exception cref=\"WxOpenException\">当SessionId或SessionKey无效时抛出异常</exception>\n        /// <returns></returns>\n        public static string DecodeEncryptedDataBySessionId(string sessionId, string encryptedData, string iv)\n        {\n            var sessionBag = SessionContainer.GetSession(sessionId);\n            if (sessionBag == null)\n            {\n                throw new WxOpenException(\"SessionId无效\");\n            }\n\n            if (string.IsNullOrEmpty(sessionBag.SessionKey))\n            {\n                throw new WxOpenException(\"SessionKey无效\");\n            }\n\n            var resultStr = DecodeEncryptedData(sessionBag.SessionKey, encryptedData, iv);\n            return resultStr;\n        }\n\n\n        /// <summary>\n        /// 检查解密消息水印\n        /// </summary>\n        /// <param name=\"entity\"></param>\n        /// <param name=\"appId\"></param>\n        /// <returns>entity为null时也会返回false</returns>","sourceCodeStart":233,"sourceCodeEnd":269,"githubUrl":"https://github.com/JeffreySu/WeiXinMPSDK/blob/be573f6f94bdbf718dd5f6cdecb137fbc7ff651e/src/Senparc.Weixin.WxOpen/src/Senparc.Weixin.WxOpen/Senparc.Weixin.WxOpen/Helpers/EncryptHelper.cs#L233-L269","documentation":"Thrown by DecodeEncryptedDataBySessionId when SessionContainer.GetSession(sessionId) finds no session, so the encrypted data cannot be decrypted with a SessionKey. Raises WxOpenException \"SessionId无效\". Same family as CheckSignature's (01) error but in the decryption path.","triggerScenarios":"Calling DecodeEncryptedDataBySessionId(sessionId, encryptedData, iv) with an expired/unknown sessionId; using a sessionId obtained before a cache restart or on a different server without shared cache; client sending a wrong or fabricated id.","commonSituations":"Memory-cache eviction/timeout between login and decrypt (e.g. phone-number decrypt long after login); load-balanced servers each with local cache; stale frontend session after server redeploy.","solutions":["Decrypt encrypted data soon after login while the session is still cached, or re-login to refresh the session","Configure a shared distributed cache for SessionContainer across all instances","Check session existence first with SessionContainer.GetSession(sessionId) and trigger re-login when null","Catch WxOpenException and return a session-expired error code to the mini-program"],"exampleFix":"// before\nvar json = EncryptHelper.DecodeEncryptedDataBySessionId(sessionId, encryptedData, iv); // throws\n// after\nif (SessionContainer.GetSession(sessionId) == null) {\n    return LoginExpired(); // client re-runs wx.login\n}\nvar json = EncryptHelper.DecodeEncryptedDataBySessionId(sessionId, encryptedData, iv);","handlingStrategy":"validation","validationCode":"if (SessionContainer.GetSession(sessionId) == null) {\n    return RequireRelogin(); // re-run wx.login + jscode2session\n}","typeGuard":"bool CanDecrypt(string sessionId) => SessionContainer.GetSession(sessionId) != null;","tryCatchPattern":"try {\n    var json = EncryptHelper.DecodeEncryptedDataBySessionId(sessionId, encryptedData, iv);\n} catch (WxOpenException ex) when (ex.Message.Contains(\"SessionId无效\")) {\n    return Unauthorized(\"session expired\");\n}","preventionTips":["Decrypt encrypted payloads promptly after login while the session is cached","Configure SessionContainer with a distributed cache (Redis) for multi-instance deployments","Notify the mini-program to re-login whenever decryption reports an invalid session","Avoid long delays between wx.login and encrypted-data decryption"],"tags":["weixin","wxopen","session","decryption","cache"],"backgroundTag":"record-not-found","analyzedSha":"be573f6f94bdbf718dd5f6cdecb137fbc7ff651e","analyzedAt":"2026-09-12T10:01:50.733Z","contentChangedAt":"2026-09-12T10:01:50.733Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}