Skip to content
Skills Library
Programming · Building & coding

jwt-auth-setup

/jwt-auth-setup

Set up JWT authentication and authorization in ASP.NET Core. Use this skill whenever the user wants to add login/auth, issue or validate JWT tokens, set up bearer authentication, refresh tokens, role/policy-based authorization, claims, or asks "how do I secure my API / add JWT auth / protect my endpoints". Always use this skill for ASP.NET Core auth requests; it produces wired-up token issuing, validation, and authorization policies with secure defaults rather than a snippet.

Licence
MIT
Last repository change
09.08.2026

Assessment and security

100/ 100Class: good
Format25 / 25
Frontmatter with name and description, valid name, description up to 1,024 characters, name matching the folder.
Discoverability25 / 25
Description explains when to use the skill, has a reasonable length and uses third person.
Content35 / 35
At least 150 words and at most 500 lines, headings, step lists, examples or code.
Hygiene15 / 15
No placeholders such as TODO, no Windows paths, with a licence.
Riskno signals detected in the scan
Files in this package
2
Scripts
0
HTML comments
0
URLs in SKILL.md
0
Deprecation

No signals detected. Repository changed on 09.08.2026, without references to retired models or APIs.

Licence

MIT, open: use and modify while retaining author attribution.

What it does and when to use it

Set up JWT authentication and authorization in ASP.NET Core. Use this skill whenever the user wants to add login/auth, issue or validate JWT tokens, set up bearer authentication, refresh tokens, role/policy-based authorization, claims, or asks "how do I secure my API / add JWT auth / protect my endpoints". Always use this skill for ASP.NET Core auth requests; it produces wired-up token issuing, validation, and authorization policies with secure defaults rather than a snippet.

Triggers from the description

how do I secure my API / add JWT auth / protect my endpoints
Original author description

Set up JWT authentication and authorization in ASP.NET Core. Use this skill whenever the user wants to add login/auth, issue or validate JWT tokens, set up bearer authentication, refresh tokens, role/policy-based authorization, claims, or asks "how do I secure my API / add JWT auth / protect my endpoints". Always use this skill for ASP.NET Core auth requests; it produces wired-up token issuing, validation, and authorization policies with secure defaults rather than a snippet.

How it works

  1. Input — point it at your project
  2. When this runs
  3. Packages
  4. Validation wiring (Program.cs)
  5. Token issuing service
  6. Refresh tokens (do this properly)
  7. Protecting endpoints
  8. Secure defaults (apply unless told otherwise)
  9. Output format
Original SKILL.md headings · 139 lines, 621 words
SKILL.md preview
---
name: jwt-auth-setup
description: Set up JWT authentication and authorization in ASP.NET Core. Use this skill whenever the user wants to add login/auth, issue or validate JWT tokens, set up bearer authentication, refresh tokens, role/policy-based authorization, claims, or asks "how do I secure my API / add JWT auth / protect my endpoints". Always use this skill for ASP.NET Core auth requests; it produces wired-up token issuing, validation, and authorization policies with secure defaults rather than a snippet.
---

# JWT Auth Setup (ASP.NET Core)

Wire up JWT bearer authentication and authorization in an ASP.NET Core API with secure defaults: token issuing, validation, refresh tokens, and policy-based authorization.

## Input — point it at your project

If they point at a project or repo, inspect the existing auth setup before changing anything:

- **A project or repo root** — find the auth wiring and review it.
- **A GitHub URL** — clone first, then inspect: `git clone --depth 1 <url> /tmp/repo && cd /tmp/repo`

Discover current state yourself, e.g.:
```
grep -rn "AddAuthentication\|AddJwtBearer\|TokenValidationParameters\|AddAuthorization\|RequireAuthorization\|RequireHttpsMetadata" --include=*.cs <target> | grep -vi "/obj/\|/bin/"
```
If auth already exists, **review and harden it** (validation flags, HTTPS metadata, ClockSkew, refresh-token handling, where the signing key lives) rather than re-adding from scratch, and flag any access-control gaps (endpoints without `RequireAuthorization` / no fallback policy).

## When this runs

The user wants to secure an API with JWTs, or wants their existing auth reviewed. If new: confirm whether they're **issuing** tokens themselves (own login) or **validating** tokens from an external IdP (Azure AD / Auth0 / Keycloak). Defaults below cover self-issued; for external IdP, the validation half is the same and issuing is delegated.

## Packages

```
dotnet add package Microsoft.AspNetCore.Authentication.JwtBearer
dotnet add package System.IdentityModel.Tokens.Jwt
```

## Validation wiring (Program.cs)

```csharp
var jwt = builder.Configuration.GetSection("Jwt");

builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddJwtBearer(options =>
    {
        options.TokenValidationParameters = new TokenValidationParameters
        {
            ValidateIssuer = true,
            ValidateAudience = true,
            ValidateLifetime = true,
            ValidateIssuerSigningKey = true,
            ValidIssuer = jwt["Issuer"],
            ValidAudience = jwt["Audience"],
            IssuerSigningKey = new SymmetricSecurityKey(
                Encoding.UTF8.GetBytes(jwt["Key"]!)),
            ClockSkew = TimeSpan.FromSeconds(30) // tighten the default 5 min
        };
    });

builder.Services.AddAuthorization(options =>
{
    options.AddPolicy("AdminOnly", p => p.RequireRole("Admin"));
    options.AddPolicy("CanManageOrders", p => p.RequireClaim("permission", "orders:manage"));
});

// ... after build:
app.UseAuthentication();
app.UseAuthorization();
```

