Skip to content

Latest commit

 

History

History
117 lines (88 loc) · 3.62 KB

Session and State Management.md

File metadata and controls

117 lines (88 loc) · 3.62 KB

Session and State Management

Session and State Management are important aspects of web applications because HTTP is a stateless protocol, meaning each request is independent and doesn't retain any information about previous requests. ASP.NET Core provides several options to store and manage state between requests.

  1. Cookies: Cookies are small pieces of data stored on the client-side by the user's browser. They can store a limited amount of information (up to 4KB) and are used for simple state management, such as storing user preferences or authentication tokens.
// Setting a cookie
var cookieOptions = new CookieOptions { Expires = DateTime.UtcNow.AddDays(7) };
Response.Cookies.Append("MyCookie", "CookieValue", cookieOptions);

// Reading a cookie
var cookieValue = Request.Cookies["MyCookie"];

// Deleting a cookie
Response.Cookies.Delete("MyCookie");
  1. Session State: ASP.NET Core provides a built-in Session State mechanism for storing user-specific data on the server-side. Session data is stored in-memory by default but can be configured to use a distributed cache, such as Redis, for better scalability.

To use Session State, you need to configure it in the Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDistributedMemoryCache();
    services.AddSession(options =>
    {
        options.IdleTimeout = TimeSpan.FromMinutes(30);
        options.Cookie.HttpOnly = true;
        options.Cookie.IsEssential = true;
    });
    // Other services...
}

And enable it in the Configure method:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    // ...
    app.UseSession();
    // ...
}

Then you can use it in your controllers or middleware:

// Set session value
HttpContext.Session.SetString("MyKey", "MyValue");

// Get session value
var sessionValue = HttpContext.Session.GetString("MyKey");

// Remove session value
HttpContext.Session.Remove("MyKey");

// Clear session
HttpContext.Session.Clear();
  1. TempData: TempData is a short-lived storage mechanism that persists data between two consecutive requests. It's useful for passing data between actions, such as showing one-time messages after redirecting. TempData is built on top of the Session State, so you need to configure it as described above.
// Set TempData value
TempData["MyKey"] = "MyValue";

// Get TempData value
var tempDataValue = TempData["MyKey"] as string;
  1. Distributed Cache: When you need to share state between different instances of your application, a distributed cache can be used. ASP.NET Core supports various distributed cache implementations, such as Redis and SQL Server.

First, you need to configure the cache in the Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    services.AddStackExchangeRedisCache(options =>
    {
        options.Configuration = "localhost";
    });
    // Other services...
}

Then, you can use the IDistributedCache interface to store and retrieve data:

public class MyController : Controller
{
    private readonly IDistributedCache _cache;

    public MyController(IDistributedCache cache)
    {
        _cache = cache;
    }

    public async Task<IActionResult> Index()
    {
        // Set cache value
        await _cache.SetStringAsync("MyKey", "MyValue");

        // Get cache value
        var cacheValue = await _cache.GetStringAsync("MyKey");

        // Remove cache value
        await _cache.RemoveAsync("MyKey");

        return View();
    }
}

These are the main options for managing state in modern ASP.NET Core applications.