Schick.Keycloak.RestApiClient 24.0.1

dotnet add package Schick.Keycloak.RestApiClient --version 24.0.1
NuGet\Install-Package Schick.Keycloak.RestApiClient -Version 24.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="Schick.Keycloak.RestApiClient" Version="24.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Schick.Keycloak.RestApiClient --version 24.0.1
#r "nuget: Schick.Keycloak.RestApiClient, 24.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.
// Install Schick.Keycloak.RestApiClient as a Cake Addin
#addin nuget:?package=Schick.Keycloak.RestApiClient&version=24.0.1

// Install Schick.Keycloak.RestApiClient as a Cake Tool
#tool nuget:?package=Schick.Keycloak.RestApiClient&version=24.0.1

.NET / C# Keycloak.RestApiClient

This is a .NET / C# REST API client

Documentation for API Endpoints

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));
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on Schick.Keycloak.RestApiClient:

Package Downloads
QuickJob.Users.Client

Package Description

Be.Auto.Authentication.Keycloak.Role

ASP.NET Core Keycloak Integration

Be.Auto.Servicestack.Authentication.Keycloak

Servicestack Keycloak Role Based Authentication

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
24.0.1 294 4/23/2024
24.0.0 127 4/20/2024
23.0.1 77 4/23/2024
23.0.0 86 4/20/2024
22.0.3 29,500 11/5/2023
22.0.2 205 11/4/2023
22.0.1 787 10/24/2023
22.0.0 1,158 10/3/2023
21.0.0 56,662 4/26/2023
20.0.3 5,021 2/22/2023
20.0.2 229 2/18/2023
20.0.1 308 12/30/2022
19.0.2 272 12/24/2022
19.0.1 313 11/7/2022
19.0.0 341 11/4/2022