基于上上签开放平台 API 实现,采用 OAuth2 + RSA256 签名机制。
包信息
- NuGet 包:
Bitzsoft.Integrations.ElectronicSignature.BestSign - 接口:
IElectronicSignatureProvider - 鉴权:OAuth2 + RSA256 签名(ClientId / PrivateKey)
- Provider ID:
BestSign
配置
| 字段 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
ClientId | string | 是 | — | OAuth2 客户端标识 |
ClientSecret | string | 是 | — | OAuth2 客户端密钥 |
DeveloperId | string | 是 | — | 开发者标识 |
PrivateKey | string | 是 | — | 开发者 RSA 私钥(PKCS#8 PEM 格式,用于请求签名) |
PublicKey | string | 是 | — | 平台 RSA 公钥(PKCS#8 PEM 格式,用于回调验签) |
BaseUrl | string | 否 | https://openapi.bestsign.cn | API 基地址(测试 https://demotest.bestsign.cn) |
Timeout | TimeSpan | 否 | 00:00:30 | HTTP 请求超时 |
HttpClientName | string | 否 | BestSignElectronicSignatureProvider | 命名 HttpClient 标识 |
{ "BestSign": { "ClientId": "your-client-id", "ClientSecret": "your-client-secret", "DeveloperId": "your-developer-id", "PrivateKey": "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----", "PublicKey": "-----BEGIN PUBLIC KEY-----\n...\n-----END PUBLIC KEY-----", "BaseUrl": "https://openapi.bestsign.cn" }}注册
using Bitzsoft.Integrations.ElectronicSignature.BestSign;
// 方式一:IConfiguration(默认读取 "BestSign" 配置节)services.AddBitzsoftBestSignElectronicSignature(configuration);
// 方式二:委托配置services.AddBitzsoftBestSignElectronicSignature(options =>{ options.ClientId = "your-client-id"; options.ClientSecret = "your-client-secret"; options.DeveloperId = "your-developer-id"; options.PrivateKey = File.ReadAllText("private_key.pem"); options.PublicKey = File.ReadAllText("public_key.pem"); options.BaseUrl = "https://openapi.bestsign.cn";});使用示例
var createResult = await provider.CreateContractAsync(new SimpleContractRequest{ Title = "合作协议", FileName = "contract.pdf", FileData = await File.ReadAllBytesAsync("contract.pdf"), SignerName = "钱七", SignerPhone = "13500000000"});
var contractId = createResult.Data!;var detail = await provider.GetContractDetailAsync(contractId);var bytes = await provider.DownloadContractAsync(contractId);鉴权机制
- 请求签名:使用开发者 RSA 私钥对请求内容做 SHA256WithRSA 签名。
- 回调验签:使用平台 RSA 公钥验证回调签名。
已知限制
私钥和公钥必须为 PKCS#8 PEM 格式。统一接口覆盖极简场景,复杂业务通过 partial class 扩展。