Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

Commit ec0c15a

Browse files
authored
Merge pull request #1028 from dotnet-architecture/enhancement/make-token-and-session-cookie-expiry-times-configurable
Make token and session cookie expiry times configurable
2 parents c6ad3f2 + 53d4865 commit ec0c15a

File tree

6 files changed

+65
-46
lines changed

6 files changed

+65
-46
lines changed

docker-compose.override.yml

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -326,40 +326,40 @@ services:
326326
environment:
327327
- ASPNETCORE_ENVIRONMENT=Production
328328
- ASPNETCORE_URLS=http://0.0.0.0:80
329-
- HealthChecks-UI__HealthChecks__1__Name=WebMVC HTTP Check
330-
- HealthChecks-UI__HealthChecks__1__Uri=http://webmvc/hc
331-
- HealthChecks-UI__HealthChecks__2__Name=WebSPA HTTP Check
332-
- HealthChecks-UI__HealthChecks__2__Uri=http://webspa/hc
333-
- HealthChecks-UI__HealthChecks__3__Name=Web Shopping Aggregator GW HTTP Check
334-
- HealthChecks-UI__HealthChecks__3__Uri=http://webshoppingagg/hc
335-
- HealthChecks-UI__HealthChecks__4__Name=Mobile Shopping Aggregator HTTP Check
336-
- HealthChecks-UI__HealthChecks__4__Uri=http://mobileshoppingagg/hc
337-
- HealthChecks-UI__HealthChecks__5__Name=Mobile Shopping API GW HTTP Check
338-
- HealthChecks-UI__HealthChecks__5__Uri=http://mobileshoppingapigw/hc
339-
- HealthChecks-UI__HealthChecks__6__Name=Mobile Marketing API GW HTTP Check
340-
- HealthChecks-UI__HealthChecks__6__Uri=http://mobilemarketingapigw/hc
341-
- HealthChecks-UI__HealthChecks__7__Name=Web Shopping API GW HTTP Check
342-
- HealthChecks-UI__HealthChecks__7__Uri=http://webshoppingapigw/hc
343-
- HealthChecks-UI__HealthChecks__8__Name=Web Marketing API GW HTTP Check
344-
- HealthChecks-UI__HealthChecks__8__Uri=http://webmarketingapigw/hc
345-
- HealthChecks-UI__HealthChecks__9__Name=Ordering HTTP Check
346-
- HealthChecks-UI__HealthChecks__9__Uri=http://ordering.api/hc
347-
- HealthChecks-UI__HealthChecks__10__Name=Ordering HTTP Background Check
348-
- HealthChecks-UI__HealthChecks__10__Uri=http://ordering.backgroundtasks/hc
349-
- HealthChecks-UI__HealthChecks__11__Name=Basket HTTP Check
350-
- HealthChecks-UI__HealthChecks__11__Uri=http://basket.api/hc
351-
- HealthChecks-UI__HealthChecks__12__Name=Catalog HTTP Check
352-
- HealthChecks-UI__HealthChecks__12__Uri=http://catalog.api/hc
353-
- HealthChecks-UI__HealthChecks__13__Name=Identity HTTP Check
354-
- HealthChecks-UI__HealthChecks__13__Uri=http://identity.api/hc
355-
- HealthChecks-UI__HealthChecks__14__Name=Marketing HTTP Check
356-
- HealthChecks-UI__HealthChecks__14__Uri=http://marketing.api/hc
357-
- HealthChecks-UI__HealthChecks__15__Name=Locations HTTP Check
358-
- HealthChecks-UI__HealthChecks__15__Uri=http://locations.api/hc
359-
- HealthChecks-UI__HealthChecks__16__Name=Payments HTTP Check
360-
- HealthChecks-UI__HealthChecks__16__Uri=http://payment.api/hc
361-
- HealthChecks-UI__HealthChecks__17__Name=Ordering SignalRHub HTTP Check
362-
- HealthChecks-UI__HealthChecks__17__Uri=http://ordering.signalrhub/hc
329+
- HealthChecks-UI__HealthChecks__0__Name=WebMVC HTTP Check
330+
- HealthChecks-UI__HealthChecks__0__Uri=http://webmvc/hc
331+
- HealthChecks-UI__HealthChecks__1__Name=WebSPA HTTP Check
332+
- HealthChecks-UI__HealthChecks__1__Uri=http://webspa/hc
333+
- HealthChecks-UI__HealthChecks__2__Name=Web Shopping Aggregator GW HTTP Check
334+
- HealthChecks-UI__HealthChecks__2__Uri=http://webshoppingagg/hc
335+
- HealthChecks-UI__HealthChecks__3__Name=Mobile Shopping Aggregator HTTP Check
336+
- HealthChecks-UI__HealthChecks__3__Uri=http://mobileshoppingagg/hc
337+
- HealthChecks-UI__HealthChecks__4__Name=Mobile Shopping API GW HTTP Check
338+
- HealthChecks-UI__HealthChecks__4__Uri=http://mobileshoppingapigw/hc
339+
- HealthChecks-UI__HealthChecks__5__Name=Mobile Marketing API GW HTTP Check
340+
- HealthChecks-UI__HealthChecks__5__Uri=http://mobilemarketingapigw/hc
341+
- HealthChecks-UI__HealthChecks__6__Name=Web Shopping API GW HTTP Check
342+
- HealthChecks-UI__HealthChecks__6__Uri=http://webshoppingapigw/hc
343+
- HealthChecks-UI__HealthChecks__7__Name=Web Marketing API GW HTTP Check
344+
- HealthChecks-UI__HealthChecks__7__Uri=http://webmarketingapigw/hc
345+
- HealthChecks-UI__HealthChecks__8__Name=Ordering HTTP Check
346+
- HealthChecks-UI__HealthChecks__8__Uri=http://ordering.api/hc
347+
- HealthChecks-UI__HealthChecks__9__Name=Ordering HTTP Background Check
348+
- HealthChecks-UI__HealthChecks__9__Uri=http://ordering.backgroundtasks/hc
349+
- HealthChecks-UI__HealthChecks__10__Name=Basket HTTP Check
350+
- HealthChecks-UI__HealthChecks__10__Uri=http://basket.api/hc
351+
- HealthChecks-UI__HealthChecks__11__Name=Catalog HTTP Check
352+
- HealthChecks-UI__HealthChecks__11__Uri=http://catalog.api/hc
353+
- HealthChecks-UI__HealthChecks__12__Name=Identity HTTP Check
354+
- HealthChecks-UI__HealthChecks__12__Uri=http://identity.api/hc
355+
- HealthChecks-UI__HealthChecks__13__Name=Marketing HTTP Check
356+
- HealthChecks-UI__HealthChecks__13__Uri=http://marketing.api/hc
357+
- HealthChecks-UI__HealthChecks__14__Name=Locations HTTP Check
358+
- HealthChecks-UI__HealthChecks__14__Uri=http://locations.api/hc
359+
- HealthChecks-UI__HealthChecks__15__Name=Payments HTTP Check
360+
- HealthChecks-UI__HealthChecks__15__Uri=http://payment.api/hc
361+
- HealthChecks-UI__HealthChecks__16__Name=Ordering SignalRHub HTTP Check
362+
- HealthChecks-UI__HealthChecks__16__Uri=http://ordering.signalrhub/hc
363363
- OrderingBackgroundTasksUrl=http://ordering.backgroundtasks/hc
364364
- ApplicationInsights__InstrumentationKey=${INSTRUMENTATION_KEY}
365365
- OrchestratorType=${ORCHESTRATOR_TYPE}

