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-613: Implementing Proper Session Expiry and Token Revocation in ASP.NET Core

CWE-613: Implementing Proper Session Expiry and Token Revocation in ASP.NET Core

Date- Jun 02,2026 256
cwe 613 session expiry

Overview

CWE-613, or 'Insufficient Session Expiration', highlights a significant security vulnerability where sessions do not expire as expected, potentially allowing unauthorized users to access sensitive information. This issue can lead to session hijacking, where an attacker gains unauthorized access by utilizing a valid session token. Proper session management is vital to mitigate these risks, ensuring that user sessions are appropriately terminated after a certain period of inactivity or upon explicit logout.

Real-world use cases for implementing proper session expiry and token revocation include online banking applications, e-commerce platforms, and any service handling sensitive user information. In such environments, maintaining the confidentiality, integrity, and availability of user sessions is paramount. This article will explore the necessary steps and best practices for implementing robust session management in ASP.NET Core applications.

Prerequisites

  • ASP.NET Core Knowledge: Understanding of ASP.NET Core framework and its middleware pipeline.
  • Authentication Mechanisms: Familiarity with ASP.NET Core Identity for user authentication.
  • Token-Based Authentication: Basic understanding of JWT (JSON Web Tokens) and their usage in securing APIs.
  • Entity Framework Core: Knowledge of EF Core for data persistence, particularly for managing user sessions.
  • HTTP Protocol: Understanding of how HTTP sessions work, including cookies and headers.

Session Management in ASP.NET Core

ASP.NET Core provides a flexible framework for managing user sessions through middleware components. The session state can be stored in-memory, on a distributed cache, or in a database, depending on the scale and needs of the application. Properly managing session states is critical to avoid vulnerabilities associated with session fixation and replay attacks.

In ASP.NET Core, sessions are typically managed using cookies. Each session is associated with a unique session ID, which is stored in a cookie on the client-side. This allows the server to identify the session and retrieve associated data. However, without proper expiration and revocation mechanisms, these sessions can remain valid indefinitely, posing a significant security risk.

public void ConfigureServices(IServiceCollection services)
{
    services.AddDistributedMemoryCache(); // Adds a memory cache
    services.AddSession(options =>
    {
        options.IdleTimeout = TimeSpan.FromMinutes(30); // Set session timeout
        options.Cookie.HttpOnly = true; // Prevent access to the cookie via JavaScript
        options.Cookie.IsEssential = true; // Ensure cookie is sent with requests
    });
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseSession(); // Enable session middleware
    app.UseRouting();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });
}

In this code snippet, we first configure the session services in the ConfigureServices method. The AddDistributedMemoryCache method adds a memory cache service, while AddSession configures session options. The IdleTimeout property sets the session expiration time to 30 minutes, meaning that if there is no activity for this duration, the session will expire. The HttpOnly property is set to true to prevent JavaScript access to the cookie, enhancing security. Finally, we enable the session middleware in the Configure method.

Understanding Session Cookies

Session cookies are crucial for maintaining state in stateless HTTP communication. When a user logs in, the server creates a session and sends a session ID back to the client as a cookie. This cookie is then included in subsequent requests, allowing the server to authenticate the user. However, it is vital to ensure that these cookies are secure and expire appropriately to prevent unauthorized access.

services.AddSession(options =>
{
    options.Cookie.SecurePolicy = CookieSecurePolicy.Always; // Use HTTPS
    options.Cookie.SameSite = SameSiteMode.Strict; // Mitigate CSRF attacks
});

In this snippet, we enhance the security of session cookies by enforcing the SecurePolicy to always use HTTPS, ensuring that cookies are only sent over secure connections. The SameSite attribute is set to Strict, which helps mitigate Cross-Site Request Forgery (CSRF) attacks by restricting how cookies are sent with cross-origin requests.

Token Revocation Strategies

Token revocation is a critical aspect of session management, particularly in scenarios where users can log out or when tokens may have been compromised. ASP.NET Core supports various strategies for revoking tokens, ensuring that once a user logs out, their session is terminated effectively.

Revocation can be implemented using a simple in-memory store, a database, or a more sophisticated approach using a distributed cache. The choice of strategy depends on the application's architecture and performance requirements. An effective revocation strategy allows for immediate termination of sessions, preventing unauthorized access.

public class TokenService
{
    private readonly IDatabase _database;
    public TokenService(IDatabase database)
    {
        _database = database;
    }

    public async Task RevokeToken(string token)
    {
        await _database.DeleteTokenAsync(token); // Remove token from the database
    }
}

This TokenService class defines a method RevokeToken that takes a token as a parameter and deletes it from the database. This ensures that once a user logs out, their token is no longer valid, effectively terminating their session.

