SKIT.FlurlHttpClient.Wechat实现微信接口开发-服务器验证回调

1. 在微信公众后台配置

设置以下内容


服务器设置


设置完成后,先不要点击提交

2. SKIT.FlurlHttpClient.Wechat 编写WechatApiClient

public static WechatApiClient GetWechatApiClient()
        {
            MpEntity defaultMpEntity = MpEntity.GetDefault();
            var options = new WechatApiClientOptions()
            {
                AppId = defaultMpEntity.AppId,
                AppSecret = defaultMpEntity.AppSecret,
                PushToken = defaultMpEntity.Token
            };
            var client = new WechatApiClient(options);

            return client;
        }

MpEntity 是业务上的数据,主要定义,AppId, AppSecret, Token等微信公众号的基本信息

MpEntity定义如下


3. 编写微信公众号需要验签的接口控制器

对应的Url是 https://www.xx.com/mp/check 这样的控制器代码

        /// 
        /// 验证微信服务器配置
        /// 
        /// 
        public IActionResult Check()
        {
            WechatApiClient wechatApiClient = SourceCodeWebPlugInService.GetWechatApiClient();

            //得到当前请求的方法
            String curMethod = Request.Method.ToLower();
            LogHelper.WriteLog(#34;请求方法:{curMethod},url:{RequestHelper.Url()}");

            if (curMethod == "get")
            {
                //如果是get方法只有订阅号过来,其他都是Post

                //得到订阅号服务提交的url参数
                string signature = Request.Get("signature");
                string timestamp = Request.Get("timestamp");
                string nonce = Request.Get("nonce");
                string echostr = Request.Get("echostr");

                LogHelper.WriteLog("订阅号请求的url" + RequestHelper.Url());

                try
                {
                    bool isCheck = wechatApiClient.VerifyEventSignatureForEcho(timestamp, nonce, signature);
                    if (isCheck)
                    {
                        return Content(echostr);
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.WriteLog("公众号服务器验证异常" + ex.Message);
                }
                
            }
            else
            {
                //对消息等进行回复
            }
            return Content("无响应");

        }

//如果是get方法只有订阅号过来,其他都是Post

//得到订阅号服务提交的url参数

4. 微信公众号与应用开发服务器的之间的关系

http://www.xxxx.com/mp/check?signature=39c6aa7beeefecc47d2457425b5b1e56ba3fdf&echostr=66071ee623693343322 tamp=1661119432&nonce=1443728737

https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Access_Overview.html

展开阅读全文

页面更新:2024-05-17

标签:服务器   令牌   密钥   控制器   公众   接口   定义   参数   消息   地址   方法

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号

Top