Example #1
0
        public async Task <IActionResult> DecodeEncryptedData(string type, string sessionId, string encryptedData, string iv)
        {
            DecodeEntityBase decodedEntity = null;

            try
            {
                switch (type.ToUpper())
                {
                case "USERINFO":    //wx.getUserInfo()
                    decodedEntity = EncryptHelper.DecodeUserInfoBySessionId(
                        sessionId,
                        encryptedData, iv);
                    break;

                default:
                    break;
                }
            }
            catch (Exception ex)
            {
                WeixinTrace.SendCustomLog("EncryptHelper.DecodeUserInfoBySessionId 方法出错",
                                          $@"sessionId: {sessionId}
encryptedData: {encryptedData}
iv: {iv}
sessionKey: { (await SessionContainer.CheckRegisteredAsync(sessionId)
                ? (await SessionContainer.GetSessionAsync(sessionId)).SessionKey
                : "未保存sessionId")}

异常信息:
{ex.ToString()}
");
            }

            //检验水印
            var checkWatermark = false;

            if (decodedEntity != null)
            {
                checkWatermark = decodedEntity.CheckWatermark(WxOpenAppId);

                //保存用户信息(可选)
                if (checkWatermark && decodedEntity is DecodedUserInfo decodedUserInfo)
                {
                    var sessionBag = await SessionContainer.GetSessionAsync(sessionId);

                    if (sessionBag != null)
                    {
                        await SessionContainer.AddDecodedUserInfoAsync(sessionBag, decodedUserInfo);
                    }
                }
            }

            //注意:此处仅为演示,敏感信息请勿传递到客户端!
            return(Json(new
            {
                success = checkWatermark,
                //decodedEntity = decodedEntity,
                msg = $"水印验证:{(checkWatermark ? "通过" : "不通过")}"
            }));
        }
Example #2
0
        public ActionResult DecodeEncryptedData(string type, string sessionId, string encryptedData, string iv)
        {
            DecodeEntityBase decodedEntity = null;

            switch (type.ToUpper())
            {
            case "USERINFO":    //wx.getUserInfo()
                decodedEntity = Senparc.Weixin.WxOpen.Helpers.EncryptHelper.DecodeUserInfoBySessionId(
                    sessionId,
                    encryptedData, iv);
                break;

            default:
                break;
            }

            //检验水印
            var checkWartmark = false;

            if (decodedEntity != null)
            {
                checkWartmark = decodedEntity.CheckWatermark(WxOpenAppId);
            }

            //注意:此处仅为演示,敏感信息请勿传递到客户端!
            return(Json(new
            {
                success = checkWartmark,
                //decodedEntity = decodedEntity,
                msg = string.Format("水印验证:{0}",
                                    checkWartmark ? "通过" : "不通过")
            }));
        }
Example #3
0
 /// <summary>
 /// 检查解密消息水印
 /// </summary>
 /// <param name="entity"></param>
 /// <param name="appId"></param>
 /// <returns>entity为null时也会返回false</returns>
 public static bool CheckWatermark(this DecodeEntityBase entity, string appId)
 {
     if (entity == null)
     {
         return(false);
     }
     return(entity.watermark.appid == appId);
 }
        public async Task <IActionResult> DecodeEncryptedData([FromBody] JObject param)
        {
            if (!await _authorizationService.AuthorizeAsync(User, MiniProgramPermission.WeCharMiniProgramAccess))
            {
                return(Unauthorized(new { success = false, msg = "未授权访问" }));
            }
            DecodeEntityBase decodedEntity = null;

            try
            {
                switch (param["type"].Value <string>().ToUpper())
                {
                case "USERINFO":     //wx.getUserInfo()
                    decodedEntity = EncryptHelper.DecodeUserInfoBySessionId(param["sessionId"].Value <string>(),
                                                                            param["encryptedData"].Value <string>(), param["iv"].Value <string>());
                    break;

                default:
                    break;
                }
            }
            catch
            {
            }

            //检验水印
            var checkWatermark = false;
            var openId         = "";

            if (decodedEntity != null)
            {
                checkWatermark = decodedEntity.CheckWatermark(_miniProgramSetting.WxOpenAppId);

                //保存用户信息(可选)
                if (checkWatermark && decodedEntity is DecodedUserInfo decodedUserInfo)
                {
                    var sessionBag = await SessionContainer.GetSessionAsync(param["sessionId"].Value <string>()).ConfigureAwait(true);

                    if (sessionBag != null)
                    {
                        await SessionContainer.AddDecodedUserInfoAsync(sessionBag, decodedUserInfo).ConfigureAwait(true);
                    }
                }

                var userInfo = (DecodedUserInfo)decodedEntity;
                openId = userInfo.openId;
                // 下面可以做持久化将小程序用户创建或者更新到数据库
            }

            //注意:此处仅为演示,敏感信息请勿传递到客户端!
            return(Json(new
            {
                success = checkWatermark,
                decodedEntity = decodedEntity,
                msg = $"水印验证:{(checkWatermark ? "通过" : "不通过")}",
                openId = openId
            }));
        }
Example #5
0
        public async Task <IActionResult> DecodeEncryptedData(string type, string sessionId, string encryptedData, string iv)
        {
            DecodeEntityBase decodedEntity = null;

            switch (type.ToUpper())
            {
            case "USERINFO":    //wx.getUserInfo()
                decodedEntity = Senparc.Weixin.WxOpen.Helpers.EncryptHelper.DecodeUserInfoBySessionId(
                    sessionId,
                    encryptedData, iv);
                break;

            default:
                break;
            }

            //检验水印
            var checkWatermark = false;

            if (decodedEntity != null)
            {
                checkWatermark = decodedEntity.CheckWatermark(WxOpenAppId);

                //保存用户信息(可选)
                if (checkWatermark && decodedEntity is DecodedUserInfo decodedUserInfo)
                {
                    var sessionBag = await SessionContainer.GetSessionAsync(sessionId);

                    if (sessionBag != null)
                    {
                        await SessionContainer.AddDecodedUserInfoAsync(sessionBag, decodedUserInfo);
                    }
                }
            }


            //注意:此处仅为演示,敏感信息请勿传递到客户端!
            return(Json(new
            {
                success = checkWatermark,
                //decodedEntity = decodedEntity,
                msg = string.Format("水印验证:{0}",
                                    checkWatermark ? "通过" : "不通过")
            }));
        }
Example #6
0
        public object Do_DecodeEncryptedData(object param)
        {
            DecodeEncryptedDataParam decodeEncryptedDataParam = JsonConvert.DeserializeObject <DecodeEncryptedDataParam>(param.ToString());

            if (decodeEncryptedDataParam == null)
            {
                throw new ApiException(CodeMessage.InvalidParam, "InvalidParam");
            }

            DecodeEntityBase decodedEntity = null;

            switch (decodeEncryptedDataParam.type.ToUpper())
            {
            case "USERINFO":    //wx.getUserInfo()
                decodedEntity = EncryptHelper.DecodeUserInfoBySessionId(
                    decodeEncryptedDataParam.token,
                    decodeEncryptedDataParam.encryptedData, decodeEncryptedDataParam.iv);
                break;

            default:
                break;
            }
            //检验水印
            var checkWartmark = false;

            if (decodedEntity != null)
            {
                checkWartmark = decodedEntity.CheckWatermark(Global.APPID);
            }

            if (checkWartmark)
            {
                return(new { check = checkWartmark });
            }
            else
            {
                throw new ApiException(CodeMessage.SenparcCode, "校验失败");
            }
        }
        public async Task <IActionResult> DecodeEncryptedData(string type, string sessionId, string encryptedData, string iv)
        {
            DecodeEntityBase      decodedEntity = null;
            CoreCmsUserWeChatInfo userInfo      = null;

            try
            {
                switch (type.ToUpper())
                {
                case "USERINFO":    //wx.getUserInfo()
                    decodedEntity = EncryptHelper.DecodeUserInfoBySessionId(sessionId, encryptedData, iv);
                    break;

                default:
                    break;
                }
            }
            catch (Exception ex)
            {
                WeixinTrace.SendCustomLog("EncryptHelper.DecodeUserInfoBySessionId 方法出错",
                                          $@"sessionId: {sessionId}encryptedData: {encryptedData}iv: {iv}sessionKey: { (await SessionContainer.CheckRegisteredAsync(sessionId)
                ? (await SessionContainer.GetSessionAsync(sessionId)).SessionKey
                : "未保存sessionId")}异常信息:{ex.ToString()}");
            }

            //检验水印
            var checkWatermark = false;

            if (decodedEntity != null)
            {
                checkWatermark = decodedEntity.CheckWatermark(WxOpenAppId);

                //保存用户信息(可选)
                if (checkWatermark && decodedEntity is DecodedUserInfo decodedUserInfo)
                {
                    var sessionBag = await SessionContainer.GetSessionAsync(sessionId);

                    if (sessionBag != null)
                    {
                        await SessionContainer.AddDecodedUserInfoAsync(sessionBag, decodedUserInfo);
                    }
                    //更新数据库讯息
                    userInfo = _userWeChatInfoServices.QueryByClause(p => p.openid == decodedUserInfo.openId);
                    if (userInfo == null)
                    {
                        userInfo            = new CoreCmsUserWeChatInfo();
                        userInfo.type       = (int)GlobalEnumVars.UserAccountTypes.微信小程序;
                        userInfo.openid     = decodedUserInfo.openId;
                        userInfo.sessionKey = sessionBag.SessionKey;
                        userInfo.unionId    = decodedUserInfo.unionId;
                        userInfo.avatar     = decodedUserInfo.avatarUrl;
                        userInfo.nickName   = decodedUserInfo.nickName;
                        userInfo.gender     = decodedUserInfo.gender;
                        userInfo.language   = "";
                        userInfo.city       = decodedUserInfo.city;
                        userInfo.province   = decodedUserInfo.province;
                        userInfo.country    = decodedUserInfo.country;
                        userInfo.mobile     = "";
                        userInfo.createTime = DateTime.Now;
                        var id = _userWeChatInfoServices.Insert(userInfo);
                        if (id > 0)
                        {
                            userInfo.id = id;
                            _userWeChatInfoServices.Update(p => new CoreCmsUserWeChatInfo()
                            {
                                userId = id
                            }, p => p.id == id);
                        }
                    }
                    else
                    {
                        userInfo.gender   = decodedUserInfo.gender;
                        userInfo.city     = decodedUserInfo.city;
                        userInfo.avatar   = decodedUserInfo.avatarUrl;
                        userInfo.country  = decodedUserInfo.country;
                        userInfo.nickName = decodedUserInfo.nickName;
                        userInfo.province = decodedUserInfo.province;
                        userInfo.unionId  = decodedUserInfo.unionId;
                        userInfo.gender   = decodedUserInfo.gender;
                        _userWeChatInfoServices.Update(userInfo);
                    }
                }
            }

            //注意:此处仅为演示,敏感信息请勿传递到客户端!
            return(Json(new
            {
                success = checkWatermark,
                userInfo = userInfo,
                msg = string.Format("水印验证:{0}",
                                    checkWatermark ? "通过" : "不通过")
            }));
        }