User commited on
Commit
1d7086a
·
1 Parent(s): 97c95a9

Extend sessions and reload language changes

Browse files
TaskTrackingSystem.WebApp/AccountEndpoints.cs CHANGED
@@ -249,7 +249,10 @@ public static class AccountEndpoints
249
  new AuthenticationProperties
250
  {
251
  IsPersistent = rememberMe,
252
- ExpiresUtc = rememberMe ? DateTimeOffset.UtcNow.AddDays(7) : DateTimeOffset.UtcNow.AddHours(8)
 
 
 
253
  });
254
  }
255
 
 
249
  new AuthenticationProperties
250
  {
251
  IsPersistent = rememberMe,
252
+ ExpiresUtc = DateTimeOffset.UtcNow.Add(
253
+ rememberMe
254
+ ? AuthenticationSessionDefaults.RememberMeLifetime
255
+ : AuthenticationSessionDefaults.SessionLifetime)
256
  });
257
  }
258
 
TaskTrackingSystem.WebApp/AuthenticationSessionDefaults.cs ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ namespace TaskTrackingSystem.WebApp;
2
+
3
+ public static class AuthenticationSessionDefaults
4
+ {
5
+ public static readonly TimeSpan SessionLifetime = TimeSpan.FromHours(12);
6
+ public static readonly TimeSpan RememberMeLifetime = TimeSpan.FromDays(7);
7
+ }
TaskTrackingSystem.WebApp/Components/App.razor CHANGED
@@ -40,15 +40,19 @@
40
  <script>
41
  window.languagePreference = {
42
  normalize(code) {
43
- return code === 'en' ? 'en' : 'my';
 
44
  },
45
  apply(storageKey, code) {
46
  const normalized = this.normalize(code);
 
47
  document.documentElement.lang = normalized;
48
  try {
49
  localStorage.setItem(storageKey, normalized);
50
  } catch {
51
  }
 
 
52
  return normalized;
53
  },
54
  init(storageKey) {
 
40
  <script>
41
  window.languagePreference = {
42
  normalize(code) {
43
+ const normalized = (code || '').toLowerCase();
44
+ return normalized === 'en' || normalized.startsWith('en-') ? 'en' : 'my';
45
  },
46
  apply(storageKey, code) {
47
  const normalized = this.normalize(code);
48
+ const culture = normalized === 'en' ? 'en-US' : 'my-MM';
49
  document.documentElement.lang = normalized;
50
  try {
51
  localStorage.setItem(storageKey, normalized);
52
  } catch {
53
  }
54
+ const secure = window.location.protocol === 'https:' ? '; Secure' : '';
55
+ document.cookie = `.AspNetCore.Culture=c=${culture}|uic=${culture}; Path=/; Max-Age=31536000; SameSite=Lax${secure}`;
56
  return normalized;
57
  },
58
  init(storageKey) {
TaskTrackingSystem.WebApp/Components/Partial/LanguageToggle.razor CHANGED
@@ -1,6 +1,7 @@
1
  @rendermode @(new InteractiveServerRenderMode(prerender: false))
2
  @implements IDisposable
3
  @inject TaskTrackingSystem.WebApp.Localization.UiLanguageService Language
 
4
 
5
  <button type="button"
6
  @onclick="ToggleLanguage"
@@ -32,6 +33,7 @@
32
  private async Task ToggleLanguage()
33
  {
34
  await Language.ToggleAsync();
 
35
  }
36
 
37
  private void OnLanguageChanged()
 
1
  @rendermode @(new InteractiveServerRenderMode(prerender: false))
2
  @implements IDisposable
3
  @inject TaskTrackingSystem.WebApp.Localization.UiLanguageService Language
4
+ @inject NavigationManager Navigation
5
 
6
  <button type="button"
7
  @onclick="ToggleLanguage"
 
33
  private async Task ToggleLanguage()
34
  {
35
  await Language.ToggleAsync();
36
+ Navigation.NavigateTo(Navigation.Uri, forceLoad: true);
37
  }
38
 
39
  private void OnLanguageChanged()
TaskTrackingSystem.WebApp/Localization/UiLanguageService.cs CHANGED
@@ -117,7 +117,5 @@ public sealed class UiLanguageService
117
  var culture = language == AppLanguage.Burmese ? CultureInfo.GetCultureInfo("my-MM") : CultureInfo.GetCultureInfo("en-US");
118
  CultureInfo.CurrentCulture = culture;
119
  CultureInfo.CurrentUICulture = culture;
120
- CultureInfo.DefaultThreadCurrentCulture = culture;
121
- CultureInfo.DefaultThreadCurrentUICulture = culture;
122
  }
123
  }
 
117
  var culture = language == AppLanguage.Burmese ? CultureInfo.GetCultureInfo("my-MM") : CultureInfo.GetCultureInfo("en-US");
118
  CultureInfo.CurrentCulture = culture;
119
  CultureInfo.CurrentUICulture = culture;
 
 
120
  }
121
  }
