Kauth.Sdk 1.0.1

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

Kauth.Sdk C# 对接文档

官网:https://kauth.cn
包名:Kauth.Sdk(Install-Package Kauth.Sdk / dotnet add package Kauth.Sdk)
目标框架:.NET 8

加密、签名由 SDK 内部完成,作者只调公开方法。KauthClient 是实例:一份配置一份 token,可多开,不要做成进程级单例。

所有接口返回 KauthResult / KauthResult<T>:

Code 含义
200 成功(IsSuccess == true)
0 网络不通、超时等通讯失败
其它(如 400、403) 服务端业务错误,看 Message

1. 快速对接

1.1 安装

dotnet add package Kauth.Sdk

Visual Studio:右键项目 → 管理 NuGet 程序包 → 搜索 Kauth.Sdk。

1.2 初始化

程序 ID、程序密钥、RSA 公钥在商家后台 程序管理 / 密钥配置。

using Kauth.Sdk;
using Kauth.Sdk.Models;

var client = new KauthClient(new KauthOptions
{
    ApiDomain = "https://api.kauth.cn",   // 接入点
    ProgramId = "你的程序ID",
    ProgramSecret = "你的程序密钥",
    MerchantPublicKey = "你的RSA公钥",   // X.509 Base64,可带 PEM 头
    Timeout = TimeSpan.FromSeconds(30)     // 可选,默认 30 秒
});

建议先调一次 GetProgramDetailAsync()。返回 code=200 说明网络和签名都正常。

1.3 登录

三种登录成功后,SDK 会自动把 token 写到 client.Token,后续心跳、用户信息等会自动带上。

卡密登录(最常用):

var login = await client.KaLoginAsync(new KaLoginRequest
{
    KaPwd = "卡密",
    DeviceId = DeviceId.GetDefault(), // 绑定设备请换成你自己稳定的设备码
    PlatformType = "windows"           // 可选:windows / android / ios 等
});

if (!login.IsSuccess)
{
    // login.Code / login.Message
    return;
}

var token = login.Data?.Token;
var nick = login.Data?.NickName;

账号密码登录:

var login = await client.PwdLoginAsync(new PwdLoginRequest
{
    LoginName = "账号",
    Password = "密码",
    DeviceId = DeviceId.GetDefault()
});

试用登录:

var login = await client.TrialLoginAsync(new TrialLoginRequest
{
    DeviceId = DeviceId.GetDefault(),
    PlatformType = "windows"
});

登录需要验证码时:先 GetCaptchaAsync(),把返回的 Uuid 和用户输入的验证码填进 CaptchaUuid / CaptchaCode。

DeviceId.GetDefault() 只是可选默认值(Windows 用 MachineGuid 的 MD5)。卡密绑定以你传入的值为准,请自己生成并持久化设备码。


2. 保持心跳

登录成功后必须启用心跳。SDK 内部调 pong,作者监听事件,在心跳失败时停软件。

建议间隔 ≥ 2 分钟。小于 30 秒会被强制改成 2 分钟。

2.1 判断逻辑(SDK 已实现)

每次心跳结果按下表处理:

pong 返回
  ├─ Code == 200
  │     成功。网络失败计数清零,继续下一次。
  │
  ├─ Code != 200 且能拿到服务端错误码(Code > 200,如 401/403)
  │     立即心跳失败,停止心跳。
  │     典型:token 失效、卡密被停、被踢下线。
  │
  └─ 网络异常(Code == 0,或超时、断网、Code < 200)
        失败计数 +1。
        连续次数 < maxFailures → 继续下一次。
        连续次数 ≥ maxFailures → 也算心跳失败,停止心跳。
        中途只要有一次 200,计数清零。

默认 maxFailures = 3(连续 3 次网络失败)。

2.2 推荐写法

client.Heartbeat.Changed += (_, e) =>
{
    switch (e.Event)
    {
        case HeartbeatEvent.Success:
            // 心跳正常,可刷新 UI「在线」
            break;

        case HeartbeatEvent.ServerFail:
            // 服务端明确失败(错误码非 200),立即停软件
            MessageBox.Show($"心跳失败:{e.Code} {e.Message}");
            Application.Exit();
            break;

        case HeartbeatEvent.NetworkFail:
            // 网络异常,尚未达到上限。e.FailureCount 为当前连续次数
            break;

        case HeartbeatEvent.Stopped:
            // 心跳已停止:手动 Stop、服务端失败、或网络连续失败超限
            if (e.Code != 0 || e.FailureCount > 0)
            {
                MessageBox.Show($"心跳已停止:{e.Message}");
                Application.Exit();
            }
            break;
    }
};

client.Heartbeat.Start(
    interval: TimeSpan.FromMinutes(2),
    maxFailures: 3   // 网络连续失败超过 3 次 → 心跳失败
);

软件退出时:

client.Heartbeat.Stop();
client.Dispose();
事件 何时触发 作者该做什么
Success pong 返回 200 保持运行
ServerFail 错误码非 200(业务失败) 立即停软件
NetworkFail 一次网络异常,未超限 可提示「网络不稳」,继续等
Stopped 心跳循环结束 若不是用户主动退出,按失败处理

