コード例 #1
0
ファイル: WxOpenController.cs プロジェクト: kzbpp/WeiXinMPSDK
        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 ? "通过" : "不通过")}"
            }));
        }
コード例 #2
0
        public async Task <IActionResult> DecryptPhoneNumber([FromBody] JObject param)
        {
            if (!await _authorizationService.AuthorizeAsync(User, MiniProgramPermission.WeCharMiniProgramAccess))
            {
                return(Unauthorized(new { success = false, msg = "未授权访问" }));
            }
            var sessionBag = await SessionContainer.GetSessionAsync(param["sessionId"].Value <string>());

            if (sessionBag == null)
            {
                return(Json(new { success = false, msg = "请先登录!" }));
            }
            try
            {
                var phoneNumber = EncryptHelper.DecryptPhoneNumber(sessionBag.Key, param["encryptedData"].ToString(), param["iv"].ToString());

                //throw new WeixinException("解密PhoneNumber异常测试");//启用这一句,查看客户端返回的异常信息

                return(Json(new { success = true, phoneNumber = phoneNumber }));
            }
            catch (Exception ex)
            {
                return(Json(new { success = false, msg = ex.Message, param }));
            }
        }
コード例 #3
0
        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
            }));
        }
コード例 #4
0
        public async Task <SessionBag> RunTemplateTestAsync(string wxOpenAppId, string sessionId, string formId)
        {
            var sessionBag = await SessionContainer.GetSessionAsync(sessionId);

            var openId = sessionBag != null ? sessionBag.OpenId : "用户未正确登陆";

            string  title       = null;
            decimal price       = 1;//单位:分,实际使用过程中,通过数据库获取订单并读取
            string  productName = null;
            string  orderNumber = null;

            if (formId.StartsWith("prepay_id="))
            {
                formId = formId.Replace("prepay_id=", "");
                title  = "这是来自小程序支付的模板消息(仅测试接收,数据不一定真实)";

                //DPBMARK TenPay
                var cacheStrategy           = CacheStrategyFactory.GetObjectCacheStrategyInstance();
                var unifiedorderRequestData = await cacheStrategy.GetAsync <TenPayV3UnifiedorderRequestData>($"WxOpenUnifiedorderRequestData-{openId}"); //获取订单请求信息缓存

                var unifedorderResult = await cacheStrategy.GetAsync <UnifiedorderResult>($"WxOpenUnifiedorderResultData-{openId}");                     //获取订单信息缓存

                if (unifedorderResult != null && formId == unifedorderResult.prepay_id)
                {
                    price       = unifiedorderRequestData.TotalFee;
                    productName = unifiedorderRequestData.Body + "/缓存获取 prepay_id 成功";
                    orderNumber = unifiedorderRequestData.OutTradeNo;
                }
                else//DPBMARK_END
                {
                    productName = "缓存获取 prepay_id 失败";
                    orderNumber = "1234567890";
                }
                productName += " | 注意:这条消息是从小程序发起的!仅作为UI上支付成功的演示!不能确定支付真实成功! | prepay_id:";
                productName += unifedorderResult.prepay_id;//DPBMARK TenPay DPBMARK_END
            }
            else
            {
                title       = "在线购买(仅测试小程序接收模板消息,数据不一定真实)";
                productName = "商品名称-模板消息测试";
                orderNumber = "9876543210";
            }

            var data = new WxOpenTemplateMessage_PaySuccessNotice(title, SystemTime.Now, productName, orderNumber, price,
                                                                  "400-031-8816", "https://sdk.senparc.weixin.com");

            await Senparc.Weixin.WxOpen.AdvancedAPIs
            .Template.TemplateApi
            .SendTemplateMessageAsync(
                wxOpenAppId, openId, data.TemplateId, data, formId, "pages/index/index", "图书", "#fff00");

            return(sessionBag);
        }
コード例 #5
0
ファイル: WxOpenController.cs プロジェクト: yfccc/WeiXinMPSDK
        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 ? "通过" : "不通过")
            }));
        }
コード例 #6
0
ファイル: WxOpenController.cs プロジェクト: yfccc/WeiXinMPSDK
        public async Task <IActionResult> TemplateTest(string sessionId, string formId)
        {
            var templateMessageService = new TemplateMessageService();

            try
            {
                var sessionBag = await templateMessageService.RunTemplateTestAsync(WxOpenAppId, sessionId, formId);

                return(Json(new { success = true, msg = "发送成功,请返回消息列表中的【服务通知】查看模板消息。\r\n点击模板消息还可重新回到小程序内。" }));
            }
            catch (Exception ex)
            {
                var sessionBag = await SessionContainer.GetSessionAsync(sessionId);

                var openId = sessionBag != null ? sessionBag.OpenId : "用户未正确登陆";

                return(Json(new { success = false, openId = openId, formId = formId, msg = ex.Message }));
            }
        }
コード例 #7
0
ファイル: WxOpenController.cs プロジェクト: kzbpp/WeiXinMPSDK
        public async Task <IActionResult> TemplateTest(string sessionId, string formId)
        {
            //注意:2020年01月10日起,新发布的小程序将不能使用模板消息,请迁移至“订阅消息”功能。

            var templateMessageService = new TemplateMessageService();

            try
            {
                //var sessionBag = await templateMessageService.RunTemplateTestAsync(WxOpenAppId, sessionId, formId);

                return(Json(new { success = true, msg = "2020年01月10日起,新发布的小程序将不能使用模板消息,请迁移至“订阅消息”功能。" }));
            }
            catch (Exception ex)
            {
                var sessionBag = await SessionContainer.GetSessionAsync(sessionId);

                var openId = sessionBag != null ? sessionBag.OpenId : "用户未正确登陆";

                return(Json(new { success = false, openId = openId, formId = formId, msg = ex.Message }));
            }
        }
コード例 #8
0
        public async Task <IActionResult> CheckWxOpenSignature([FromBody] JObject param)
        {
            if (!await _authorizationService.AuthorizeAsync(User, MiniProgramPermission.WeCharMiniProgramAccess))
            {
                return(Unauthorized(new { success = false, msg = "未授权访问" }));
            }
            try
            {
                var sessionBag = await SessionContainer.GetSessionAsync(param["sessionId"].Value <string>());

                if (sessionBag == null)
                {
                    return(Json(new { success = false, msg = "请先登录!" }));
                }
                var checkSuccess = EncryptHelper.CheckSignature(sessionBag.Key, param["rawData"].Value <string>(), param["signature"].Value <string>());
                return(Json(new { success = checkSuccess, msg = checkSuccess ? "签名校验成功" : "签名校验失败" }));
            }
            catch (Exception ex)
            {
                return(Json(new { success = false, msg = ex.Message }));
            }
        }
コード例 #9
0
        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 ? "通过" : "不通过")
            }));
        }