Skip to content
Back to search

keycloak-authservices

Authentication and Authorization with Keycloak and ASP.NET Core 🔐

Safety scan

A static text scan, not a guarantee. Select the badge to see what was scanned and every finding.

Checked today
Badge for your README
findsafeskills safety rating

Markdown

[![findsafeskills safety rating](/api/listings/nikiforovall--keycloak-authorization-services-dotnet/badge.svg)](/listings/nikiforovall--keycloak-authorization-services-dotnet)

HTML

<a href="/listings/nikiforovall--keycloak-authorization-services-dotnet"><img src="/api/listings/nikiforovall--keycloak-authorization-services-dotnet/badge.svg" alt="findsafeskills safety rating" /></a>

Install

Install instructions from the README

As written by the repo's author. findsafeskills has not verified these steps.

Install packages:

dotnet add package Keycloak.AuthServices.Authentication
// Program.cs
using Keycloak.AuthServices.Authentication;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddKeycloakWebApiAuthentication(builder.Configuration);
builder.Services.AddAuthorization();

var app = builder.Build();

app.UseAuthentication();
app.UseAuthorization();

app.MapGet("/", () => "Hello World!").RequireAuthorization();

app.Run();

In this example, configuration is based on `appsettings.json`.

//appsettings.json
{
    "Keycloak": {
        "realm": "Test",
        "auth-server-url": "http://localhost:8080/",
        "ssl-required": "none",
        "resource": "test-client",
        "verify-token-audience": false,
        "credentials": {
            "secret": ""
        },
        "confidential-port": 0
    }
}

What was scanned

The files the safety scan read, as written by the repository's author and shown as plain text. Lines with a finding are marked.

.claude-plugin/marketplace.json · Manifest · 17 lines

