askill
net-jwt-auth

net-jwt-authSafety 60Repository

Implement JWT authentication and authorization for ASP.NET Core

133 stars
2.7k downloads
Updated 2/2/2026

Package Files

Loading files...
SKILL.md

What I Do

I implement complete JWT authentication:

  • JWT token generation
  • Token validation
  • Refresh token support
  • Role-based authorization
  • Password hashing
  • User claims

When to Use Me

Use this skill when:

  • Adding authentication to API
  • Implementing JWT tokens
  • Setting up authorization
  • Securing API endpoints

Authentication Structure

src/{ProjectName}.Infrastructure/Security/
├── Jwt/
│   ├── IJwtTokenGenerator.cs
│   ├── JwtTokenGenerator.cs
│   ├── IJwtTokenValidator.cs
│   └── JwtTokenValidator.cs
├── Password/
│   ├── IPasswordHasher.cs
│   └── PasswordHasher.cs
├── Claims/
│   └── ClaimConstants.cs
└── Extensions/
    └── AuthenticationExtensions.cs

src/{ProjectName}.Application/Authentication/
├── Commands/
│   ├── RegisterCommand.cs
│   ├── RegisterCommandHandler.cs
│   ├── LoginCommand.cs
│   └── LoginCommandHandler.cs
├── DTOs/
│   ├── RegisterRequest.cs
│   ├── LoginRequest.cs
│   └── AuthResponse.cs
└── Services/
    └── IAuthService.cs

JWT Implementation

Token Generation

public class JwtTokenGenerator : IJwtTokenGenerator
{
    private readonly IOptions<JwtSettings> _jwtSettings;

    public JwtTokenGenerator(IOptions<JwtSettings> jwtSettings)
    {
        _jwtSettings = jwtSettings;
    }

    public string GenerateToken(User user, IList<string> roles)
    {
        var key = new SymmetricSecurityKey(
            Encoding.UTF8.GetBytes(_jwtSettings.Value.Secret));

        var credentials = new SigningCredentials(
            key, SecurityAlgorithms.HmacSha256);

        var claims = new List<Claim>
        {
            new Claim(JwtRegisteredClaimNames.Sub, user.Id.ToString()),
            new Claim(JwtRegisteredClaimNames.Email, user.Email),
            new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
            new Claim(ClaimTypes.NameIdentifier, user.Id.ToString())
        };

        claims.AddRange(roles.Select(role => 
            new Claim(ClaimTypes.Role, role)));

        var token = new JwtSecurityToken(
            issuer: _jwtSettings.Value.Issuer,
            audience: _jwtSettings.Value.Audience,
            claims: claims,
            expires: DateTime.UtcNow.Add(_jwtSettings.Value.Expiry),
            signingCredentials: credentials
        );

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

JWT Configuration

{
  "JwtSettings": {
    "Secret": "your-256-bit-secret-key-here",
    "Issuer": "https://yourdomain.com",
    "Audience": "https://yourdomain.com",
    "Expiry": "01:00:00",
    "RefreshTokenExpiry": "07:00:00:00"
  }
}

Authorization Policy

builder.Services.AddAuthorization(options =>
{
    options.AddPolicy("AdminOnly", policy =>
        policy.RequireRole("Admin"));

    options.AddPolicy("CanManageProducts", policy =>
        policy.RequireClaim("Permission", "ManageProducts"));
});

Security Best Practices

  1. Use strong secret key (256+ bits)
  2. Set appropriate token expiration
  3. Implement refresh token rotation
  4. Store refresh tokens securely (hashed)
  5. Use HTTPS in production
  6. Validate all claims
  7. Implement rate limiting
  8. Log authentication events

Example Usage

Implement JWT authentication with:
- User registration
- User login
- Token generation
- Refresh token support
- Role-based authorization
- Password hashing (BCrypt)

I will generate complete JWT authentication implementation.

Install

Download ZIP
Requires askill CLI v1.0+

AI Quality Score

80/100Analyzed 2/23/2026

Well-structured skill document for implementing JWT authentication in ASP.NET Core. Includes comprehensive code examples, file structure, security best practices, and clear sections. Has When to Use trigger (R3), structured steps (R5), and metadata tags (R6, R7). Located in dedicated skills folder (R10). Minor penalty for .opencode path suggesting internal config (R8). Safety dimension lower due to limited security warnings. Overall high-quality reusable skill for .NET developers.

60
90
85
80
85

Metadata

Licenseunknown
Version-
Updated2/2/2026
Publishermitkox

Tags

apisecurity