Bitzsoft.Integrations.Captcha
1.0.4
dotnet add package Bitzsoft.Integrations.Captcha --version 1.0.4
NuGet\Install-Package Bitzsoft.Integrations.Captcha -Version 1.0.4
<PackageReference Include="Bitzsoft.Integrations.Captcha" Version="1.0.4" />
<PackageVersion Include="Bitzsoft.Integrations.Captcha" Version="1.0.4" />
<PackageReference Include="Bitzsoft.Integrations.Captcha" />
paket add Bitzsoft.Integrations.Captcha --version 1.0.4
#r "nuget: Bitzsoft.Integrations.Captcha, 1.0.4"
#:package Bitzsoft.Integrations.Captcha@1.0.4
#addin nuget:?package=Bitzsoft.Integrations.Captcha&version=1.0.4
#tool nuget:?package=Bitzsoft.Integrations.Captcha&version=1.0.4
Bitzsoft.Integrations.Captcha
验证码抽象层 -- 统一第三方验证码(阿里云验证码 2.0 / 腾讯天御 / 极验 GeeTest v4)的接口定义与基础模型。
本包仅含抽象层(接口、工厂、模型、异常)。各厂商实现见独立的子包:
Captcha.Aliyun/Captcha.Tencent/Captcha.Geetest, 或一步到位的聚合包Captcha.All。
功能特性
- 统一契约:一套
ICaptchaProvider接口覆盖三家厂商,消费方不感知厂商差异 - Provider 工厂:
ICaptchaProviderFactory按供应商编码(aliyun/tencent/geetest)运行时解析 Provider - 无感验证统一模式:三家均采用「前端 SDK 自渲染 → 用户无感验证 → 前端提交 token → 后端二次校验」
- 统一结果模型:
CaptchaResult<T>/CaptchaVerification/CaptchaChallenge,含成功/失败/供应商原始错误码 - 安全优先:
fail-closed校验语义(响应字段缺失一律判失败),密钥不落日志 - 降级友好:第三方不可用时返回
Fail,宿主可回退自有验证码(如 SVG 图形码)
安装
.NET CLI
dotnet add package Bitzsoft.Integrations.Captcha
PackageReference
<PackageReference Include="Bitzsoft.Integrations.Captcha" Version="1.0.0" />
通常无需单独安装本包,安装任意一个厂商实现包或聚合包时会自动引入。
核心契约
ICaptchaProvider
public interface ICaptchaProvider
{
// 供应商编码("aliyun" / "tencent" / "geetest")
string Code { get; }
// 生成验证码挑战(返回前端 SDK 初始化凭证,无感验证码不发网络请求)
Task<CaptchaResult<CaptchaChallenge>> GenerateAsync(CaptchaGenerateRequest request, CancellationToken ct = default);
// 校验前端回传的 token(服务端二次校验,token 一次性)
Task<CaptchaResult<CaptchaVerification>> VerifyAsync(CaptchaVerifyRequest request, CancellationToken ct = default);
}
ICaptchaProviderFactory
public interface ICaptchaProviderFactory
{
ICaptchaProvider GetProvider(string code); // 按编码解析
IEnumerable<string> GetRegisteredCodes(); // 已注册的供应商编码
}
核心类型一览
| 类型 | 说明 |
|---|---|
ICaptchaProvider |
验证码 Provider 标准契约(生成 + 校验) |
ICaptchaProviderFactory |
Provider 工厂(按 Code 解析) |
ICaptchaConfigProvider<TOptions> |
配置提供器抽象(支持 IOptions / 数据库 / 配置中心) |
CaptchaResult<T> |
带数据的校验/生成结果(IsSuccess / Data / ErrorCode / ErrorMessage) |
CaptchaResult |
无数据结果(仅成功/失败) |
CaptchaChallenge |
生成结果(前端 SDK 凭证:AppKey / AppId / SceneId / ScriptUrl / Challenge) |
CaptchaGenerateRequest |
生成请求(Type / Scene / ExternalUserId / RemoteIp) |
CaptchaVerifyRequest |
校验请求(Token + 厂商专用字段 LotNumber / CaptchaOutput / PassToken / GenTime / Randstr) |
CaptchaVerification |
校验结果(Passed / Score / VerifyCode / RawData) |
CaptchaException |
验证码统一异常(携带 ProviderName / ErrorCode / ProviderMessage) |
CaptchaProviderCode |
供应商编码常量(Aliyun / Tencent / Geetest) |
供应商编码对照表
| 编码 | 实现类 | 所在包 | 平台 |
|---|---|---|---|
aliyun |
AliyunCaptchaProvider |
Captcha.Aliyun |
阿里云验证码 2.0 |
tencent |
TencentCaptchaProvider |
Captcha.Tencent |
腾讯天御验证码 |
geetest |
GeetestCaptchaProvider |
Captcha.Geetest |
极验 GeeTest v4 |
使用模式
完整的注册与使用示例见各厂商包 README 或聚合包
Captcha.AllREADME。
// 通过工厂按编码解析(推荐:风控引擎等消费方按配置动态选择厂商)
var factory = sp.GetRequiredService<ICaptchaProviderFactory>();
var provider = factory.GetProvider("aliyun");
// 生成前端 SDK 凭证
var challenge = await provider.GenerateAsync(new CaptchaGenerateRequest { Type = CaptchaType.Intelligent });
// 校验前端回传 token
var result = await provider.VerifyAsync(new CaptchaVerifyRequest { Token = frontEndToken, RemoteIp = userIp });
if (result.IsSuccess && result.Data!.Passed) { /* 校验通过 */ }
安全说明
- fail-closed:阿里云响应缺
VerifyResult字段时一律判失败,绝不静默放行 - token 一次性:所有厂商的验证 token 校验后即失效,不可重放
- 密钥脱敏:
AccessKeySecret/SecretKey/AppSecretKey/CaptchaKey等敏感字段须配合 RequestLogging 脱敏配置,不落入审计日志 - 腾讯 AppSecretKey:腾讯
DescribeCaptchaResult接口要求 AppSecretKey 随请求体传输(系腾讯既定设计),务必确保该字段被日志脱敏
依赖
| 包 | 说明 |
|---|---|
Bitzsoft.Integrations.Compatibility |
基础工具库(参数校验 / 哈希 / 编码兼容) |
相关包
- Bitzsoft.Integrations.Captcha.Aliyun -- 阿里云验证码 2.0 实现
- Bitzsoft.Integrations.Captcha.Tencent -- 腾讯天御验证码实现
- Bitzsoft.Integrations.Captcha.Geetest -- 极验 GeeTest v4 实现
- Bitzsoft.Integrations.Captcha.All -- 聚合包(一次引用全部实现)
- Bitzsoft.Integrations.SocialAuth -- 社交登录集成(同属身份认证分类)
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 is compatible. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. 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 is compatible. 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. |
-
net10.0
- Bitzsoft.Integrations.Compatibility (>= 1.0.4)
- Bitzsoft.Integrations.Core (>= 1.0.4)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.10)
-
net5.0
- Bitzsoft.Integrations.Compatibility (>= 1.0.4)
- Bitzsoft.Integrations.Core (>= 1.0.4)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 5.0.0)
-
net8.0
- Bitzsoft.Integrations.Compatibility (>= 1.0.4)
- Bitzsoft.Integrations.Core (>= 1.0.4)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.10)
NuGet packages (4)
Showing the top 4 NuGet packages that depend on Bitzsoft.Integrations.Captcha:
| Package | Downloads |
|---|---|
|
Bitzsoft.Integrations.Captcha.All
验证码聚合包 — 包含阿里云 / 腾讯天御 / 极验 GeeTest 全部实现 |
|
|
Bitzsoft.Integrations.Captcha.Geetest
极验 GeeTest v4 验证码实现 — 前端 gt.js 自渲染 + 服务端 validate 二次校验 |
|
|
Bitzsoft.Integrations.Captcha.Aliyun
阿里云验证码 2.0 实现 — 前端 SDK 自渲染 + 服务端 VerifyIntelligentCaptcha 二次校验 |
|
|
Bitzsoft.Integrations.Captcha.Tencent
腾讯天御验证码实现 — 前端 TCaptcha 自渲染 + 服务端 DescribeCaptchaResult 二次校验 |
GitHub repositories
This package is not used by any popular GitHub repositories.