src/Services/Identity/Identity.API/Controllers/AccountController.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using Microsoft.eShopOnContainers.Services.Identity.API.Models;
1616
using Microsoft.eShopOnContainers.Services.Identity.API.Models.AccountViewModels;
1717
using Microsoft.eShopOnContainers.Services.Identity.API.Services;
18+
using Microsoft.Extensions.Configuration;
1819
using Microsoft.Extensions.Logging;
1920

2021
namespace Microsoft.eShopOnContainers.Services.Identity.API.Controllers
@@ -32,6 +33,7 @@ public class AccountController : Controller
3233
private readonly IClientStore _clientStore;
3334
private readonly ILogger<AccountController> _logger;
3435
private readonly UserManager<ApplicationUser> _userManager;
36+
private readonly IConfiguration _configuration;
3537

3638
public AccountController(
3739

@@ -40,13 +42,15 @@ public AccountController(
4042
IIdentityServerInteractionService interaction,
4143
IClientStore clientStore,
4244
ILogger<AccountController> logger,
43-
UserManager<ApplicationUser> userManager)
45+
UserManager<ApplicationUser> userManager,
46+
IConfiguration configuration)
4447
{
4548
_loginService = loginService;
4649
_interaction = interaction;
4750
_clientStore = clientStore;
4851
_logger = logger;
4952
_userManager = userManager;
53+
_configuration = configuration;
5054
}
5155

