skip to content

In ASP.NET Core 10, what do SessionOptions.IdleTimeout, IOTimeout and Cookie default to, and what does IdleTimeout actually expire?

level: middleimportance: should knowfreq 40%

answer

  1. server record vs browser cookie
  2. IdleTimeout = 20 minutes, sliding
  3. IOTimeout = 1 minute for store calls
  4. .AspNetCore.Session, HttpOnly, SameSite Lax
  5. IsEssential = false, Expiration throws

basics

~20 s

IdleTimeout defaults to 20 minutes and is a sliding expiration on the server-side record, not on the cookie. IOTimeout (1 minute) bounds async store calls. The .AspNetCore.Session cookie is a browser-session cookie: HttpOnly, SameSite Lax, not essential.

solid answer

~40 s

`SessionOptions.IdleTimeout` is **20 minutes**. The middleware commits the record with a sliding expiration of that length, or refreshes it when nothing changed, so every request through `UseSession` restarts the clock; it expires the data in the `IDistributedCache`, not the cookie. `IOTimeout` is **1 minute** and caps `LoadAsync` and `CommitAsync`, throwing `OperationCanceledException` when a store call takes longer. `Cookie` is a `CookieBuilder` named `.AspNetCore.Session`, path `/`, `HttpOnly = true`, `SameSite = Lax`, `IsEssential = false`, `SecurePolicy = None`. It has no expiry: setting `Cookie.Expiration` throws `InvalidOperationException`, so the cookie lives until the browser session ends. After an idle expiry the browser still sends the old key, and the app simply starts an empty session under it.

code

csharp · 9 lines
csharp
builder.Services.AddSession(options =>
{
    options.IdleTimeout = TimeSpan.FromMinutes(15);   // sliding, on the stored record
    options.IOTimeout = TimeSpan.FromSeconds(5);      // async store calls only
    options.Cookie.Name = ".Mortgage.Session";
    options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
    options.Cookie.IsEssential = true;              // the wizard cannot work without it
    // options.Cookie.Expiration = TimeSpan.FromDays(1);  // throws InvalidOperationException
});

go deeper

for a junior

Remember IdleTimeout is 20 minutes and resets with each request, and that the cookie is called .AspNetCore.Session and is HttpOnly.

for a middle

Separate the stored record from the cookie: IdleTimeout slides the record through SetAsync or RefreshAsync, the cookie has no expiry, and IOTimeout of one minute caps only the async store calls.

for a senior

Bring the defaults that cause incidents: IsEssential false under a consent policy, SecurePolicy None on HTTPS sites, silent empty sessions after expiry, and no absolute lifetime in SessionOptions.

for a principal

Decide what the idle window should be for a given flow and whether session is the right home at all, given that it neither persists past the browser session nor caps total lifetime.

## Two clocks that people confuse ASP.NET Core session has a **server-side record** in an `IDistributedCache` and a **cookie** in the browser that names it. `SessionOptions` configures both, and the most common interview mistake is to treat `IdleTimeout` as the cookie's lifetime. | Option | Default | What it governs | |---|---|---| | `IdleTimeout` | 20 minutes | sliding expiration of the record in the store | | `IOTimeout` | 1 minute | how long `LoadAsync` and `CommitAsync` may wait on the store | | `Cookie.Name` | `.AspNetCore.Session` | the cookie that carries the protected session key | | `Cookie.Path` | `/` | the URL path the cookie is sent for | | `Cookie.HttpOnly` | `true` | page script cannot read the cookie | | `Cookie.SameSite` | `SameSiteMode.Lax` | cross-site sending rule | | `Cookie.IsEssential` | `false` | whether the cookie is written before consent | | `Cookie.SecurePolicy` | `CookieSecurePolicy.None` | whether the `Secure` flag is set | ## How IdleTimeout slides At the end of each request `SessionMiddleware` calls `ISession.CommitAsync`: 1. If the session was modified, `DistributedSession` serializes the whole record and calls `SetAsync` with `SetSlidingExpiration(IdleTimeout)`. 2. If it was not modified, it calls `RefreshAsync`, which restarts the sliding window in the store. 3. Either way, any request that passes through `UseSession` keeps the record alive. A request served before `UseSession` in the pipeline, such as a static file, does not. When the window lapses, the store evicts the record. The browser still holds `.AspNetCore.Session`, so the next request arrives with a valid-looking key; `DistributedSession` finds no data, logs *Accessing expired session* at Information level and carries on with an empty session under the same key. No exception tells your code the data expired, which is why a wizard should check for missing steps rather than assume they are there. `SessionOptions` has no absolute-lifetime option: a record keeps living for as long as requests keep arriving inside the idle window. ## Why the cookie has no expiry The session cookie is built by a private `SessionCookieBuilder` whose `Expiration` getter always returns `null` and whose setter throws *Expiration cannot be set for the cookie defined by SessionOptions*. The cookie is therefore a browser-session cookie, dropped when the browser session ends. Making session state outlive the browser is not what this feature is for; persistent data belongs in a database tied to the user account. ## IOTimeout and the store `IOTimeout` wraps each asynchronous store call in a linked `CancellationTokenSource` with `CancelAfter(IOTimeout)`. A slow store makes `LoadAsync` throw `OperationCanceledException` with *Timed out loading the session.*, and a slow commit throws *Timed out committing the session.*, which the middleware catches and logs. It does not help the synchronous path: if a handler touches session without calling `LoadAsync` first, the first access loads synchronously and the timeout does not cut it short. `Timeout.InfiniteTimeSpan` disables it. ## Cookie defaults that bite - **`IsEssential = false`.** With `UseCookiePolicy` and a `CheckConsentNeeded` callback that returns `true`, non-essential cookies are not issued until the visitor consents. The session cookie never arrives, every request starts a fresh session, and data written on one page is missing on the next. Either obtain consent or set `options.Cookie.IsEssential = true` when the session is genuinely necessary. - **`SecurePolicy = None`.** The session builder does not mark the cookie `Secure`, even on HTTPS. Set `options.Cookie.SecurePolicy = CookieSecurePolicy.Always` on an HTTPS-only site. - **Name and path.** Two apps on one host with the default name and path share a cookie name; give each its own `Cookie.Name`. These defaults come straight from `SessionOptions` and `SessionDefaults` in the Microsoft.AspNetCore.Session package, so they are worth checking against the source rather than memory.

  • In ASP.NET Core, does IdleTimeout restart when a request reads the session but changes nothing?
    Yes. At the end of every request that passes through `UseSession`, the middleware calls `CommitAsync`. With no changes it calls `IDistributedCache.RefreshAsync` on the record, which restarts the sliding expiration. Only requests handled before `UseSession` in the pipeline, or short-circuited ahead of it, leave the clock alone.
  • How does ASP.NET Core session behave before cookie consent when UseCookiePolicy requires consent?
    The session cookie is not essential by default, so the cookie policy suppresses it until consent is given. Each request then arrives without a key, the middleware mints a new one, and values written in the previous request are unreachable. Setting `Cookie.IsEssential = true` lets the cookie through, which should be reserved for sessions the site cannot work without.

  • IdleTimeout sets the session cookie's Expires attribute
  • The default session idle timeout is 30 minutes
  • IOTimeout is how long a session may stay idle
  • Setting Cookie.Expiration makes the session cookie persistent
  • An expired session makes GetString throw an exception
  • The session cookie is always marked Secure on HTTPS