Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

Commit bd19ba9

Browse files
authored
Revert obsoleting CookieAuthenticationOptions.ExpireTimeSpan (#1296)
- Revert the obsoleting of CookieAuthenticationOptions.ExpireTimeSpan in #1285 - Add test to ensure Cookie.Expiration is ignored
1 parent 658f462 commit bd19ba9

File tree

4 files changed

+45
-39
lines changed

4 files changed

+45
-39
lines changed

src/Microsoft.AspNetCore.Authentication.Cookies/CookieAuthenticationHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,14 +270,14 @@ public async virtual Task SignInAsync(ClaimsPrincipal user, AuthenticationProper
270270

271271
if (!signInContext.Properties.ExpiresUtc.HasValue)
272272
{
273-
signInContext.Properties.ExpiresUtc = issuedUtc.Add(Options.Cookie.Expiration ?? default(TimeSpan));
273+
signInContext.Properties.ExpiresUtc = issuedUtc.Add(Options.ExpireTimeSpan);
274274
}
275275

276276
await Events.SigningIn(signInContext);
277277

278278
if (signInContext.Properties.IsPersistent)
279279
{
280-
var expiresUtc = signInContext.Properties.ExpiresUtc ?? issuedUtc.Add(Options.Cookie.Expiration ?? default(TimeSpan));
280+
var expiresUtc = signInContext.Properties.ExpiresUtc ?? issuedUtc.Add(Options.ExpireTimeSpan);
281281
signInContext.CookieOptions.Expires = expiresUtc.ToUniversalTime();
282282
}
283283

src/Microsoft.AspNetCore.Authentication.Cookies/CookieAuthenticationOptions.cs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ public class CookieAuthenticationOptions : AuthenticationSchemeOptions
2121
SameSite = SameSiteMode.Lax,
2222
HttpOnly = true,
2323
SecurePolicy = CookieSecurePolicy.SameAsRequest,
24-
Expiration = TimeSpan.FromDays(14),
2524
};
2625

2726
/// <summary>
2827
/// Create an instance of the options initialized with the default values
2928
/// </summary>
3029
public CookieAuthenticationOptions()
3130
{
31+
ExpireTimeSpan = TimeSpan.FromDays(14);
3232
ReturnUrlParameter = CookieAuthenticationDefaults.ReturnUrlParameter;
3333
SlidingExpiration = true;
3434
Events = new CookieAuthenticationEvents();
@@ -42,7 +42,6 @@ public CookieAuthenticationOptions()
4242
/// <seealso cref="CookieBuilder.SameSite"/> defaults to <see cref="SameSiteMode.Lax"/>.
4343
/// <seealso cref="CookieBuilder.HttpOnly"/> defaults to <c>true</c>.
4444
/// <seealso cref="CookieBuilder.SecurePolicy"/> defaults to <see cref="CookieSecurePolicy.SameAsRequest"/>.
45-
/// <seealso cref="CookieBuilder.Expiration"/> defaults to 14 days.
4645
/// </para>
4746
/// </summary>
4847
/// <remarks>
@@ -60,9 +59,7 @@ public CookieAuthenticationOptions()
6059
/// The default is true, which means the cookie will only be passed to http requests and is not made available to script on the page.
6160
/// </para>
6261
/// <para>
63-
/// <seealso cref="CookieBuilder.Expiration"/> controls how much time the cookie will remain valid from the point it is created. The expiration
64-
/// information is in the protected cookie ticket. Because of that an expired cookie will be ignored
65-
/// even if it is passed to the server after the browser should have purged it
62+
/// <seealso cref="CookieBuilder.Expiration"/> is currently ignored. Use <see cref="ExpireTimeSpan"/> to control lifetime of cookie authentication.
6663
/// </para>
6764
/// </remarks>
6865
public CookieBuilder Cookie
@@ -140,6 +137,19 @@ public CookieBuilder Cookie
140137
/// </summary>
141138
public ITicketStore SessionStore { get; set; }
142139

140+
/// <summary>
141+
/// <para>
142+
/// Controls how much time the authentication ticket stored in the cookie will remain valid from the point it is created
143+
/// The expiration information is stored in the protected cookie ticket. Because of that an expired cookie will be ignored
144+
/// even if it is passed to the server after the browser should have purged it.
145+
/// </para>
146+
/// <para>
147+
/// This is separate from the value of <seealso cref="CookieOptions.Expires"/>, which specifies
148+
/// how long the browser will keep the cookie.
149+
/// </para>
150+
/// </summary>
151+
public TimeSpan ExpireTimeSpan { get; set; }
152+
143153
#region Obsolete API
144154
/// <summary>
145155
/// <para>
@@ -201,23 +211,6 @@ public CookieBuilder Cookie
201211
/// </summary>
202212
[Obsolete("This property is obsolete and will be removed in a future version. The recommended alternative is " + nameof(Cookie) + "." + nameof(CookieBuilder.SecurePolicy) + ".")]
203213
public CookieSecurePolicy CookieSecure { get => Cookie.SecurePolicy; set => Cookie.SecurePolicy = value; }
204-
205-
/// <summary>
206-
/// <para>
207-
/// This property is obsolete and will be removed in a future version. The recommended alternative is <seealso cref="CookieBuilder.Expiration"/> on <see cref="Cookie"/>.
208-
/// </para>
209-
/// <para>
210-
/// Controls how much time the cookie will remain valid from the point it is created. The expiration
211-
/// information is in the protected cookie ticket. Because of that an expired cookie will be ignored
212-
/// even if it is passed to the server after the browser should have purged it
213-
/// </para>
214-
/// </summary>
215-
[Obsolete("This property is obsolete and will be removed in a future version. The recommended alternative is " + nameof(Cookie) + "." + nameof(CookieBuilder.Expiration) + ".")]
216-
public TimeSpan ExpireTimeSpan
217-
{
218-
get => Cookie.Expiration ?? default(TimeSpan);
219-
set => Cookie.Expiration = value;
220-
}
221214
#endregion
222215
}
223216
}