5256
/// <summary>
@@ -81,20 +85,21 @@ public async Task<IActionResult> Login(LoginViewModel model)
8185

8286
if (await _loginService.ValidateCredentials(user, model.Password))
8387
{
88+
var tokenLifetime = _configuration.GetValue("TokenLifetimeMinutes", 120);
89+
8490
var props = new AuthenticationProperties
8591
{
86-
ExpiresUtc = DateTimeOffset.UtcNow.AddHours(2),
92+
ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(tokenLifetime),
8793
AllowRefresh = true,
8894
RedirectUri = model.ReturnUrl
8995
};
9096

9197
if (model.RememberMe)
9298
{
93-
props = new AuthenticationProperties
94-
{
95-
IsPersistent = true,
96-
ExpiresUtc = DateTimeOffset.UtcNow.AddYears(10)
97-
};
99+
var permanentTokenLifetime = _configuration.GetValue("PermanentTokenLifetimeDays", 365);
100+
101+
props.ExpiresUtc = DateTimeOffset.UtcNow.AddDays(permanentTokenLifetime);
102+
props.IsPersistent = true;
98103
};
99104

100105
await _loginService.SignInAsync(user, props);

src/Services/Identity/Identity.API/appsettings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,7 @@
2525
"Name": "eshop",
2626
"ClientId": "your-clien-id",
2727
"ClientSecret": "your-client-secret"
28-
}
28+
},
29+
"TokenLifetimeMinutes": 120,
30+
"PermanentTokenLifetimeDays": 365
2931
}

src/Web/WebMVC/Controllers/AccountController.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,29 @@
66
using Microsoft.AspNetCore.Authentication;
77
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
88
using Microsoft.AspNetCore.Authentication.Cookies;
9+
using Microsoft.Extensions.Logging;
10+
using System;
911

1012
namespace Microsoft.eShopOnContainers.WebMVC.Controllers
1113
{
1214
[Authorize]
1315
public class AccountController : Controller
1416
{
17+
private readonly ILogger<AccountController> _logger;
18+
19+
public AccountController(ILogger<AccountController> logger)
20+
{
21+
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
22+
}
23+
1524
[Authorize]
1625
public async Task<IActionResult> SignIn(string returnUrl)
1726
{
1827
var user = User as ClaimsPrincipal;
19-
2028
var token = await HttpContext.GetTokenAsync("access_token");
2129

30+
_logger.LogInformation("----- User {@User} authenticated into {AppName}", user, Program.AppName);
31+
2232
if (token != null)
2333
{
2434
ViewData["access_token"] = token;

src/Web/WebMVC/Startup.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ public static IServiceCollection AddCustomAuthentication(this IServiceCollection
238238
var useLoadTest = configuration.GetValue<bool>("UseLoadTest");
239239
var identityUrl = configuration.GetValue<string>("IdentityUrl");
240240
var callBackUrl = configuration.GetValue<string>("CallBackUrl");
241+
var sessionCookieLifetime = configuration.GetValue("SessionCookieLifetimeMinutes", 60);
241242

242243
// Add Authentication services
243244

@@ -246,7 +247,7 @@ public static IServiceCollection AddCustomAuthentication(this IServiceCollection
246247
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
247248
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
248249
})
249-
.AddCookie(setup=>setup.ExpireTimeSpan = TimeSpan.FromHours(2))
250+
.AddCookie(setup=>setup.ExpireTimeSpan = TimeSpan.FromMinutes(sessionCookieLifetime))
250251
.AddOpenIdConnect(options =>
251252
{
252253
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;

src/Web/WebMVC/appsettings.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@
2727
"InstrumentationKey": ""
2828
},
2929
"HttpClientRetryCount": 8,
30-
"HttpClientExceptionsAllowedBeforeBreaking": 7
31-
}
30+
"HttpClientExceptionsAllowedBeforeBreaking": 7,
31+
"SessionCookieLifetimeMinutes": 60
32+
}

0 commit comments

Comments
 (0)