Schick.Keycloak.RestApiClient 24.0.1
.NET / C# Keycloak.RestApiClient
This is a .NET / C# REST API client
- for the Keycloak Admin REST API
- auto-generated by OpenAPI Generator
- using the OpenAPI definitions provided by Keycloak
Documentation for API Endpoints
- Official documentation: Keycloak Admin REST API
- Generated Swagger UI: Swagger Editor
Keycloak versions supported
Keycloak.RestApiClient | Keycloak |
---|---|
24.n.n | 24.x.x |
23.n.n | 23.x.x |
22.n.n | 22.x.x |
21.n.n | 21.x.x |
20.n.n | 20.x.x |
19.n.n | 19.x.x |
Frameworks supported
- .NET Core >=1.0
- .NET Framework >=4.6
- Mono/Xamarin >=vNext
Installation
Install from NuGet package
Install-Package Schick.Keycloak.RestApiClient
Getting Started
Method names
Method names are humanized:
GET
on path /{realm}/users
becomes GetUsers(Async)
GET
on path /{realm}/identity-provider/providers/{provider_id}
becomes GetIdentityProviderProvidersByProviderId(Async)
Authentication
You can select authentication flow either by the username and password or by providing client ID and client secret.
Sample code
With authentication by username/password
using FS.Keycloak.RestApiClient.Api;
using FS.Keycloak.RestApiClient.Authentication.ClientFactory;
using FS.Keycloak.RestApiClient.Authentication.Flow;
using FS.Keycloak.RestApiClient.ClientFactory;
var credentials = new PasswordGrantFlow
{
KeycloakUrl = "https://<keycloak-url>",
Realm = "<realm>",
UserName = "<username>",
Password = "<password>"
};
using var httpClient = AuthenticationHttpClientFactory.Create(credentials);
using var usersApi = ApiClientFactory.Create<UsersApi>(httpClient);
var users = await usersApi.GetUsersAsync("<realm>");
Console.WriteLine($"Users: {users.Count}");
With authentication by client-id/client-secret
using FS.Keycloak.RestApiClient.Api;
using FS.Keycloak.RestApiClient.Authentication.ClientFactory;
using FS.Keycloak.RestApiClient.Authentication.Flow;
using FS.Keycloak.RestApiClient.ClientFactory;
var credentials = new ClientCredentialsFlow
{
KeycloakUrl = "https://<keycloak-url>",
Realm = "<realm>",
ClientId = "<client-id>",
ClientSecret = "<client-secret>"
};
using var httpClient = AuthenticationHttpClientFactory.Create(credentials);
using var usersApi = ApiClientFactory.Create<UsersApi>(httpClient);
var users = await usersApi.GetUsersAsync("<realm>");
Console.WriteLine($"Users: {users.Count}");
Advanced Usage
To use the API client with a HTTP proxy, setup a System.Net.WebProxy
Configuration c = new Configuration();
System.Net.WebProxy webProxy = new System.Net.WebProxy("http://myProxyUrl:80/");
webProxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
c.Proxy = webProxy;
Connections
Each ApiClass (properly the ApiClient inside it) will create an instance of HttpClient. It will use that for the entire lifecycle and dispose it when called the Dispose method.
To better manager the connections it's a common practice to reuse the HttpClient and HttpClientHandler (see here for details). To use your own HttpClient instance just pass it to the ApiClass constructor.
HttpClientHandler yourHandler = new HttpClientHandler();
HttpClient yourHttpClient = new HttpClient(yourHandler);
var api = new YourApiClass(yourHttpClient, yourHandler);
If you want to use an HttpClient and don't have access to the handler, for example in a DI context in Asp.net Core when using IHttpClientFactory.
HttpClient yourHttpClient = new HttpClient();
var api = new YourApiClass(yourHttpClient);
You'll loose some configuration settings, the features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings. You need to either manually handle those in your setup of the HttpClient or they won't be available.
Here an example of DI setup in a sample web project:
services.AddHttpClient<YourApiClass>(httpClient => new PetApi(httpClient));
Showing the top 20 packages that depend on Schick.Keycloak.RestApiClient.
Packages | Downloads |
---|---|
OElite.Core
Package Description
|
11 |
OElite.Core
Package Description
|
6 |
OElite.Core
Package Description
|
5 |
OElite.Core
Package Description
|
4 |
OElite.Core
Package Description
|
3 |
OElite.Core
Package Description
|
2 |
.NET Standard 2.0
- JsonSubTypes (>= 2.0.1)
- Newtonsoft.Json (>= 13.0.3)
- Polly (>= 8.1.0)
- System.ComponentModel.Annotations (>= 5.0.0)
Version | Downloads | Last updated |
---|---|---|
26.0.7 | 1 | 01/10/2025 |
26.0.6 | 0 | 12/02/2024 |
26.0.2 | 6 | 10/30/2024 |
26.0.0 | 0 | 10/18/2024 |
25.0.0 | 44 | 08/10/2024 |
24.0.2 | 2 | 09/08/2024 |
24.0.2-beta1 | 1 | 08/12/2024 |
24.0.1 | 7 | 08/26/2024 |
24.0.0 | 2 | 08/26/2024 |
23.0.1 | 2 | 09/08/2024 |
23.0.0 | 3 | 09/27/2024 |
22.0.3 | 2 | 08/12/2024 |
22.0.2 | 2 | 08/12/2024 |
22.0.1 | 0 | 10/24/2023 |
22.0.0 | 2 | 09/07/2024 |
21.0.0 | 2 | 08/26/2024 |
20.0.3 | 1 | 08/12/2024 |
20.0.2 | 2 | 09/08/2024 |
20.0.1 | 4 | 08/27/2024 |
19.0.2 | 3 | 10/31/2024 |
19.0.1 | 4 | 08/26/2024 |
19.0.0 | 3 | 09/07/2024 |