JeffreySu/WeiXinMPSDK · error · WxOpenException

SessionId无效(01)

Error message

SessionId无效(01)

What it means

Thrown by EncryptHelper.CheckSignature when SessionContainer.GetSession returns no session for the provided sessionId, i.e. the client-supplied SessionId does not correspond to any stored WxOpen session. Without a session there is no SessionKey against which to verify the rawData signature, so a WxOpenException with code (01) is raised.

Solutions

  1. Ensure the login flow calls JsCode2Session (creating the session) before any CheckSignature call and pass that sessionId
  2. Use a distributed cache (Redis) so sessions survive restarts and are shared across servers
  3. Have the client re-login to obtain a fresh SessionId when this error occurs
  4. Catch WxOpenException and return an auth-expired response prompting re-login

Example fix

// before
var ok = EncryptHelper.CheckSignature(sessionId, rawData, signature); // throws if sessionId expired
// after
var session = SessionContainer.GetSession(sessionId);
if (session == null) {
    // force re-login to get a new session
    return RedirectLogin();
}
var ok = EncryptHelper.CheckSignature(sessionId, rawData, signature);
Defensive patterns

Strategy: validation

Validate before calling

var session = SessionContainer.GetSession(sessionId);
if (session == null) {
    // session missing — trigger client re-login before CheckSignature
    return RequireRelogin();
}

Type guard

bool SessionExists(string sessionId) => SessionContainer.GetSession(sessionId) != null;

Try / catch

try {
    var ok = EncryptHelper.CheckSignature(sessionId, rawData, compareSignature);
} catch (WxOpenException ex) when (ex.Message.Contains("SessionId无效")) {
    return Unauthorized("session expired, please re-login");
}

Prevention

When it happens

Trigger: Calling EncryptHelper.CheckSignature(sessionId, rawData, compareSignature) with an invalid, expired, or evicted sessionId; passing a sessionId from a different cache instance or after cache restart; client sending a forged/garbage session id.

Common situations: Session cache expiration or memory cache restart between js2session call and signature check; multi-server deployment without a shared distributed cache; appId/appSecret changed so sessions were flushed; frontend sending stale sessionId from old login.

Understand the failure class

Background: Record Not Found Errors: "not found", RecordNotFound, and "was not found" — what they mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of JeffreySu/WeiXinMPSDK@be573f6f94 (2026-09-12). Data as JSON: /api/errors/a03264ca83b9d2be. Report an issue: GitHub.

Appendix: source

Thrown at src/Senparc.Weixin.WxOpen/src/Senparc.Weixin.WxOpen/Senparc.Weixin.WxOpen/Helpers/EncryptHelper.cs:111

            var signature = Senparc.CO2NET.Helpers.EncryptHelper.GetSha1(rawData + sessionKey);
            //Senparc.Weixin.Helpers.EncryptHelper.SHA1_Encrypt(rawData + sessionKey);
            return signature;
        }

        /// <summary>
        /// 比较签名是否正确
        /// </summary>
        /// <param name="sessionId"></param>
        /// <param name="rawData"></param>
        /// <param name="compareSignature"></param>
        /// <exception cref="WxOpenException">当SessionId或SessionKey无效时抛出异常</exception>
        /// <returns></returns>
        public static bool CheckSignature(string sessionId, string rawData, string compareSignature)
        {
            var sessionBag = SessionContainer.GetSession(sessionId);
            if (sessionBag == null)
            {
                throw new WxOpenException("SessionId无效(01)");
            }

            if (string.IsNullOrEmpty(sessionBag.SessionKey))
            {
                throw new WxOpenException("SessionKey无效(02)");
            }

            var signature = GetSignature(rawData, sessionBag.SessionKey);
            return signature == compareSignature;
        }

        #endregion

        #region 解密

        #region 私有方法

        private static byte[] AES_Decrypt(String Input, byte[] Iv, byte[] Key, int keySize = 128)

View on GitHub (pinned to be573f6f94)