Skip to content
Back to skills

Keycloak Auth

ASecurity

Configure cookie-based OIDC authentication with Keycloak for affolterNET.Web.Bff. Use when setting up login/logout, token refresh, or Keycloak integration.

  • 666 stars
  • 0 votes
  • 0 copies
  • 3 views
  • Added February 9, 2026
developmenttypescriptgo

Works with

  • cli

Security analysis

A100/100

Scanned February 12, 2026

npx -y skills add majiayu000/claude-skill-registry --skill keycloak-auth --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Keycloak Auth?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Keycloak Auth
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/majiayu000-keycloak-auth/badge)](https://www.skillsdirectory.com/skills/majiayu000-keycloak-auth)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: keycloak-auth
description: Configure cookie-based OIDC authentication with Keycloak for affolterNET.Web.Bff. Use when setting up login/logout, token refresh, or Keycloak integration.
---

# Keycloak Authentication

Configure cookie-based OIDC authentication with Keycloak.

For complete reference, see [Library Guide](../../LIBRARY_GUIDE.md).

## Quick Start

### appsettings.json

```json
{
  "affolterNET": {
    "Web": {
      "Auth": {
        "Provider": {
          "Authority": "https://keycloak.example.com/realms/myrealm",
          "ClientId": "my-bff-client",
          "ClientSecret": "your-client-secret"
        },
        "CookieAuth": {
          "CookieName": ".MyApp.Auth",
          "ExpireTimeSpan": "01:00:00"
        }
      },
      "BffOptions": {
        "AuthMode": "Authenticate"
      }
    }
  }
}
```

## Authentication Modes

| Mode | Description |
|------|-------------|
| `None` | No authentication required |
| `Authenticate` | Valid session required, no permission checks |
| `Authorize` | Valid session + Keycloak RPT permissions required |

## Login/Logout Endpoints

The BFF provides these authentication endpoints:

| Endpoint | Method | Description |
|----------|--------|-------------|
| `/bff/account/login` | GET | Initiates OIDC login flow |
| `/bff/account/logout` | GET/POST | Logs out user |
| `/bff/account/user` | GET | Returns current user info |

### Login with Return URL

```
/bff/account/login?returnUrl=/dashboard
```

## Configuration Options

### AuthProviderOptions

| Property | Description |
|----------|-------------|
| `Authority` | Keycloak realm URL |
| `ClientId` | OIDC client identifier |
| `ClientSecret` | OIDC client secret |

### CookieAuthOptions

| Property | Type | Default | Description |
|----------|------|---------|-------------|
| `CookieName` | string | `.AspNetCore.Auth` | Authentication cookie name |
| `ExpireTimeSpan` | TimeSpan | `01:00:00` | Cookie expiration |
| `SlidingExpiration` | bool | `true` | Extend cookie on activity |

## Token Refresh

The `RefreshTokenMiddleware` automatically refreshes tokens:
- Checks token expiration before each request
- Refreshes when < 10 seconds until expiration
- Uses semaphore lock to prevent concurrent refreshes
- Signs out user on refresh failure

## SPA Integration

The BFF returns 401 instead of redirecting to Keycloak:

```typescript
// Handle 401 in your SPA
if (response.status === 401) {
    window.location.href = '/bff/account/login?returnUrl=' +
        encodeURIComponent(window.location.pathname);
}
```

## Troubleshooting

### Login redirects to wrong URL
- Verify `Authority` URL is correct
- Check Keycloak client redirect URIs include your app
- Ensure cookies are being set (check SameSite settings)

### Token refresh fails
- Check refresh token hasn't expired
- Verify Keycloak client has offline_access scope
- Review Keycloak session timeout settings

### Logout doesn't work
- Ensure Keycloak client has logout redirect URI configured
- Check if front-channel logout is enabled

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.