• 微信公众号开发--.Net Core实现微信消息加解密


    1:准备工作

    进入微信公众号后台设置微信服务器配置参数(注意:Token和EncodingAESKey必须和微信服务器验证参数保持一致,不然验证不会通过)。

    2:基本配置

    设置为安全模式

    3、代码实现(主要分为验证接口和消息处理接口):

    /// <summary>
    /// 验证接口
    /// </summary>
    /// <param name="signature">签名</param>
    /// <param name="timestamp">时间戳</param>
    /// <param name="nonce"></param>
    /// <param name="echostr"></param>
    /// <returns></returns>
    [HttpGet, Route("Message")]
    [AllowAnonymous]
    public ActionResult MessageGet(string signature, string timestamp, string nonce, string echostr)
    {
    if (new SecurityHelper().CheckSignature(signature, timestamp, nonce, _settings.Value.Token))
    {
    return Content(echostr);
    }
    return Content("");
    }
    
    /// <summary>
    /// 接收消息并处理和返回相应结果
    /// </summary>
    /// <param name="msg_signature">当加密模式时才会有该变量(消息签名)</param>
    /// <param name="signature">签名</param>
    /// <param name="timestamp">时间戳</param>
    /// <param name="nonce"></param>
    /// <returns></returns>
    [HttpPost, Route("Message")]
    [AllowAnonymous]
    public ActionResult MessagePost(string msg_signature, string signature, string timestamp, string nonce)
    {
    try
    {
    if (!new SecurityHelper().CheckSignature(signature, timestamp, nonce, _settings.Value.Token))
    {
    return Content(null);
    }
    using (Stream stream = HttpContext.Request.Body)
    {
    byte[] buffer = new byte[HttpContext.Request.ContentLength.Value];
    stream.Read(buffer, 0, buffer.Length);
    string content = Encoding.UTF8.GetString(buffer);
    if (!string.IsNullOrWhiteSpace(msg_signature)) // 消息加密模式
    {
    string decryptMsg = string.Empty;
    var wxBizMsgCrypt = new WXBizMsgCrypt(_settings.Value.Token, _settings.Value.EncodingAESKey, _settings.Value.AppId);
    int decryptResult = wxBizMsgCrypt.DecryptMsg(msg_signature, timestamp, nonce, content, ref decryptMsg);
    if (decryptResult == 0 && !string.IsNullOrWhiteSpace(decryptMsg))
    {
    string resultMsg = new WechatMessageHelper().MessageResult(decryptMsg);
    string sEncryptMsg = string.Empty;
    if (!string.IsNullOrWhiteSpace(resultMsg))
    {
    int encryptResult = wxBizMsgCrypt.EncryptMsg(resultMsg, timestamp, nonce, ref sEncryptMsg);
    if (encryptResult == 0 && !string.IsNullOrWhiteSpace(sEncryptMsg))
    {
    return Content(sEncryptMsg);
    }
    }
    }
    }
    else // 消息未加密码处理
    {
    string resultMsg = new WechatMessageHelper().MessageResult(content);
    return Content(resultMsg);
    }
    return Content(null);
    }
    }
    catch (Exception ex)
    {
    _logger.LogError("接收消息并处理和返回相应结果异常:", ex);
    return Content(null);
    }
    }

    加解密实现(微信公众号官网有源码)

  • 相关阅读:
    前端工具webpack4.0各个击破——html部分
    云下IDC和云上VPC如何高速稳定互联?——云专线接入方案
    程序猿必备!最简单的颈椎操
    16节课搞懂大数据,视频教程限时免费领取
    【DevCloud · 敏捷智库】如何玩转每日站会?
    华为云实时数据处理“三剑客”
    一统江湖的大前端React.js-从开发者到工程师
    关于【微服务】,你必须了解这些
    【鲲鹏来了】华为云鲲鹏弹性云服务器 KC1一文全掌握(5)
    【昇腾学院】昇腾AI处理器软件栈--框架管理器(Framework)离线模型生成
  • 原文地址:https://www.cnblogs.com/Agui520/p/8377216.html
Copyright © 2020-2023  润新知