Galosys.Foundation.AspNetCore.Keycloak
26.7.31.1
dotnet add package Galosys.Foundation.AspNetCore.Keycloak --version 26.7.31.1
NuGet\Install-Package Galosys.Foundation.AspNetCore.Keycloak -Version 26.7.31.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="Galosys.Foundation.AspNetCore.Keycloak" Version="26.7.31.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Galosys.Foundation.AspNetCore.Keycloak" Version="26.7.31.1" />
<PackageReference Include="Galosys.Foundation.AspNetCore.Keycloak" />
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 Galosys.Foundation.AspNetCore.Keycloak --version 26.7.31.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Galosys.Foundation.AspNetCore.Keycloak, 26.7.31.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 Galosys.Foundation.AspNetCore.Keycloak@26.7.31.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=Galosys.Foundation.AspNetCore.Keycloak&version=26.7.31.1
#tool nuget:?package=Galosys.Foundation.AspNetCore.Keycloak&version=26.7.31.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Galosys.Foundation.AspNetCore.Keycloak
Keycloak 认证集成模块,基于 Keycloak.AuthServices 封装,提供与现有 JwtBearer / HttpBasic / ApiKey 共存的认证方案。
快速开始
// appsettings.json
{
"Auth": {
//"DefaultScheme": "Keycloak", // 取消注释将 Keycloak 设为默认
"Keycloak": {
"Realm": "galosys",
"AuthServerUrl": "http://localhost:8080/",
"SslRequired": "none",
"Resource": "galosys-api",
"VerifyTokenAudience": false,
"RolesMappingSource": "All"
}
}
}
// Program.cs
builder.Services.AddAspNetCore(builder.Configuration);
// Keycloak 通过模块自动发现注册,无需额外代码
配置项
| 配置路径 | 类型 | 说明 |
|---|---|---|
Auth:DefaultScheme |
string | 设为 "Keycloak" 切换默认认证方案 |
Auth:Keycloak:Realm |
string | Keycloak realm 名称 |
Auth:Keycloak:AuthServerUrl |
string | 认证服务器地址 |
Auth:Keycloak:Resource |
string | 客户端 ID(ClientId) |
Auth:Keycloak:VerifyTokenAudience |
bool | 是否验证 audience |
Auth:Keycloak:RolesMappingSource |
string | 角色映射范围:None / Realm / Resource / All |
Auth:Keycloak:AdminClient:Realm |
string | Admin API 所在 realm(可选) |
Auth:Keycloak:AdminClient:Resource |
string | Admin API 客户端 ID(可选) |
Auth:Keycloak:AdminClient:Secret |
string | Admin API 客户端密钥(可选) |
认证共存
模块自动注册 Keycloak 认证方案,与 AspNetCore 模块的 JwtBearer / HttpBasic / ApiKey 共存:
// 默认走 JwtBearer(自签 JWT)
[Authorize]
public IActionResult Default() => Ok();
// 指定走 Keycloak
[Authorize(AuthenticationSchemes = "Keycloak")]
public IActionResult KeycloakOnly() => Ok();
// 混合
[Authorize(AuthenticationSchemes = "Keycloak,ApiKey")]
public IActionResult Either() => Ok();
DI 扩展方法
| 方法 | 说明 |
|---|---|
AddKeycloakAuthentication() |
注册 Keycloak JWT Bearer 认证 |
AddKeycloakRoleMapping() |
注册 Realm/Client 角色→.NET Claims 映射 |
AddKeycloakAdminClient() |
注册 Admin API 客户端(可选) |
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. 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.
-
net10.0
- Galosys.Foundation.AspNetCore (>= 26.7.31.1)
- Galosys.Foundation.Core (>= 26.7.31.1)
- Keycloak.AuthServices.Authentication (>= 2.6.1)
- Keycloak.AuthServices.Authorization (>= 2.6.1)
- Keycloak.AuthServices.Sdk (>= 2.6.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.