## Token issuing service

```csharp
public class TokenService(IConfiguration config)
{
    public string CreateAccessToken(User user)
    {
        var jwt = config.GetSection("Jwt");
        var claims = new List<Claim>
        {
            new(JwtRegisteredClaimNames.Sub, user.Id.ToString()),
            new(JwtRegisteredClaimNames.Email, user.Email),
            new(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
            new(ClaimTypes.Role, user.Role)
        };

        var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwt["Key"]!));
        var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

        var token = new JwtSecurityToken(
            issuer: jwt["Issuer"],
            audience: jwt["Audience"],
            claims: claims,
            expires: DateTime.UtcNow.AddMinutes(15), // short-lived access token
            signingCredentials: creds);

        return new JwtSecurityTokenHandler().WriteToken(token);
    }
}
```

## Refresh tokens (do this properly)

- Access token: **short-lived** (~15 min). Refresh token: **long-lived, persisted, revocable**.
- Store refresh tokens **hashed** in the DB with an expiry and a `Revoked` flag — never just a JWT you can't revoke.
- On refresh: validate the stored token, rotate it (issue a new refresh token, invalidate the old), then issue a new access token.
- On logout / suspected theft: revoke the refresh token row.

## Protecting endpoints

```csharp
app.MapGet("/orders", () => ...).RequireAuthorization();
app.MapPost("/orders", () => ...).RequireAuthorization("CanManageOrders");
app.MapDelete("/admin/{id}", () => ...).RequireAuthorization("AdminOnly");

// Reading the current user:
app.MapGet("/me", (ClaimsPrincipal user) =>
    Results.Ok(new { Id = user.FindFirstValue(JwtRegisteredClaimNames.Sub) }))
   .RequireAuthorization();
```

## Secure defaults (apply unless told otherwise)

- **Never put the signing key in source or appsettings committed to Git.** Use user-secrets in dev, a secret store / env var / Key Vault in prod. Key must be ≥ 32 bytes for HS256.
- **HTTPS only**; `RequireHttpsMetadata = true` in production.
- **Short access-token lifetime + revocable refresh tokens.** A JWT can't be un-issued — keep them short.
- **Validate everything** (issuer, audience, lifetime, signature). Don't disable validation to "make it work".
- **Tighten `ClockSkew`** — the 5-minute default extends token lifetime by 5 minutes.
- Prefer **policy-based authorization** over scattering `[Authorize(Roles=...)]` strings.
- Consider **asymmetric keys (RS256)** when multiple services validate tokens issued by one — they only need the public key.

## Output format

```
## What I'm setting up
<self-issued vs external IdP, what's protected>

## Packages + Program.cs wiring
...

## Token issuing / refresh
...

## Config & secrets
<appsettings shape + where the key actually goes>
```
Files in this package (2)
  • SKILL.md 6 KB
  • USAGE.md 2 KB

Versions

The database has no other skills named jwt-auth-setup.

Database snapshot: 13.11.2026 · package version 8cf90c381acb (SHA-256 digest of the entire package)

Downloads in the last 30 days: 0 · ranking

Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.

Similar skills

  • 100authentication
    Authentication and authorization for ASP.NET Core. Covers JWT bearer tokens, OpenID Connect, ASP.NET Identity, authorization policies, role and claim-based authorization, and API key authentication. Load this skill when implementing login, protecting endpoints, designing…
  • 100jwt-authentication
    Use when implementing stateless JWT authentication in Symfony with LexikJWTAuthenticationBundle — login endpoint, token issuance, refresh tokens, custom claims, and the JWT firewall. Use when the task mentions JWT, bearer token, access token, refresh token, or stateless API auth.
  • 95auth0-aspnetcore-api
    Use when securing ASP.NET Core Web API endpoints with JWT Bearer token validation, scope/permission checks, or stateless auth - integrates Auth0.AspNetCore.Authentication.Api for REST APIs receiving access tokens from frontends or mobile apps. Also handles DPoP…
  • 95aspnet-identity
    ASP.NET Core Identity for authentication, roles, claims, and external providers. Covers Identity setup, customization, and token-based auth. USE WHEN: user mentions "ASP.NET Identity", "user authentication", ".NET auth", "roles and claims", "Identity scaffolding", "external…
  • 90dotnet-aspnetcore-identity-authz-review
    Use this skill when reviewing how an ASP.NET Core application authenticates and authorizes requests — authentication schemes, JWT TokenValidationParameters, cookie and session security, policy-based authorization, authorization handlers, claims trust, role-versus-resource…
  • 100auth-system-setup
    When the user wants to set up authentication and authorization for a web application. Use when the user mentions "auth," "login," "OAuth," "SSO," "single sign-on," "role-based access," "RBAC," "permissions," "user roles," "access control," "authentication," or "authorization."…

Feedback and reports

Your message goes to Bartosz Sokoliński. An email address is needed only for a reply. See the privacy policy.