Quick.Keycloak.Client 1.0.0

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

// Install Quick.Keycloak.Client as a Cake Tool
#tool nuget:?package=Quick.Keycloak.Client&version=1.0.0

Quick.Keycloack.client

contributionswelcome Conventional Commits License

Quick start for client rest api using Keycloack.

Package Version Description
Quick.Keycloack.client 1.0

Install and configure a server keycloak on docker

docker run  -p 8082:8080 -e KEYCLOAK_ADMIN=admin -e KEYCLOAK_ADMIN_PASSWORD=admin  quay.io/keycloak/keycloak start-dev
  • create a realm "Test"

  • create a user test

    alt text

  • set user password

    alt text

  • create a client testapi width url http://localhost:5130 (Quick.Keycloack.Sample launching settings)

alt text

  • config client

alt text alt text Replace url https://localhost:7217 by http://localhost:5130 in this screen view sample

  • Create realm role "RealAdmin"

alt text

  • map user role "RealAdmin"

alt text

<i>nota: for this example api rest is only on http (not https) </i>

Getting Started

  • Add "builder.AddKeycloakTokenJWT();" in your program.cs
// Program.cs
using Quick.Keycloak.Client;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddAuthorization(options =>
{
    options.AddPolicy("admin", policy => policy.RequireClaim(ClaimTypes.Role, "RealAdmin"));
});
builder.Services.AddControllers();

builder.AddKeycloakTokenJWT();

var app = builder.Build();

// Configure the HTTP request pipeline.

app.UseHttpsRedirection();

app.UseAuthentication();

app.UseAuthorization();


app.MapControllers();

app.Run();
  • Modify file appsettings.json and add a new section adaptor configs of your client keycloak

in Keycloack export adapter alt text

and fill appsettings.json like below

 "KeycloackAdatpter": {
    "realm": "test",
    "auth-server-url": "http://localhost:8080/",
    "ssl-required": "external",
    "resource": "testapi",
    "public-client": true,
    "confidential-port": 0  
  }
    
  • secure controller with Authorize attribute
    [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme,Policy = "admin") ]
      [ApiController]
      [Route("[controller]")]
      public class WeatherForecastController : ControllerBase
      {
    

Custom Method AddQuickKeycloakTokenJWT

you can transform your claim as you want using Action<ClaimsIdentity> transformClaim

public static void AddQuickKeycloakTokenJWT(this WebApplicationBuilder builder, Action<ClaimsIdentity> transformClaim =null)
public static void AddQuickKeycloakTokenJWT(this WebApplicationBuilder builder, KeyCloackAdapterOption keyCloackAdapterOption, Action<ClaimsIdentity> transformClaim = null)

parameter "Action<ClaimsIdentity>" can be use to transform principal claimidentity to your custom role

like this for example :

builder.AddQuickKeycloakTokenJWT(claim =>
{
  var userRoleRealm = claim.FindFirst((claim) => claim.Type == "realm_access");
  if (userRoleRealm != null)
  {
      var realmAccess = JsonConvert.DeserializeObject<RealmAccess>(userRoleRealm.Value);

      if (realmAccess != null)
          foreach (var role in realmAccess.roles)
          {
              claim.AddClaim(new Claim(ClaimTypes.Role, role));
          }
  }
});

Sample

See project ApiDemo

  • With Postman get user token alt text

  • swagger get weather endpoint alt text

Build and Development

dotnet build --configuration release ./Quick.Keycloack.client.csproj

dotnet pack -o ./Artefacts ./Quick.Keycloack.client.csproj

Special thanks to the blog of Xavier Hahn

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 132 9/21/2023