Documentation
For Developer Documentation see: https://nikiforovall.github.io/keycloak-authorization-services-dotnet
API Reference
See: https://nikiforovall.github.io/keycloak-authorization-services-dotnet-docs
Keycloak.AuthServices.Sdk.Kiota v26.0.5
https://www.nuget.org/packages/Keycloak.AuthServices.Sdk.Kiota#readme-body-tab
.net cli
安装命令
dotnet add package Keycloak.AuthServices.Sdk.Kiota --version 26.0.5
ClientRepresentation 对象
namespace Keycloak.AuthServices.Sdk.Kiota.Admin.Models;
public class ClientRepresentation : IAdditionalDataHolder, IParsable
里面有一个内置方法,获取当前模型的反序列化信息,如下所示:
/// <summary>
/// The deserialization information for the current model
/// </summary>
/// <returns>A IDictionary<string, Action<IParseNode>></returns>
public virtual IDictionary<string, Action<IParseNode>> GetFieldDeserializers()
{
return new Dictionary<string, Action<IParseNode>>
{
{ "access", n => { Access = n.GetObjectValue<Keycloak.AuthServices.Sdk.Kiota.Admin.Models.ClientRepresentation_access>(Keycloak.AuthServices.Sdk.Kiota.Admin.Models.ClientRepresentation_access.CreateFromDiscriminatorValue); } },
{ "adminUrl", n => { AdminUrl = n.GetStringValue(); } },
{ "alwaysDisplayInConsole", n => { AlwaysDisplayInConsole = n.GetBoolValue(); } },
{ "attributes", n => { Attributes = n.GetObjectValue<Keycloak.AuthServices.Sdk.Kiota.Admin.Models.ClientRepresentation_attributes>(Keycloak.AuthServices.Sdk.Kiota.Admin.Models.ClientRepresentation_attributes.CreateFromDiscriminatorValue); } },
{ "authenticationFlowBindingOverrides", n => { AuthenticationFlowBindingOverrides = n.GetObjectValue<Keycloak.AuthServices.Sdk.Kiota.Admin.Models.ClientRepresentation_authenticationFlowBindingOverrides>(Keycloak.AuthServices.Sdk.Kiota.Admin.Models.ClientRepresentation_authenticationFlowBindingOverrides.CreateFromDiscriminatorValue); } },
{ "authorizationServicesEnabled", n => { AuthorizationServicesEnabled = n.GetBoolValue(); } },
{ "authorizationSettings", n => { AuthorizationSettings = n.GetObjectValue<Keycloak.AuthServices.Sdk.Kiota.Admin.Models.ResourceServerRepresentation>(Keycloak.AuthServices.Sdk.Kiota.Admin.Models.ResourceServerRepresentation.CreateFromDiscriminatorValue); } },
{ "baseUrl", n => { BaseUrl = n.GetStringValue(); } },
{ "bearerOnly", n => { BearerOnly = n.GetBoolValue(); } },
{ "clientAuthenticatorType", n => { ClientAuthenticatorType = n.GetStringValue(); } },
{ "clientId", n => { ClientId = n.GetStringValue(); } },
{ "clientTemplate", n => { ClientTemplate = n.GetStringValue(); } },
{ "consentRequired", n => { ConsentRequired = n.GetBoolValue(); } },
{ "defaultClientScopes", n => { DefaultClientScopes = n.GetCollectionOfPrimitiveValues<string>()?.ToList(); } },
{ "defaultRoles", n => { DefaultRoles = n.GetCollectionOfPrimitiveValues<string>()?.ToList(); } },
{ "description", n => { Description = n.GetStringValue(); } },
{ "directAccessGrantsEnabled", n => { DirectAccessGrantsEnabled = n.GetBoolValue(); } },
{ "directGrantsOnly", n => { DirectGrantsOnly = n.GetBoolValue(); } },
{ "enabled", n => { Enabled = n.GetBoolValue(); } },
{ "frontchannelLogout", n => { FrontchannelLogout = n.GetBoolValue(); } },
{ "fullScopeAllowed", n => { FullScopeAllowed = n.GetBoolValue(); } },
{ "id", n => { Id = n.GetStringValue(); } },
{ "implicitFlowEnabled", n => { ImplicitFlowEnabled = n.GetBoolValue(); } },
{ "name", n => { Name = n.GetStringValue(); } },
{ "nodeReRegistrationTimeout", n => { NodeReRegistrationTimeout = n.GetIntValue(); } },
{ "notBefore", n => { NotBefore = n.GetIntValue(); } },
{ "optionalClientScopes", n => { OptionalClientScopes = n.GetCollectionOfPrimitiveValues<string>()?.ToList(); } },
{ "origin", n => { Origin = n.GetStringValue(); } },
{ "protocol", n => { Protocol = n.GetStringValue(); } },
{ "protocolMappers", n => { ProtocolMappers = n.GetCollectionOfObjectValues<Keycloak.AuthServices.Sdk.Kiota.Admin.Models.ProtocolMapperRepresentation>(Keycloak.AuthServices.Sdk.Kiota.Admin.Models.ProtocolMapperRepresentation.CreateFromDiscriminatorValue)?.ToList(); } },
{ "publicClient", n => { PublicClient = n.GetBoolValue(); } },
{ "redirectUris", n => { RedirectUris = n.GetCollectionOfPrimitiveValues<string>()?.ToList(); } },
{ "registeredNodes", n => { RegisteredNodes = n.GetObjectValue<Keycloak.AuthServices.Sdk.Kiota.Admin.Models.ClientRepresentation_registeredNodes>(Keycloak.AuthServices.Sdk.Kiota.Admin.Models.ClientRepresentation_registeredNodes.CreateFromDiscriminatorValue); } },
{ "registrationAccessToken", n => { RegistrationAccessToken = n.GetStringValue(); } },
{ "rootUrl", n => { RootUrl = n.GetStringValue(); } },
{ "secret", n => { Secret = n.GetStringValue(); } },
{ "serviceAccountsEnabled", n => { ServiceAccountsEnabled = n.GetBoolValue(); } },
{ "standardFlowEnabled", n => { StandardFlowEnabled = n.GetBoolValue(); } },
{ "surrogateAuthRequired", n => { SurrogateAuthRequired = n.GetBoolValue(); } },
{ "type", n => { Type = n.GetStringValue(); } },
{ "useTemplateConfig", n => { UseTemplateConfig = n.GetBoolValue(); } },
{ "useTemplateMappers", n => { UseTemplateMappers = n.GetBoolValue(); } },
{ "useTemplateScope", n => { UseTemplateScope = n.GetBoolValue(); } },
{ "webOrigins", n => { WebOrigins = n.GetCollectionOfPrimitiveValues<string>()?.ToList(); } },
};
}
这段代码是使用 Kiota 自动生成的反序列化逻辑,用于将 Keycloak
的 REST API
响应中的 JSON
数据映射到 C#
模型类中。它定义了一个 Dictionary<string, Action<IParseNode>>
,其中每个键(如 "access"
、"adminUrl"
)对应一个字段名,值是一个委托方法,描述了如何从解析节点(IParseNode
)中读取该字段的值并赋给模型类的属性。
🧱 类似结构说明
return new Dictionary<string, Action<IParseNode>>
{
{ "jsonFieldName", n => { ClassNameProperty = n.GetValue(); } }
};
"jsonFieldName"
:Keycloak 返回的 JSON 字段名称。n => { ... }
:处理该字段的解析逻辑。ClassNameProperty
:C# 模型类中对应的属性名。n.GetValue()
:从IParseNode
中提取值的方法。
🔍 各参数作用详解
JSON 字段 | 对应属性 | 作用 |
---|---|---|
"access" | Access | 客户端访问权限控制对象,例如是否允许配置、查看等。 |
"adminUrl" | AdminUrl | 管理界面 URL,用于管理客户端的前端地址。 |
"alwaysDisplayInConsole" | AlwaysDisplayInConsole | 是否始终在 Keycloak 控制台显示此客户端。 |
"attributes" | Attributes | 自定义扩展属性,用于存储额外信息。 |
"authenticationFlowBindingOverrides" | AuthenticationFlowBindingOverrides | 覆盖认证流程绑定设置,如自定义登录流程。 |
"authorizationServicesEnabled" | AuthorizationServicesEnabled | 是否启用授权服务(基于 UMA)。 |
"authorizationSettings" | AuthorizationSettings | 授权服务器相关配置,如资源保护策略。 |
"baseUrl" | BaseUrl | 客户端的基础 URL,用于重定向或链接生成。 |
"bearerOnly" | BearerOnly | 是否为 Bearer-only 客户端(仅接受 Token 认证)。 |
"clientAuthenticatorType" | ClientAuthenticatorType | 客户端身份验证方式,如 client-secret。 |
"clientId" | ClientId | 客户端唯一标识符,用于 OAuth2 流程中的客户端 ID。 |
"clientTemplate" | ClientTemplate | 使用的客户端模板名称(用于继承配置)。 |
"consentRequired" | ConsentRequired | 是否需要用户授权确认(OAuth2 中的同意页面)。 |
"defaultClientScopes" | DefaultClientScopes | 默认分配给客户端的 scope 列表。 |
"defaultRoles" | DefaultRoles | 客户端默认角色列表。 |
"description" | Description | 客户端描述信息。 |
"directAccessGrantsEnabled" | DirectAccessGrantsEnabled | 是否启用直接访问授权(Resource Owner Password Credentials)。 |
"directGrantsOnly" | DirectGrantsOnly | 是否只允许直接授权模式。 |
"enabled" | Enabled | 客户端是否启用。 |
"frontchannelLogout" | FrontchannelLogout | 是否启用前端通道注销(Frontchannel Logout)。 |
"fullScopeAllowed" | FullScopeAllowed | 是否允许访问所有 scope。 |
"id" | Id | 客户端内部唯一 ID(通常为 UUID)。 |
"implicitFlowEnabled" | ImplicitFlowEnabled | 是否启用隐式授权流程(Implicit Flow)。 |
"name" | Name | 客户端显示名称。 |
"nodeReRegistrationTimeout" | NodeReRegistrationTimeout | 集群节点重新注册超时时间(秒)。 |
"notBefore" | NotBefore | 客户端生效前的时间戳(Unix 时间戳)。 |
"optionalClientScopes" | OptionalClientScopes | 可选的客户端 scope 列表。 |
"origin" | Origin | 客户端来源(如 SSO 提供商)。 |
"protocol" | Protocol | 使用的身份协议类型,如 openid-connect、saml。 |
"protocolMappers" | ProtocolMappers | 协议映射器列表,用于将用户属性映射到 Token。 |
"publicClient" | PublicClient | 是否为公共客户端(不需客户端密钥)。 |
"redirectUris" | RedirectUris | OAuth2 登录完成后允许跳转的 URI 列表。 |
"registeredNodes" | RegisteredNodes | 已注册的集群节点信息。 |
"registrationAccessToken" | RegistrationAccessToken | 注册客户端时使用的访问令牌。 |
"rootUrl" | RootUrl | 根路径 URL,用于构建完整 URL。 |
"secret" | Secret | 客户端密钥(用于身份验证)。 |
"serviceAccountsEnabled" | ServiceAccountsEnabled | 是否启用服务账户(自动创建服务用户)。 |
"standardFlowEnabled" | StandardFlowEnabled | 是否启用标准授权码流程(Authorization Code Flow)。 |
"surrogateAuthRequired" | SurrogateAuthRequired | 是否需要代理认证(用于外部系统)。 |
"type" | Type | 客户端类型(如 confidential、bearer-only)。 |
"useTemplateConfig" | UseTemplateConfig | 是否使用模板配置进行初始化。 |
"useTemplateMappers" | UseTemplateMappers | 是否使用模板映射器。 |
"useTemplateScope" | UseTemplateScope | 是否使用模板 scope。 |
"webOrigins" | WebOrigins | 允许跨域请求的源列表(CORS 设置)。 |
📌 总结
这些字段共同构成了一个完整的 Keycloak ClientRepresentation 模型,表示一个客户端应用在 Keycloak
中的所有配置信息。通过这个模型,你可以:
- 获取/更新客户端配置;
- 动态管理客户端行为;
- 实现与
Keycloak
的集成(如自动化配置、SSO
等);
适用于开发基于 Keycloak
的统一认证中心、多租户系统、微服务安全网关等场景。