Revoking Refresh Tokens

In applications using refresh tokens, it is equally important to manage their lifecycle. Refresh tokens allow users to obtain new access tokens without re-authenticating. However, if a refresh token is not revoked upon logout or if it is compromised, it can lead to security vulnerabilities.

public async Task RevokeRefreshToken(string refreshToken)
{
    var token = await _database.GetRefreshTokenAsync(refreshToken);
    if (token != null)
    {
        token.IsRevoked = true; // Mark token as revoked
        await _database.UpdateTokenAsync(token);
    }
}

In this example, the RevokeRefreshToken method checks if the provided refresh token exists in the database. If found, it marks the token as revoked and updates the database. This ensures that the refresh token cannot be used to obtain new access tokens after user logout.

Edge Cases & Gotchas

When implementing session expiry and token revocation, several edge cases and pitfalls can lead to security vulnerabilities. Understanding these scenarios is crucial for maintaining robust session management.

One common pitfall is failing to clear session data on logout. If session data persists after a user logs out, an attacker could exploit this by gaining access to the session data through an active session cookie.

public async Task Logout()
{
    await _database.ClearUserSessionAsync(); // Clear session data
    HttpContext.SignOutAsync(); // Sign out the user
}

In this Logout method, we ensure that the user's session data is cleared from the database before signing them out. This prevents any unauthorized access to session data after logout.

Performance & Best Practices

Implementing proper session expiry and token revocation requires careful consideration of performance and best practices. Efficient session management can significantly impact the responsiveness of an application and the user experience.

Using distributed caching solutions, such as Redis or SQL Server, can enhance performance when managing sessions at scale. These solutions provide faster access to session data and allow for session sharing across multiple instances of an application.

services.AddStackExchangeRedisCache(options =>
{
    options.Configuration = "localhost:6379"; // Redis configuration
});

services.AddSession(options =>
{
    options.Cookie.Name = "MyApp.Session";
    options.IdleTimeout = TimeSpan.FromMinutes(15);
});

In this code, we configure Redis as the distributed cache for session management. The AddStackExchangeRedisCache method sets up Redis with the specified configuration. We also set a shorter IdleTimeout to improve security, balancing performance with the need for timely session expirations.

Real-World Scenario: Building a Secure Login System

In this section, we will build a simple login system that integrates session expiry and token revocation. The application will allow users to log in, maintain their session, and securely log out.

public class AccountController : Controller
{
    private readonly TokenService _tokenService;
    public AccountController(TokenService tokenService)
    {
        _tokenService = tokenService;
    }

    [HttpPost]
    public async Task Login(string username, string password)
    {
        var user = await _database.ValidateUserAsync(username, password);
        if (user != null)
        {
            var token = GenerateToken(user);
            HttpContext.Session.SetString("Token", token);
            return Ok();
        }
        return Unauthorized();
    }

    [HttpPost]
    public async Task Logout()
    {
        var token = HttpContext.Session.GetString("Token");
        if (token != null)
        {
            await _tokenService.RevokeToken(token);
            HttpContext.Session.Remove("Token");
        }
        return Ok();
    }
}

The AccountController manages user authentication. In the Login method, we validate the user's credentials and generate a token, which is stored in the session. Upon logout, we revoke the token and remove it from the session, ensuring secure session management.

Conclusion

  • Implementing proper session expiry and token revocation is critical for preventing unauthorized access.
  • Utilizing secure cookie attributes enhances the security of session management.
  • Revocation strategies should be well-defined to ensure tokens cannot be reused after logout.
  • Performance considerations are essential when managing sessions at scale.
  • Real-world applications should prioritize security to protect sensitive user information.

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

Related Articles

CWE-384: Preventing Session Fixation in ASP.NET Core with Secure Session Configuration
Apr 28, 2026
CWE-312: Preventing Cleartext Storage of Passwords and Tokens in ASP.NET Core
Jun 03, 2026
Securing Dapper Queries in ASP.NET Core Against SQL Injection
Apr 09, 2026
CWE-306: Missing Authentication for Critical Functions - Securing Sensitive Endpoints
Mar 23, 2026
Previous in ASP.NET Core
CWE-347: Secure JWT Token Validation in ASP.NET Core Web API
Next in ASP.NET Core
CWE-311: Securely Encrypting Sensitive Data at Rest Using ASP.NET…
Buy me a pizza

Comments

🔥 Trending This Month

  • 1
    CWE-269: Improper Privilege Management - Implementing the … 361 views
  • 2
    Building Custom Bedrock Add-Ons with JavaScript: A Complet… 1,937 views
  • 3
    Send Email With HTML Template And PDF Using ASP.Net C# 17,201 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… 827 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 26683 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