src/Microsoft.AspNetCore.Authentication.Cookies/Microsoft.AspNetCore.Authentication.Cookies.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,4 @@
1919
<ProjectReference Include="..\Microsoft.AspNetCore.Authentication\Microsoft.AspNetCore.Authentication.csproj" />
2020
</ItemGroup>
2121

22-
<ItemGroup>
23-
<Folder Include="Properties\" />
24-
</ItemGroup>
25-
2622
</Project>

test/Microsoft.AspNetCore.Authentication.Test/CookieTests.cs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,23 @@ public async Task SignInCausesDefaultCookieToBeCreated()
143143
Assert.DoesNotContain("; secure", setCookie);
144144
}
145145

146+
[Fact]
147+
public async Task CookieExpirationOptionIsIgnored()
148+
{
149+
var server = CreateServerWithServices(s => s.AddAuthentication().AddCookie(o =>
150+
{
151+
o.Cookie.Name = "TestCookie";
152+
// this is currently ignored. Users should set o.ExpireTimeSpan instead
153+
o.Cookie.Expiration = TimeSpan.FromDays(10);
154+
}), SignInAsAlice);
155+
156+
var transaction = await SendAsync(server, "http://example.com/testpath");
157+
158+
var setCookie = transaction.SetCookie;
159+
Assert.StartsWith("TestCookie=", setCookie);
160+
Assert.DoesNotContain("; expires=", setCookie);
161+
}
162+
146163
[Fact]
147164
public async Task SignInWrongAuthTypeThrows()
148165
{
@@ -277,7 +294,7 @@ public async Task CookieStopsWorkingAfterExpiration()
277294
{
278295
var server = CreateServer(o =>
279296
{
280-
o.Cookie.Expiration = TimeSpan.FromMinutes(10);
297+
o.ExpireTimeSpan = TimeSpan.FromMinutes(10);
281298
o.SlidingExpiration = false;
282299
}, SignInAsAlice);
283300

@@ -306,7 +323,7 @@ public async Task CookieExpirationCanBeOverridenInSignin()
306323
{
307324
var server = CreateServer(o =>
308325
{
309-
o.Cookie.Expiration = TimeSpan.FromMinutes(10);
326+
o.ExpireTimeSpan = TimeSpan.FromMinutes(10);
310327
o.SlidingExpiration = false;
311328
},
312329
context =>
@@ -339,7 +356,7 @@ public async Task ExpiredCookieWithValidatorStillExpired()
339356
{
340357
var server = CreateServer(o =>
341358
{
342-
o.Cookie.Expiration = TimeSpan.FromMinutes(10);
359+
o.ExpireTimeSpan = TimeSpan.FromMinutes(10);
343360
o.Events = new CookieAuthenticationEvents
344361
{
345362
OnValidatePrincipal = ctx =>
@@ -367,7 +384,7 @@ public async Task CookieCanBeRejectedAndSignedOutByValidator()
367384
{
368385
var server = CreateServer(o =>
369386
{
370-
o.Cookie.Expiration = TimeSpan.FromMinutes(10);
387+
o.ExpireTimeSpan = TimeSpan.FromMinutes(10);
371388
o.SlidingExpiration = false;
372389
o.Events = new CookieAuthenticationEvents
373390
{
@@ -395,7 +412,7 @@ public async Task CookieNotRenewedAfterSignOut()
395412
{
396413
var server = CreateServer(o =>
397414
{
398-
o.Cookie.Expiration = TimeSpan.FromMinutes(10);
415+
o.ExpireTimeSpan = TimeSpan.FromMinutes(10);
399416
o.SlidingExpiration = false;
400417
o.Events = new CookieAuthenticationEvents
401418
{
@@ -431,7 +448,7 @@ public async Task CookieCanBeRenewedByValidator()
431448
{
432449
var server = CreateServer(o =>
433450
{
434-
o.Cookie.Expiration = TimeSpan.FromMinutes(10);
451+
o.ExpireTimeSpan = TimeSpan.FromMinutes(10);
435452
o.SlidingExpiration = false;
436453
o.Events = new CookieAuthenticationEvents
437454
{
@@ -476,7 +493,7 @@ public async Task CookieCanBeRenewedByValidatorWithSlidingExpiry()
476493
{
477494
var server = CreateServer(o =>
478495
{
479-
o.Cookie.Expiration = TimeSpan.FromMinutes(10);
496+
o.ExpireTimeSpan = TimeSpan.FromMinutes(10);
480497
o.Events = new CookieAuthenticationEvents
481498
{
482499
OnValidatePrincipal = ctx =>
@@ -520,7 +537,7 @@ public async Task CookieValidatorOnlyCalledOnce()
520537
{
521538
var server = CreateServer(o =>
522539
{
523-
o.Cookie.Expiration = TimeSpan.FromMinutes(10);
540+
o.ExpireTimeSpan = TimeSpan.FromMinutes(10);
524541
o.SlidingExpiration = false;
525542
o.Events = new CookieAuthenticationEvents
526543
{
@@ -569,7 +586,7 @@ public async Task ShouldRenewUpdatesIssuedExpiredUtc(bool sliding)
569586
DateTimeOffset? lastExpiresDate = null;
570587
var server = CreateServer(o =>
571588
{
572-
o.Cookie.Expiration = TimeSpan.FromMinutes(10);
589+
o.ExpireTimeSpan = TimeSpan.FromMinutes(10);
573590
o.SlidingExpiration = sliding;
574591
o.Events = new CookieAuthenticationEvents
575592
{
@@ -619,7 +636,7 @@ public async Task CookieExpirationCanBeOverridenInEvent()
619636
{
620637
var server = CreateServer(o =>
621638
{
622-
o.Cookie.Expiration = TimeSpan.FromMinutes(10);
639+
o.ExpireTimeSpan = TimeSpan.FromMinutes(10);
623640
o.SlidingExpiration = false;
624641
o.Events = new CookieAuthenticationEvents()
625642
{
@@ -656,7 +673,7 @@ public async Task CookieIsRenewedWithSlidingExpiration()
656673
{
657674
var server = CreateServer(o =>
658675
{
659-
o.Cookie.Expiration = TimeSpan.FromMinutes(10);
676+
o.ExpireTimeSpan = TimeSpan.FromMinutes(10);
660677
o.SlidingExpiration = true;
661678
},
662679
SignInAsAlice);

0 commit comments

Comments
 (0)