DreamSlave.Wecom 1.6.0

dotnet add package DreamSlave.Wecom --version 1.6.0
                    
NuGet\Install-Package DreamSlave.Wecom -Version 1.6.0
                    
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="DreamSlave.Wecom" Version="1.6.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DreamSlave.Wecom" Version="1.6.0" />
                    
Directory.Packages.props
<PackageReference Include="DreamSlave.Wecom" />
                    
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 DreamSlave.Wecom --version 1.6.0
                    
#r "nuget: DreamSlave.Wecom, 1.6.0"
                    
#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 DreamSlave.Wecom@1.6.0
                    
#: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=DreamSlave.Wecom&version=1.6.0
                    
Install as a Cake Addin
#tool nuget:?package=DreamSlave.Wecom&version=1.6.0
                    
Install as a Cake Tool

企业微信相关方法封装

状态

当前为预览版,仅用于实验/学习,勿直接用于生产。

功能

  • AccessToken 获取 / 缓存(IMemoryCache)
  • Jsapi Ticket 获取 / 缓存(IMemoryCache)
  • 回调签名验证 / 加解密
  • 回调消息解析 (文本/事件/扫码/批量任务/通讯录变更等)
  • OAuth2 鉴权 & 用户基础/敏感信息
  • Web 登录鉴权
  • 应用消息发送(文本/Markdown/图片/语音/视频/卡片/图文等)
IWecomUnifiedService

通过 serviceName 区分多实例,内部使用 IMemoryCache 保存 token / ticket。

注册方式

// 多实例按需调用多次
builder.Services.AddWecomConfig("oauth2", cfg =>
{
    cfg.CorpID = "企业微信Id";
    cfg.CorpSecret = "Secret";
    cfg.AgentId = 1000001;
    cfg.Token = "";
    cfg.EncodingAesKey = "";
    cfg.AutoRefresh = true; // 后台自动刷新
});

获取 Js-SDK 签名

var dto = _wecom.GetJsapiTicketDto("oauth2", currentUrl);

OAuth2 鉴权

var authUrl = _wecom.BuildOAuth2Url("oauth2", callbackUrl, state);
// 回调里
var user = await _wecom.GetOAuth2UserInfoAsync("oauth2", code);
var detail = await _wecom.GetOAuth2UserDetailAsync("oauth2", user);

Web 登录

var loginUrl = _wecom.BuildWebLoginUrl("oauth2", callbackUrl, state);
var webUser = await _wecom.GetWebLoginUserInfoAsync("oauth2", code);

回调处理

后台已自动注册 WecomCommandExecService (按实例 serviceName)。

[HttpGet("/api/wecom/callback")] // 检测签名
public IActionResult Verify([FromQuery] Callback cb)
{
    if(!_wecom.CheckSignature("oauth2", cb)) return Content("签名错误");
    return Content(_wecom.DecryptEchostr("oauth2", cb));
}

[HttpPost("/api/wecom/callback")] // 处理消息
public async Task<IActionResult> Receive([FromQuery] Callback cb)
{
    using var reader = new StreamReader(Request.Body);
    var payload = await reader.ReadToEndAsync();
    var xml = _wecom.DecryptCallBackData("oauth2", payload);
    return Content(await _exec.HandleMessageAsync(xml));
}

自定义指令/事件处理

文本命令:

public class DeveloperHandler
{
    [WecomTextCommand("id")]
    public Task<string> IdAsync(IServiceProvider sp, MessageReceive msg)
        => Task.FromResult("您的ID:" + msg.fromUserName);
}

事件命令:

public class EventHandler
{
    [WecomEventCommand("enter_agent")] public Task<string> EnterAsync(IServiceProvider sp, MessageReceive m) => Task.FromResult("进入应用:"+m.AgentId);
}

支持作用域过滤:

[WecomHandlerScope("oauth2")] // 仅在 oauth2 实例生效

发送消息

await _wecom.SendTextMessageAsync("oauth2", "测试", "UserA");

自动刷新

当配置 AutoRefresh=true,会启动后台刷新服务:

  • AccessToken 提前 1 分钟过期续订
  • JsapiTicket 需自行按需调用刷新 (或调用 GetJsapiTicketDto 前确保已刷新)

免责声明

仅供学习/测试,不对稳定性与合规性负责。

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 is compatible.  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
1.6.0 27 9/28/2025
1.5.7 114 9/26/2025 1.5.7 is deprecated because it is no longer maintained and has critical bugs.
1.5.6 104 9/26/2025 1.5.6 is deprecated because it is no longer maintained and has critical bugs.
1.5.5 106 9/26/2025 1.5.5 is deprecated because it is no longer maintained and has critical bugs.
1.5.0 128 9/25/2025 1.5.0 is deprecated because it is no longer maintained and has critical bugs.
1.4.0 186 9/24/2025 1.4.0 is deprecated because it is no longer maintained and has critical bugs.
1.3.13 199 9/24/2025 1.3.13 is deprecated because it is no longer maintained and has critical bugs.
1.3.12 256 9/24/2025 1.3.12 is deprecated because it is no longer maintained and has critical bugs.
1.3.11 187 9/24/2025 1.3.11 is deprecated because it is no longer maintained and has critical bugs.
1.3.10 188 9/24/2025 1.3.10 is deprecated because it is no longer maintained and has critical bugs.
1.3.9 184 9/24/2025 1.3.9 is deprecated because it is no longer maintained and has critical bugs.
1.3.8 179 9/24/2025 1.3.8 is deprecated because it is no longer maintained and has critical bugs.
1.3.7 180 9/24/2025 1.3.7 is deprecated because it is no longer maintained and has critical bugs.
1.3.6 236 9/24/2025 1.3.6 is deprecated because it is no longer maintained and has critical bugs.
1.3.5 173 9/24/2025 1.3.5 is deprecated because it is no longer maintained and has critical bugs.
1.3.4 181 9/24/2025 1.3.4 is deprecated because it is no longer maintained and has critical bugs.
1.3.3 185 9/24/2025 1.3.3 is deprecated because it is no longer maintained and has critical bugs.
1.3.2 184 9/24/2025 1.3.2 is deprecated because it is no longer maintained and has critical bugs.
1.3.1 189 9/24/2025 1.3.1 is deprecated because it is no longer maintained and has critical bugs.
1.3.0 190 9/24/2025 1.3.0 is deprecated because it is no longer maintained and has critical bugs.
1.2.0 217 9/23/2025 1.2.0 is deprecated because it is no longer maintained and has critical bugs.
1.1.1 231 9/23/2025 1.1.1 is deprecated because it is no longer maintained and has critical bugs.
1.1.0 215 9/23/2025 1.1.0 is deprecated because it is no longer maintained and has critical bugs.
1.0.7 360 9/16/2025 1.0.7 is deprecated because it is no longer maintained and has critical bugs.
1.0.6 299 9/15/2025 1.0.6 is deprecated because it is no longer maintained and has critical bugs.
1.0.5 295 9/15/2025 1.0.5 is deprecated because it is no longer maintained and has critical bugs.
1.0.4 295 9/15/2025 1.0.4 is deprecated because it is no longer maintained and has critical bugs.
1.0.3 297 9/15/2025 1.0.3 is deprecated because it is no longer maintained and has critical bugs.
1.0.2 296 9/15/2025 1.0.2 is deprecated because it is no longer maintained and has critical bugs.
1.0.1 297 9/15/2025 1.0.1 is deprecated because it is no longer maintained and has critical bugs.
1.0.0 266 9/15/2025 1.0.0 is deprecated because it is no longer maintained and has critical bugs.