View this file on GitHub (read at the repository's current version: the scan did not record which commit it read)

  1. {
  2. "name": "keycloak-authservices",
  3. "owner": {
  4. "name": "Oleksii Nikiforov"
  5. },
  6. "metadata": {
  7. "description": "Keycloak administration and Keycloak.AuthServices .NET library development skills"
  8. },
  9. "plugins": [
  10. {
  11. "name": "keycloak-authservices",
  12. "source": "./",
  13. "description": "Keycloak administration and Keycloak.AuthServices .NET library development skills",
  14. "version": "1.0.0"
  15. }
  16. ]
  17. }
README.md · README · 135 lines

View this file on GitHub (read at the repository's current version: the scan did not record which commit it read)

  1. # Keycloak.AuthServices
  2. [![Discord](https://img.shields.io/discord/1236946465318768670?color=blue&label=Chat%20on%20Discord)](https://discord.gg/S449PhBPRQ)
  3. [![Build](https://github.com/NikiforovAll/keycloak-authorization-services-dotnet/actions/workflows/build.yml/badge.svg?branch=main)](https://github.com/NikiforovAll/keycloak-authorization-services-dotnet/actions/workflows/build.yml)
  4. [![NuGet](https://img.shields.io/nuget/dt/Keycloak.AuthServices.Authentication.svg)](https://nuget.org/packages/Keycloak.AuthServices.Authentication)
  5. [![contributionswelcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/nikiforovall/keycloak-authorization-services-dotnet)
  6. [![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org)
  7. [![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/nikiforovall/keycloak-authorization-services-dotnet/blob/main/LICENSE.md)
  8. 🔐 Easy Authentication and Authorization with Keycloak in .NET.
  9. | Package | Version | Description |
  10. | -------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------ |
  11. | `Keycloak.AuthServices.Authentication` | [![Nuget](https://img.shields.io/nuget/v/Keycloak.AuthServices.Authentication.svg)](https://nuget.org/packages/Keycloak.AuthServices.Authentication) | Keycloak Authentication JWT + OIDC |
  12. | `Keycloak.AuthServices.Authorization` | [![Nuget](https://img.shields.io/nuget/v/Keycloak.AuthServices.Authorization.svg)](https://nuget.org/packages/Keycloak.AuthServices.Authorization) | Authorization Services. Use Keycloak as authorization server |
  13. | `Keycloak.AuthServices.Authorization.TokenIntrospection` | [![Nuget](https://img.shields.io/nuget/v/Keycloak.AuthServices.Authorization.TokenIntrospection.svg)](https://nuget.org/packages/Keycloak.AuthServices.Authorization.TokenIntrospection) | Token introspection for lightweight access tokens (KC 24+) |
  14. | `Keycloak.AuthServices.Authorization.Uma` | [![Nuget](https://img.shields.io/nuget/v/Keycloak.AuthServices.Authorization.Uma.svg)](https://nuget.org/packages/Keycloak.AuthServices.Authorization.Uma) | UMA 2.0 support — permission ticket challenges, RPT exchange |
  15. | `Keycloak.AuthServices.Sdk` | [![Nuget](https://img.shields.io/nuget/v/Keycloak.AuthServices.Sdk.svg)](https://nuget.org/packages/Keycloak.AuthServices.Sdk) | HTTP API integration with Keycloak |
  16. | `Keycloak.AuthServices.Sdk.Kiota` | [![Nuget](https://img.shields.io/nuget/v/Keycloak.AuthServices.Sdk.Kiota.svg)](https://nuget.org/packages/Keycloak.AuthServices.Sdk.Kiota) | HTTP API integration with Keycloak based on OpenAPI |
  17. | `Keycloak.AuthServices.OpenTelemetry` | [![Nuget](https://img.shields.io/nuget/v/Keycloak.AuthServices.OpenTelemetry.svg)](https://nuget.org/packages/Keycloak.AuthServices.OpenTelemetry) | OpenTelemetry support |
  18. | `Keycloak.AuthServices.Templates` | [![Nuget](https://img.shields.io/nuget/v/Keycloak.AuthServices.Templates.svg)](https://nuget.org/packages/Keycloak.AuthServices.Templates) | `dotnet new` templates |
  19. ## Documentation
  20. For Developer Documentation see: <https://nikiforovall.github.io/keycloak-authorization-services-dotnet>
  21. ### API Reference
  22. See: <https://nikiforovall.github.io/keycloak-authorization-services-dotnet-docs>
  23. ## Getting Started
  24. Install packages:
  25. ```bash
  26. dotnet add package Keycloak.AuthServices.Authentication
  27. ```
  28. ```csharp
  29. // Program.cs
  30. using Keycloak.AuthServices.Authentication;
  31. var builder = WebApplication.CreateBuilder(args);
  32. builder.Services.AddKeycloakWebApiAuthentication(builder.Configuration);
  33. builder.Services.AddAuthorization();
  34. var app = builder.Build();
  35. app.UseAuthentication();
  36. app.UseAuthorization();
  37. app.MapGet("/", () => "Hello World!").RequireAuthorization();
  38. app.Run();
  39. ```
  40. In this example, configuration is based on `appsettings.json`.
  41. ```jsonc
  42. //appsettings.json
  43. {
  44. "Keycloak": {
  45. "realm": "Test",
  46. "auth-server-url": "http://localhost:8080/",
  47. "ssl-required": "none",
  48. "resource": "test-client",
  49. "verify-token-audience": false,
  50. "credentials": {
  51. "secret": ""
  52. },
  53. "confidential-port": 0
  54. }
  55. }
  56. ```
  57. ## Example - Add Authorization
  58. With `Keycloak.AuthServices.Authorization`, you can implement role-based authorization in your application. This package allows you to define policies based on roles. Also, you can use Keycloak as Authorization Server. It is a powerful way to organize and apply authorization polices centrally.
  59. ```csharp
  60. var builder = WebApplication.CreateBuilder(args);
  61. var host = builder.Host;
  62. var configuration = builder.Configuration;
  63. var services = builder.Services;
  64. services.AddKeycloakWebApiAuthentication(configuration);
  65. services.AddAuthorization(options =>
  66. {
  67. options.AddPolicy("AdminAndUser", builder =>
  68. {
  69. builder
  70. .RequireRealmRoles("User") // Realm role is fetched from token
  71. .RequireResourceRoles("Admin"); // Resource/Client role is fetched from token
  72. });
  73. })
  74. .AddKeycloakAuthorization(configuration);
  75. var app = builder.Build();
  76. app.UseAuthentication();
  77. app.UseAuthorization();
  78. app.MapGet("/hello", () => "[]")
  79. .RequireAuthorization("AdminAndUser");
  80. app.Run();
  81. ```
  82. ### Example - Invoke Admin API
  83. ```csharp
  84. var services = new ServiceCollection();
  85. services.AddKeycloakAdminHttpClient(new KeycloakAdminClientOptions
  86. {
  87. AuthServerUrl = "http://localhost:8080/",
  88. Realm = "master",
  89. Resource = "admin-api",
  90. });
  91. var sp = services.BuildServiceProvider();
  92. var client = sp.GetRequiredService<IKeycloakRealmClient>();
  93. var realm = await client.GetRealmAsync("Test");
  94. ```
  95. ## Build and Development
  96. `dotnet cake --target build`
  97. `dotnet cake --target test`
  98. `dotnet pack -o ./Artefacts`

Ratings and comments

Sign in to rate this skill.

Sign in

Free to use. If it’s helped, pass it on.