手动 Stop() 也会收到 Stopped(Code=0,Message=手动停止)。用一个标志区分「用户点退出」和「心跳失败」,避免误弹窗。


3. SDK API 一览

命名空间:Kauth.Sdk、Kauth.Sdk.Models。
未注明的接口登录后才可调(需要 Token)。

3.1 登录与账号

方法 说明 请求 返回 Data
GetCaptchaAsync(CaptchaRequest?) 图形验证码。不传则自动生成 uuid Uuid CaptchaBase64, Uuid
KaLoginAsync(KaLoginRequest) 卡密登录 KaPwd, DeviceId, 可选验证码 / PlatformType / HardwareId LoginResponse
PwdLoginAsync(PwdLoginRequest) 账号密码登录 LoginName, Password, DeviceId LoginResponse
TrialLoginAsync(TrialLoginRequest) 试用登录 DeviceId LoginResponse
RegisterAsync(RegisterRequest) 账号注册 LoginName, Password, KaPassword RegisterResponse
LogoutAsync() 退出登录 无 无
ChangePasswordAsync(ChangePasswordRequest) 修改密码 LoginName, OldPassword, NewPassword, ConfirmPassword 无
RechargeAsync(RechargeRequest) 卡密给账号充值 LoginName, KaPassword 无
RechargeKaAsync(KaRechargeKaRequest) 卡密给卡密充值 CardPwd, RechargeCardPwd 无

LoginResponse:UserId, NickName, Token, PongInterval, VmpSerial。

3.2 登录后状态

方法 说明 返回 Data
PongAsync() 心跳(一般用 Heartbeat.Start,不必自己循环调) 无
GetUserInfoAsync() 到期时间、剩余次数、是否试用 UserInfo
Heartbeat.Start(interval, maxFailures) 启动心跳 见第 2 节
Heartbeat.Stop() 停止心跳 —

UserInfo:UserId, ServerExpireTime, ServerRemainNum, ServerType, Trial。

3.3 设备解绑

方法 说明 请求
UnbindDeviceAsync(UnbindDeviceRequest) 账号+密码解绑 LoginName, Password, DeviceId
UnbindDeviceKaPwdAsync(UnbindDeviceKaPwdRequest) 卡密解绑 KaPwd, DeviceId
UnbindCurrentDeviceAsync() 已登录状态下解绑当前设备 无

3.4 程序与时间

方法 说明 返回 Data
GetProgramDetailAsync() 程序名、公告、强制更新、当前版本 ProgramDetailResponse
GetServerTimeAsync() 服务器时间 ServerTimeResponse

无需登录即可调用,适合启动时探测配置是否正确。

3.5 自定义配置

方法 说明 请求 返回 Data
GetUserConfigAsync() 读账号配置 无 Config
UpdateUserConfigAsync(UpdateConfigRequest) 写账号配置 Config 无
GetKaConfigAsync() 读卡密配置 无 Config
UpdateKaConfigAsync(UpdateConfigRequest) 写卡密配置 Config 无

3.6 远程变量 / 远程数据 / 云函数

方法 说明 请求 返回 Data
GetRemoteVarAsync(GetRemoteVarRequest) 读远程变量 Key Key, Value, Type
GetRemoteDataAsync(GetRemoteVarRequest) 读远程数据 Key 同上
AddRemoteDataAsync(RemoteDataAddRequest) 新增远程数据 Key, Value, Type? 无
UpdateRemoteDataAsync(RemoteDataUpdateRequest) 更新远程数据 Key, Value 无
DeleteRemoteDataAsync(RemoteDataDeleteRequest) 删除远程数据 Key 无
CallFunctionAsync(CallFunctionRequest) 调云函数 FunctionName, FunctionParams FunctionName, Result, Success

CallFunctionParam:ParamName, ParamValue。

3.7 脚本

方法 说明 请求 返回 Data
GetNewestScriptAsync(GetNewestScriptRequest?) 最新脚本版本 可选 ScriptName ScriptName, VersionNumber, VersionDescription, ScriptType, ScriptReleaseTime
GetScriptDownloadV2Async(ScriptDownloadRequest) 下载脚本 ScriptName, VersionNumber? DownloadUrl, Md5, FileSize, ScriptContent
ReportScriptErrorAsync(ScriptErrorReportRequest) 上报脚本错误 ScriptName, ErrorMessage, StackTrace, Line, Os, OsVersion, DeviceId 无

3.8 其它成员

成员 说明
client.Token 当前登录 token,登录成功自动赋值,也可手动赋值
client.Options 初始化配置
client.Heartbeat 心跳控制器
client.Dispose() 停心跳并释放 HttpClient
DeviceId.GetDefault() 可选默认设备码

窗口示例:examples/KaWin。控制台示例:examples/KaLogin。

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.
  • net8.0

    • No dependencies.

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.0.1 99 9/9/2026
1.0.0 89 9/9/2026