Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

https://github.com/fschick/Keycloak.RestApiClient

Keycloak Admin REST API client (.NET)
https://github.com/fschick/Keycloak.RestApiClient

csharp dotnet keycloak keycloak-api rest rest-client

Last synced: about 1 month ago
JSON representation

Keycloak Admin REST API client (.NET)

Lists

README

        

# .NET / C# Keycloak.RestApiClient
This is a .NET / C# REST API client

* for the [Keycloak Admin REST API](https://www.keycloak.org/docs-api/latest/rest-api/index.html)
* auto-generated by [OpenAPI Generator](https://openapi-generator.tech)
* using the [OpenAPI definitions](https://www.keycloak.org/docs-api/latest/rest-api/openapi.json) provided by Keycloak

## Documentation for API Endpoints

* Official documentation: [Keycloak Admin REST API](https://www.keycloak.org/docs-api/latest/rest-api/index.html)
* Generated Swagger UI: [Swagger Editor](https://editor.swagger.io/?url=https://www.keycloak.org/docs-api/latest/rest-api/openapi.json)

## 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
```powershell
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

```csharp
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://",
Realm = "",
UserName = "",
Password = ""
};

using var httpClient = AuthenticationHttpClientFactory.Create(credentials);
using var usersApi = ApiClientFactory.Create(httpClient);

var users = await usersApi.GetUsersAsync("");
Console.WriteLine($"Users: {users.Count}");
```

With authentication by client-id/client-secret

```csharp
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://",
Realm = "",
ClientId = "",
ClientSecret = ""
};

using var httpClient = AuthenticationHttpClientFactory.Create(credentials);
using var usersApi = ApiClientFactory.Create(httpClient);

var users = await usersApi.GetUsersAsync("");
Console.WriteLine($"Users: {users.Count}");
```

## Advanced Usage
To use the API client with a HTTP proxy, setup a `System.Net.WebProxy`
```csharp
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](https://docs.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests#issues-with-the-original-httpclient-class-available-in-net) for details). To use your own HttpClient instance just pass it to the ApiClass constructor.

```csharp
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.

```csharp
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:

```csharp
services.AddHttpClient(httpClient => new PetApi(httpClient));
```