コード例 #1
0
 /// <summary>
 /// Initializes a <see cref="WeixinMessageEventReceivedContext"/>
 /// </summary>
 /// <param name="context">The HTTP environment</param>
 /// <param name="options">The options for Weixin</param>
 /// <param name="sender">The sender respective for <see cref="MessageHandler{TC}}"/></param>
 /// <param name="args"><see cref="TEventArgs"/></param>
 public WeixinReceivedContext(
     WeixinMessageHandler sender,
     TEventArgs args,
     bool needEncrypt)
 {
     Sender      = sender;
     Args        = args;
     NeedEncrypt = needEncrypt;
 }
コード例 #2
0
        /// <summary>
        /// 微信公众号消息接收地址
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task InvokePost(HttpContext context)
        {
            HttpRequest request   = context.Request;
            var         signature = request.Query["signature"];
            var         timestamp = request.Query["timestamp"];
            var         nonce     = request.Query["nonce"];

            var websiteToken = _options.WebsiteToken;

            //context.Response.Clear();
            context.Response.ContentType = "text/plain;charset=utf-8";
            if (!SignatureHelper.ValidateSignature(signature, timestamp, nonce, websiteToken) && !_options.Debug)
            {
                var result = "这是一个微信程序,请用微信客户端访问!";
                await context.Response.WriteAsync(result);

                return;
            }

            var messageHandler = new WeixinMessageHandler();

            try
            {
                var result = await messageHandler.InitializeAsync(_options, context, _logger, _encryptor);

                //var result = await messageHandler.HandleAsync();
                //if (!result)
                //{
                //    await _next(context);
                //}
            }
            catch (Exception ex)
            {
                _logger.LogError(0, ex, "处理POST报文时发生异常");
            }
            finally
            {
                try { await messageHandler.TeardownAsync(); }
                catch (Exception)
                {
                    // Don't mask the original exception, if any
                }
            }
        }