LuBan.Wechat 2026.7.17.1

dotnet add package LuBan.Wechat --version 2026.7.17.1
                    
NuGet\Install-Package LuBan.Wechat -Version 2026.7.17.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="LuBan.Wechat" Version="2026.7.17.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="LuBan.Wechat" Version="2026.7.17.1" />
                    
Directory.Packages.props
<PackageReference Include="LuBan.Wechat" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add LuBan.Wechat --version 2026.7.17.1
                    
#r "nuget: LuBan.Wechat, 2026.7.17.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package LuBan.Wechat@2026.7.17.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=LuBan.Wechat&version=2026.7.17.1
                    
Install as a Cake Addin
#tool nuget:?package=LuBan.Wechat&version=2026.7.17.1
                    
Install as a Cake Tool

English | 中文

LuBan.Wechat

作者: yswenli | 联系邮箱: yswenli@outlook.com | 代码仓库: https://github.com/yswenli/luban-framework

微信公众号、小程序、企业微信、微信支付——一个工厂类搞定全部微信生态对接。


为什么选择 LuBan.Wechat?

微信生态对接涉及公众号 API、企业微信 API、微信支付 V3 三套完全不同的 SDK,加上回调签名验证、XML 消息解析、AES 加解密等繁琐细节,往往让开发者疲于应付。
LuBan.Wechat 封装了统一的客户端工厂和完整的消息协议模型,让你用最少的代码完成微信全场景集成。


快速预览

// 获取微信公众号客户端
var client = WechatClientFactory.Create(EnumWechatType.Api);

// 获取微信支付客户端
var payClient = WechatClientFactory.Create(EnumWechatType.Pay);

// 获取企业微信客户端
var corpClient = WechatClientFactory.Create(EnumWechatType.Corp);

技术栈 & 依赖

类别 组件 说明
运行时 .NET 8.0
框架依赖 LuBan.Web.Core, LuBan.Service, LuBan.Common LuBan 框架核心

安装

dotnet add package LuBan.Wechat

功能全景

客户端工厂

WechatClientFactory 基于 ConcurrentDictionary 缓存客户端实例,同一类型只创建一次:

方法 说明
Create(EnumWechatType) 按类型获取客户端(Api/Pay/App/Corp)
CreateWechatApiClient(options) 创建公众号客户端
CreateWechatAppClient(options) 创建微信 App 客户端
CreateWechatTenpayClient(options) 创建微信支付客户端
CreateWechatCorpClient(options) 创建企业微信客户端

回调处理

说明
WechatCallBackCall 公众号回调:签名验证、XML 消息解析、回复构建
WechatCorpCallBackCall 企业微信回调处理

消息协议模型

完整的微信消息收发模型(29 个模型类):

接收消息(Receive):

  • ReceiveText — 文本消息
  • ReceiveImage — 图片消息
  • ReceiveVoice — 语音消息
  • ReceiveVideo — 视频消息
  • ReceiveLocation — 位置消息
  • ReceiveLink — 链接消息
  • ReceiveEvent — 事件消息

回复消息(Reply):

  • ReplyText — 回复文本
  • ReplyImage — 回复图片
  • ReplyVoice — 回复语音
  • ReplyVideo — 回复视频
  • ReplyMusic — 回复音乐
  • ReplyNews — 回复图文

企业微信

说明
WechatCorpClient 企业微信客户端,包含 WechatWorkClientWechatCorpSuiteClient
IWechatCorpClient 企业微信客户端接口
WechatCorpSignature 企业微信签名工具
WechatBizMsgCryptor 企业微信消息加解密
CryptographyUtil 密码学工具

其他工具

说明
WechatClientInterceptor HTTP 请求拦截器,自动日志记录
XmlResolve XML 消息解析工具
TencentLocationServiceClient 腾讯位置服务客户端

代码示例

处理公众号回调

public class WechatController : BaseApiController
{
    // GET 验证签名
    [HttpGet("wechat/callback")]
    public IActionResult Verify(string signature, string timestamp, string nonce, string echostr)
    {
        var input = new ReceiveInput
        {
            token = "your_token",
            signature = signature,
            timestamp = timestamp,
            nonce = nonce
        };
        var call = new WechatCallBackCall(input);
        if (call.AccessValid())
            return Content(echostr);
        return Content("验证失败");
    }

    // POST 接收消息
    [HttpPost("wechat/callback")]
    public async Task<IActionResult> ReceiveMessage()
    {
        var bytes = await WebApp.HttpContext.GetRequestBodyBytesAsync();
        var result = WechatCallBackCall.XmlMsgRevolve(bytes, out string xml, out Receive receive);

        if (receive is ReceiveText textMsg)
        {
            // 回复文本消息
            var reply = new ReplyText
            {
                ToUserName = textMsg.FromUserName,
                FromUserName = textMsg.ToUserName,
                Content = $"你说的是:{textMsg.Content}"
            };
            return Content(WechatCallBackCall.Reply(reply), "application/xml");
        }

        return Content("success");
    }
}

生成 JS-SDK 签名

var signature = WechatCallBackCall.Signature(
    jsapi_ticket: "your_ticket",
    url: "https://example.com/page",
    out string noncestr,
    out long timestamp
);

调用微信 API

var client = WechatClientFactory.Create(EnumWechatType.Api) as WechatApiClient;

// 获取用户信息
var request = new CreateWechatAccessTokenRequest();
var response = await client.ExecuteAsync(request);

配置

appsettings.json 中添加微信配置:

{
  "WechatOptions": {
    "WechatAppId": "wx_your_appid",
    "WechatAppSecret": "your_secret",
    "WxOpenAppId": "wx_open_appid",
    "WxOpenAppSecret": "wx_open_secret",
    "CorpId": "your_corp_id",
    "CorpSecret": "your_corp_secret"
  },
  "WechatPayOptions": {
    "MerchantId": "your_merchant_id",
    "MerchantV3Secret": "your_v3_secret",
    "MerchantCertificateSerialNumber": "your_cert_sn",
    "MerchantCertificatePrivateKey": "/certs/apiclient_key.pem"
  }
}

小贴士

  1. 客户端缓存WechatClientFactory 自动缓存客户端实例,无需手动管理生命周期
  2. 拦截器:所有客户端自动注入 WechatClientInterceptor,记录请求/响应日志
  3. 企业微信WechatCorpClient 同时支持自建应用(Client)和第三方代开发(SuiteClients
  4. 回调验证AccessValid() 方法自动完成 SHA1 签名校验
  5. XML 解析XmlMsgRevolve 自动识别消息类型并反序列化为对应子类

许可证

Copyright (c) yswenli. All Rights Reserved.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2026.7.17.1 36 7/17/2026
2026.7.13.2 88 7/13/2026
2026.7.13.1 83 7/13/2026
2026.7.12.2 98 7/12/2026
2026.7.12.1 92 7/12/2026
2026.7.11.2 87 7/11/2026

LuBan Framework中微信对接相关业务核心项目