GenAuth DocsDocuments
Concepts
User Guide
Development Integration
AgentAuth
Metadata
Development Integration
Multi-tenant (beta)
Console Documentation
Multi-tenant Console
Tenant Console
Saas Application Demo
Concepts
User Guide
Development Integration
AgentAuth
Metadata
Development Integration
Multi-tenant (beta)
Console Documentation
Multi-tenant Console
Tenant Console
Saas Application Demo
Old version
  • Single Page Web Application

  • Mobile and Client Applications

  • Standard Web Application

  • Framework Integration

  • Others

¶ 安装

Update time: 2025-07-23 07:34:21
Edit

¶ Nuget

Install-Package GenAuth.CSharp.SDK

Github 仓库: https://github.com/Authing/authing-csharp-sdk (opens new window)

¶ 初始化

初始化 ManagementClient 需要使用 accessKeyId 和 accessKeySecret 参数:

using GenAuth.CSharp.SDK.Models;
using GenAuth.CSharp.SDK.Services;

ManagementClientOptions options = new ManagementClientOptions()
{
	AccessKeyId = "YOUR_ACCESS_KEY_ID",
	AccessKeySecret = "YOUR_ACCESS_KEY_SECRET"
};

ManagementClient managementClient = new ManagementClient(options);

ManagementClient 会自动从 GenAuth 服务器获取 Management API Token,并通过返回的 Token 过期时间自动对 Token 进行缓存。

完整的参数和释义如下:

  • accessKeyId: GenAuth 用户池 ID;
  • accessKeySecret: GenAuth 用户池密钥;
  • timeout: 超时时间,单位为 ms,默认为 10000 ms;
  • host: GenAuth 服务器地址,默认为 https://api.genauth.ai。如果你使用的是 GenAuth 公有云版本,请忽略此参数。如果你使用的是私有化部署的版本,此参数必填,格式如下: https://authing-api.my-authing-service.com(最后不带斜杠 /)。
  • lang: 接口 Message 返回语言格式(可选),可选值为 zh-CN 和 en-US,默认为 zh-CN。

¶ 快速开始

初始化完成 ManagementClient 之后,你可以获取 ManagementClient 的实例,然后调用此实例上的方法。例如:

  • 获取用户列表
UserPaginatedRespDto userPaginatedRespDto =await managementClient.ListUsers();
  • 创建角色
CreateRoleDto dto = new CreateRoleDto()
{
	Code = "管理员",
	Namespace = "default",
	Description = "管理员角色描述"
};

RoleSingleRespDto roleSingle =await managementClient.CreateRole(dto);

完整的接口列表,你可以在 GenAuth Open API (opens new window) 和 SDK 文档 (opens new window) 中获取。

¶ 错误处理

ManagementClient 中的每个方法,遵循统一的返回结构:

  • statusCode: 请求是否成功状态码,当 statusCode 为 200 时,表示操作成功,非 200 全部为失败。
  • apiCode: 细分错误码,当 apiCode 非 200 时,可通过此错误码得到具体的错误类型。
  • message: 具体的错误信息。
  • data: 具体返回的接口数据。

一般情况下,如果你只需要判断操作是否成功,只需要对比一下 code 是否为 200。如果非 200,可以在代码中通抛出异常或者任何你项目中使用的异常处理方式。

UserSingleRespDto userSingleRespDto =await managementClient.GetUser("61c188ccfff26fef0ca6880d");

if (userSingleRespDto.StatusCode !== 200)
{
	throw new Exception(userSingleRespDto.Message); // 抛出异常,由全局异常捕捉中间件进行异常捕捉
}
  // 继续你的业务逻辑 ...

¶ 私有化部署

如果你使用的是私有化部署的 GenAuth IDaaS 服务,需要指定此 GenAuth 私有化实例的 host,如:

using GenAuth.CSharp.SDK.Models;
using GenAuth.CSharp.SDK.Services;

ManagementClientOptions options = new ManagementClientOptions()
{
	AccessKeyId = "YOUR_ACCESS_KEY_ID",
	AccessKeySecret = "YOUR_ACCESS_KEY_SECRET",
   	Host="https://authing-api.my-authing-service.com"
};

ManagementClient managementClient = new ManagementClient(options);

如果你不清楚如何获取,可以联系 GenAuth IDaaS 服务管理员。

  • Nuget
  • 初始化
  • 快速开始
  • 错误处理
  • 私有化部署

User identity management

Integrated third-party login
Customized authentication process

Enterprise internal management

Single sign-on
Multi-factor authentication
Permission management

Developer

Development Documentation
GitHub (opens new window)

Company

official@genauth.ai
16th Floor, Building B, Beichen Century Center, Chaoyang District, Beijing (Total)
Room 406, 4th Floor, Block B, No. 200, Tianfu Fifth Street, High-tech Zone, Chengdu (Branch)

© Beijing Steamory Technology Co., Ltd.