Make CookieAuthenticationOptions.SlidingExpiration behaviour configurable
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 281
Description
### Issue
The `SlidingExpiration` option when set to `true` only configures the handler to re-issue the cookie if it processes a request more than half way through the expiry window. As a result, in a 1 hour expiry window a request processed at 29 minutes will not reset the window.
The `CheckForRefresh` method which handles this is `private`, and therefore this behaviour cannot be modified easily by the calling application.
### Potential solution
Introduce another configuration option that allows the developer to set the period elapsed of the timeout window at which the cookie will be re-issued - this should default to 50% of the `ExpireTimeSpan` for backwards compatibility.
Current handler code:
```
private void CheckForRefresh(AuthenticationTicket ticket)
{
var currentUtc = Clock.UtcNow;
var issuedUtc = ticket.Properties.IssuedUtc;
var expiresUtc = ticket.Properties.ExpiresUtc;
var allowRefresh = ticket.Properties.AllowRefresh ?? true;
if (issuedUtc != null && expiresUtc != null && Options.SlidingExpiration && allowRefresh)
{
var timeElapsed = currentUtc.Subtract(issuedUtc.Value);
var timeRemaining = expiresUtc.Value.Subtract(currentUtc);
if (timeRemaining < timeElapsed)
{
RequestRefresh(ticket);
}
}
}
```
Suggested handler code:
```
private void CheckForRefresh(AuthenticationTicket ticket)
{
var currentUtc = Clock.UtcNow;
var issuedUtc = ticket.Properties.IssuedUtc;
var refreshUtc = issuedUtc + Options.RefreshPeriod;
var allowRefresh = ticket.Properties.AllowRefresh ?? true;
if (issuedUtc != null && Options.SlidingExpiration && allowRefresh && currentUtc > refreshUtc)
{
RequestRefresh(ticket);
}
}
```
Contributor guide
Research direction
Start with the CookieAuthenticationHandler.CheckForRefresh method and the CookieAuthenticationOptions configuration described in the issue. Trace the existing SlidingExpiration behavior and related tests, if present. Done means the refresh threshold is configurable while preserving the current 50% default behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- authentication
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100