Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Products
  • Resources
    • Cheatsheets
    • Tech Comparisons
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# ASP.NET MVC ASP.NET Web Forms C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet General Web Development HTML, CSS HTML/CSS Java JavaScript JavaScript, HTML, CSS JavaScript, Node.js Node.js
      Python Python 3.11, Pandas, SQL Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. ASP.NET Core
  4. CWE-347: Secure JWT Token Validation in ASP.NET Core Web API

CWE-347: Secure JWT Token Validation in ASP.NET Core Web API

Date- Jun 02,2026 239
cwe 347 jwt

Overview

The Common Weakness Enumeration (CWE) identifier 347 focuses on the security implications of improperly validating JSON Web Tokens (JWTs). JWTs are widely used for authentication and authorization in modern web applications due to their compact size and ease of transmission. However, without rigorous validation, attackers can exploit vulnerabilities, leading to unauthorized access and data breaches.

JWTs consist of three parts: header, payload, and signature. The header typically contains the type of token and the signing algorithm, the payload contains claims (information about the user and other data), and the signature validates that the sender of the JWT is who it says it is and ensures that the message wasn't changed. In production systems, failure to validate these tokens properly can result in significant security risks, making it essential for developers to implement robust validation mechanisms.

Real-world use cases for JWT include web applications, mobile applications, and microservices architectures, where secure, stateless authentication is crucial. By adhering to best practices in validating JWTs, developers can significantly reduce the risk of unauthorized access and enhance the overall security posture of their applications.

Prerequisites

  • ASP.NET Core SDK: Ensure you have the latest version installed for development.
  • Knowledge of JWT: Familiarity with JWT structure and its use cases is essential.
  • Entity Framework Core: Basic understanding to manage user data and authentication.
  • NuGet Packages: Install necessary packages like Microsoft.AspNetCore.Authentication.JwtBearer.
  • Basic C# Skills: Proficiency in C# will help in understanding the code examples.

Understanding JWT Structure

Before diving into validation, it's essential to understand the structure of a JWT. Each JWT consists of three base64-encoded sections separated by dots:

  • Header: Contains metadata about the token, including the signing algorithm.
  • Payload: Contains the claims, which are statements about an entity (typically, the user) and additional data.
  • Signature: To create the signature part, you need to take the encoded header, encoded payload, a secret, and the algorithm specified in the header.

For example, a JWT might look like this: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c. The first part is the header, the second is the payload, and the third is the signature.

JWT Claims

Claims are key-value pairs that provide information about the user and the token itself. They can be categorized into three types:

  • Registered Claims: Predefined claims that are recommended to be used, such as iss (issuer), exp (expiration time), and sub (subject).
  • Public Claims: Custom claims that can be defined by the user but should be collision-resistant.
  • Private Claims: Custom claims created to share information between parties that agree on using them.

Setting Up JWT Authentication in ASP.NET Core

To validate JWT tokens securely in an ASP.NET Core Web API, you must first set up JWT authentication in your application. This involves configuring the authentication middleware to use JWT Bearer authentication.

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddAuthentication(options =>
        {
            options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        })
        .AddJwtBearer(options =>
        {
            options.TokenValidationParameters = new TokenValidationParameters
            {
                ValidateIssuer = true,
                ValidateAudience = true,
                ValidateLifetime = true,
                ValidateIssuerSigningKey = true,
                ValidIssuer = "yourdomain.com",
                ValidAudience = "yourdomain.com",
                IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("YourSuperSecretKey"))
            };
        });
        services.AddControllers();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseRouting();

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

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}

This code configures JWT authentication in the Startup class. Here's a breakdown:

  • services.AddAuthentication: Configures the authentication scheme to use JWT Bearer.
  • TokenValidationParameters: Specifies the criteria for validating tokens, including issuer, audience, lifetime, and signing key.
  • app.UseAuthentication: Enables authentication middleware in the request pipeline.
  • app.UseAuthorization: Ensures that authorization is applied after authentication.

Validating JWT Tokens

Once JWT authentication is set up, the next step is to validate the tokens in your API controllers. You can do this by applying the [Authorize] attribute to your controller or individual actions.

[ApiController]
[Route("api/[controller]")]
public class WeatherForecastController : ControllerBase
{
    [HttpGet]
    [Authorize]
    public IActionResult Get()
    {
        var user = HttpContext.User;
        return Ok(new { message = "Hello, authorized user!", claims = user.Claims.Select(c => new { c.Type, c.Value }) });
    }
}

This example shows how to protect the Get method of the WeatherForecastController using the [Authorize] attribute. Here's the explanation:

  • HttpContext.User: Retrieves the claims principal representing the authenticated user.
  • Ok(): Returns a 200 OK response along with a message and user claims.

Handling Unauthorized Access

When a user tries to access an endpoint without a valid token, the API should return a 401 Unauthorized response. This can be handled globally or on an action-by-action basis by customizing the response in the authentication middleware.

services.AddAuthentication(options =>
{
    options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
    options.Events = new JwtBearerEvents
    {
        OnChallenge = context =>
        {
            context.HandleResponse();
            context.Response.StatusCode = 401;
            context.Response.ContentType = "application/json";
            var result = JsonConvert.SerializeObject(new { error = "Unauthorized access" });
            return context.Response.WriteAsync(result);
        }
    };
});

This code customizes the behavior of the JWT Bearer authentication middleware by overriding the OnChallenge event. The explanation is as follows:

  • context.HandleResponse(): Prevents the default response from being sent.
  • context.Response.StatusCode: Sets the HTTP status code to 401.
  • context.Response.ContentType: Sets the response content type to JSON.
  • context.Response.WriteAsync(): Writes a custom error message to the response.

