Skip to content
Bitzsoft.Integrationsbitzsoft.integrations

Concept

身份认证与 SSO

身份认证域包含 MFA、SocialAuth、SsoRedirect、AzureAD、Captcha 五个子域,覆盖多因素认证、社交登录、SSO 跳转和验证码。

Last updated

身份认证与 SSO 不是单一域,而是多个子域的集合。它们各自独立解决一个认证相关的细分问题,部分子域之间有复用关系——比如 SsoRedirect 被 LegalDatabase 域委托调用。

子域总览

子域职责
MFA 多因素认证Bitzsoft.Integrations.MFATOTP / FIDO2 / 短信 / 邮箱验证码
SocialAuth 社交登录Bitzsoft.Integrations.SocialAuth10 家 OAuth 供应商登录
SsoRedirect SSO 跳转Bitzsoft.Integrations.SsoRedirect生成 SSO 跳转链接
AzureAD 微软 Entra IDBitzsoft.Integrations.AzureADGraph API、JWT、用户预配
Captcha 验证码Captcha + 3 家厂商 + .All阿里云 / 腾讯 / 极验

MFA 多因素认证

支持 5 种 MFA 方式:TOTP(基于时间的一次性密码)、FIDO2(无密码硬件认证)、短信、邮箱、恢复码。

核心服务接口:

public interface ITotpService // TOTP 密钥绑定与验证
public interface IFido2Service // FIDO2 凭证注册与断言
public interface IMfaCoordinator // 协调多因素流程,串联策略与各验证通道

MFA 用 builder 模式注册,支持策略配置(哪些操作强制 MFA、信任设备有效期等)。AddBitzsoftMFA 接受 Action<MFAOptions> 委托:

builder.Services.AddBitzsoftMFA(options =>
{
options.DefaultPolicy = MFAPolicyLevel.High;
options.TrustedDeviceLifetime = TimeSpan.FromDays(30);
});

注册后注入 IMfaCoordinator 统一发起和验证 MFA 流程。FIDO2 凭证存储、策略、日志等通过对应 Repository 接口接入,调用方提供实现。

SocialAuth 社交登录

支持 10 家 OAuth 供应商:

供应商Provider ID(配置键)枚举值
GooglegoogleSocialAuthProvider.Google
GitHubgithubSocialAuthProvider.GitHub
MicrosoftmicrosoftSocialAuthProvider.Microsoft
AppleappleSocialAuthProvider.Apple
SlackslackSocialAuthProvider.Slack
钉钉dingtalkSocialAuthProvider.DingTalk
企业微信wecomSocialAuthProvider.WeCom
飞书feishuSocialAuthProvider.Feishu
微信开放平台wechat_openSocialAuthProvider.WechatOpen
微信公众号wechat_mpSocialAuthProvider.WechatMp

核心接口 ISocialAuthService 统一发起授权和换取用户信息,内部由 IOAuthProviderFactory 按供应商分发到具体 IOAuthProvider 实现。内置 PKCE 支持(IPkceProvider)。只注册配置中 Enabled = true 的 Provider,每个 Provider 以 Typed HttpClient 注册,确保连接池隔离。

// ① 委托配置(Providers 字典用小写 Provider ID 作键)
builder.Services.AddBitzsoftSocialAuth(options =>
{
options.Providers["github"] = new ProviderConfig
{
Enabled = true,
ClientId = "...",
ClientSecret = "...",
RedirectUri = "https://app.example.com/callback/github",
};
});
// ② 从配置节绑定
builder.Services.AddBitzsoftSocialAuth(builder.Configuration.GetSection("SocialAuth"));

SsoRedirect SSO 跳转

生成带认证参数的 SSO 访问链接。这是 LegalDatabase 域委托调用的共享域——多数法律数据库厂商只有 SSO 跳转入口,没有 API 检索。

核心类型 SsoRedirectProvider,通过 SsoRedirectOptions.AuthMode 支持四种认证模式:

AuthMode说明是否需 HttpClient
UrlTemplate模板替换(userId / token / timestamp)
AesEncryptionAES 加密后拼链接
ApiRedirect调 API 拿 key 再跳转
Pipeline自定义步骤组合视步骤而定
// ① UrlTemplate 模式(最简单)
builder.Services.AddBitzsoftSsoRedirect(options =>
{
options.ProviderName = "威科先行";
options.SsoUrlTemplate = "https://www.wkinfo.com.cn/sso?uid={userId}&token={token}&t={timestamp}";
options.Token = "your-sso-token";
});
// ② ApiRedirect 模式(需 HttpClient,自动注册)
builder.Services.AddBitzsoftSsoRedirect(options =>
{
options.ProviderName = "探案家";
options.AuthMode = SsoAuthMode.ApiRedirect;
options.ApiUrl = "https://api.example.com/getKey";
options.ApiResponseUrlPath = "data.appKey";
});

AzureAD 微软 Entra ID

Microsoft Entra ID(原 Azure AD)集成。提供三块能力,各自独立注册:

能力接口DI 方法
Graph API 调用IAzureADGraphClientAddBitzsoftAzureADGraph()
JWT Bearer 认证(中间件配置)AddBitzsoftAzureADJwtBearer()
用户预配IAzureADProvisioningClientAddBitzsoftAzureADProvisioning()
// ① Graph API(配置通过 AddOptions 绑定,可链式扩展 Group/Role 映射)
builder.Services.AddBitzsoftAzureADGraph()
.WithGroupRoleMapping<MyGroupRoleMappingRepository>();
// ② JWT Bearer 认证(参数为认证方案名)
builder.Services.AddBitzsoftAzureADJwtBearer("AzureADJwtBearer");
// ③ 一键注册(含 Graph + 预配,Action 配置选项)
builder.Services.AddBitzsoftAzureAD(options =>
{
options.TenantId = "...";
options.ClientId = "...";
options.ClientSecret = "...";
});

Group/Role 映射、外部登录记录、用户查询通过对应 Repository 接口接入。

Captcha 验证码

统一三家验证码服务商的生成与服务端校验,采用”前端 SDK 自渲染 → 用户无感验证 → 前端提交 token → 后端校验 API”模式。

核心接口 ICaptchaProvider

public interface ICaptchaProvider
{
string Code { get; }
Task<CaptchaResult<CaptchaChallenge>> GenerateAsync(CaptchaGenerateRequest request, CancellationToken ct = default);
Task<CaptchaResult<CaptchaVerification>> VerifyAsync(CaptchaVerifyRequest request, CancellationToken ct = default);
}

厂商清单:

厂商CodeDI 方法
阿里云验证码 2.0AliyunAddBitzsoftAliyunCaptcha()
腾讯天御TencentAddBitzsoftTencentCaptcha()
极验 GeeTestGeetestAddBitzsoftGeetestCaptcha()
// ① 单厂商
builder.Services.AddBitzsoftAliyunCaptcha(builder.Configuration.GetSection("Captcha:Aliyun"));
// ② 全量聚合
builder.Services.AddBitzsoftCaptchaAll(builder.Configuration, "Captcha");

token 是一次性的,校验后即失效。

注册示例(多子域组合)

身份认证子域通常组合使用。一个典型 Web 应用的注册:

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddBitzsoftSocialAuth(builder.Configuration.GetSection("SocialAuth")); // 社交登录
builder.Services.AddBitzsoftMFA(); // 多因素认证(按需配 Action<MFAOptions>)
builder.Services.AddBitzsoftAzureADJwtBearer("AzureADJwtBearer"); // Entra ID JWT
builder.Services.AddBitzsoftCaptchaAll(builder.Configuration, "Captcha"); // 验证码

相关

100%

滚轮或按钮缩放 · 放大后拖动画面 · 双击切换 100% / 200%