TaskTrackingSystem.WebApp/Program.cs CHANGED
@@ -1,5 +1,6 @@
1
  using System.Globalization;
2
  using Microsoft.AspNetCore.Authentication.Cookies;
 
3
  using TaskTrackingSystem.WebApp.Localization;
4
  using TaskTrackingSystem.Shared.Localization;
5
  using TaskTrackingSystem.WebApp;
@@ -51,13 +52,26 @@ builder.Services.AddScoped<ILocalizedContentService, LocalizedContentService>();
51
  builder.Services.AddScoped<TaskTrackingSystem.Shared.Localization.IContentTranslationService, ApiContentTranslationService>();
52
  builder.Services.AddScoped<TaskTrackingSystem.Shared.Localization.LanguageDetectionService>();
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  // Cookie authentication for Blazor pages and HTTP middleware.
55
  builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
56
  .AddCookie(options =>
57
  {
58
  options.LoginPath = "/login";
59
  options.AccessDeniedPath = "/login";
60
- options.ExpireTimeSpan = TimeSpan.FromHours(8);
61
  options.SlidingExpiration = true;
62
  options.Cookie.HttpOnly = true;
63
  options.Cookie.SameSite = SameSiteMode.Lax;
@@ -91,6 +105,7 @@ if (!app.Environment.IsDevelopment())
91
  }
92
 
93
  app.UseStaticFiles();
 
94
  app.UseAuthentication();
95
  app.UseAuthorization();
96
  app.UseAntiforgery();
 
1
  using System.Globalization;
2
  using Microsoft.AspNetCore.Authentication.Cookies;
3
+ using Microsoft.AspNetCore.Localization;
4
  using TaskTrackingSystem.WebApp.Localization;
5
  using TaskTrackingSystem.Shared.Localization;
6
  using TaskTrackingSystem.WebApp;
 
52
  builder.Services.AddScoped<TaskTrackingSystem.Shared.Localization.IContentTranslationService, ApiContentTranslationService>();
53
  builder.Services.AddScoped<TaskTrackingSystem.Shared.Localization.LanguageDetectionService>();
54
 
55
+ var supportedCultures = new[]
56
+ {
57
+ CultureInfo.GetCultureInfo("my-MM"),
58
+ CultureInfo.GetCultureInfo("en-US")
59
+ };
60
+
61
+ builder.Services.Configure<RequestLocalizationOptions>(options =>
62
+ {
63
+ options.DefaultRequestCulture = new RequestCulture("my-MM");
64
+ options.SupportedCultures = supportedCultures;
65
+ options.SupportedUICultures = supportedCultures;
66
+ });
67
+
68
  // Cookie authentication for Blazor pages and HTTP middleware.
69
  builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
70
  .AddCookie(options =>
71
  {
72
  options.LoginPath = "/login";
73
  options.AccessDeniedPath = "/login";
74
+ options.ExpireTimeSpan = AuthenticationSessionDefaults.SessionLifetime;
75
  options.SlidingExpiration = true;
76
  options.Cookie.HttpOnly = true;
77
  options.Cookie.SameSite = SameSiteMode.Lax;
 
105
  }
106
 
107
  app.UseStaticFiles();
108
+ app.UseRequestLocalization();
109
  app.UseAuthentication();
110
  app.UseAuthorization();
111
  app.UseAntiforgery();