Edge Cases & Gotchas

When implementing JWT validation, several edge cases and gotchas can arise. One common mistake is failing to validate the token expiration correctly.

var tokenValidationParameters = new TokenValidationParameters
{
    ValidateIssuer = true,
    ValidateAudience = true,
    ValidateLifetime = false, // This is incorrect
};

In the above example, setting ValidateLifetime to false allows expired tokens to be accepted, leading to potential security vulnerabilities. The correct approach is:

var tokenValidationParameters = new TokenValidationParameters
{
    ValidateIssuer = true,
    ValidateAudience = true,
    ValidateLifetime = true, // Correctly validating lifetime
};

Another common pitfall is neglecting to validate the issuer and audience. Tokens should be verified against expected values to ensure they come from a trusted source.

Performance & Best Practices

To ensure optimal performance and security when validating JWTs in ASP.NET Core, consider the following best practices:

  • Use Asynchronous Methods: Always use asynchronous methods for I/O-bound operations, such as database calls, to avoid blocking threads.
  • Cache Validated Tokens: Implement caching mechanisms to store validated tokens temporarily, reducing the need for repeated validations.
  • Short Expiration Times: Set short expiration times for JWTs and implement refresh tokens for better security and performance.
  • Log Security Events: Monitor and log security-related events to detect anomalies and potential breaches.

Measuring Performance

To measure the performance of your authentication flow, you can use tools like Application Insights or MiniProfiler to analyze the time taken for token validation and response times. This data can help identify bottlenecks and optimize your API further.

Real-World Scenario: Mini Project

Let's build a simple ASP.NET Core Web API that uses JWT for authentication and demonstrates the secure validation of tokens.

public class AuthController : ControllerBase
{
    private readonly IConfiguration _configuration;

    public AuthController(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    [HttpPost("login")]
    public IActionResult Login([FromBody] LoginModel login)
    {
        if (IsValidUser(login))
        {
            var token = GenerateJwtToken(login.Username);
            return Ok(new { token });
        }
        return Unauthorized();
    }

    private bool IsValidUser(LoginModel login)
    {
        // Validate user credentials (this should query a database in a real app)
        return login.Username == "test" && login.Password == "password";
    }

    private string GenerateJwtToken(string username)
    {
        var claims = new[]
        {
            new Claim(JwtRegisteredClaimNames.Sub, username),
            new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())
        };
        var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["Jwt:Key"]));
        var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
        var token = new JwtSecurityToken(
            issuer: _configuration["Jwt:Issuer"],
            audience: _configuration["Jwt:Audience"],
            claims: claims,
            expires: DateTime.Now.AddMinutes(30),
            signingCredentials: creds);
        return new JwtSecurityTokenHandler().WriteToken(token);
    }
}

This AuthController handles user login and generates a JWT token. Here's a detailed explanation:

  • Login: Validates user credentials against hardcoded values (replace with database validation in production).
  • GenerateJwtToken: Creates a JWT with specified claims, issuer, audience, and expiration.
  • Return Token: Sends the generated token back to the client upon successful authentication.

Conclusion

  • Understanding the structure and validation process of JWT is crucial for secure authentication.
  • Always validate issuer, audience, and token expiration to prevent security vulnerabilities.
  • Implement best practices such as caching, using async methods, and monitoring performance.
  • Test your authentication flow thoroughly to ensure it meets security requirements.
  • Consider using libraries and tools that facilitate JWT handling and validation.

S
Shubham Saini
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

CWE-306: Securing Sensitive ASP.NET Core Endpoints with Authentication Middleware
Jun 01, 2026
CWE-94: Preventing Code Injection in ASP.NET Core Dynamic Expression Evaluation
Jun 01, 2026
CWE-502: Preventing Insecure Deserialization in ASP.NET Core Web API
May 30, 2026
Best Practices for Secure Gemini API Integration in ASP.NET
Apr 03, 2026
Previous in ASP.NET Core
CWE-522: Implementing Secure Password Hashing in ASP.NET Core Ide…
Next in ASP.NET Core
CWE-613: Implementing Proper Session Expiry and Token Revocation …
Buy me a pizza

Comments

🔥 Trending This Month

  • 1
    CWE-269: Improper Privilege Management - Implementing the … 361 views
  • 2
    Send Email With HTML Template And PDF Using ASP.Net C# 17,215 views
  • 3
    Building Custom Bedrock Add-Ons with JavaScript: A Complet… 1,937 views
  • 4
    Error-An error occurred while processing your request in .… 11,964 views
  • 5
    Elasticsearch Integration in ASP.NET Core - Full-Text Sear… 243 views
  • 6
    Fix Gemini API Error 429: Quota Exceeded on Free Tier (Sys… 828 views
  • 7
    Integrating Google reCAPTCHA v3 in ASP.NET Core for Seamle… 613 views

On this page

🎯

Interview Prep

Ace your ASP.NET Core interview with curated Q&As for all levels.

View ASP.NET Core Interview Q&As

More in ASP.NET Core

  • How to Encrypt and Decrypt Password in Asp.Net 26684 views
  • Exception Handling Asp.Net Core 21720 views
  • HTTP Error 500.31 Failed to load ASP NET Core runtime 21177 views
  • How to implement Paypal in Asp.Net Core 20127 views
  • Task Scheduler in Asp.Net core 18201 views
View all ASP.NET Core posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1780
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Products
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • ASP.NET MVC
  • ASP.NET Web Forms
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • General Web Development
  • HTML, CSS
  • HTML/CSS
  • Java
  • JavaScript
  • JavaScript, HTML, CSS
  • JavaScript, Node.js
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor