diff --git a/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Timing/AbpTimeZoneMiddleware.cs b/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Timing/AbpTimeZoneMiddleware.cs index a126595a16..8ee803feb0 100644 --- a/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Timing/AbpTimeZoneMiddleware.cs +++ b/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/Timing/AbpTimeZoneMiddleware.cs @@ -34,7 +34,7 @@ public class AbpTimeZoneMiddleware : AbpMiddlewareBase, ITransientDependency if (timezone.IsNullOrWhiteSpace()) { // Try to get the timezone from the current running server if the setting and request are not available - timezone = context.RequestServices.GetRequiredService().GetCurrentIanaTimezoneName(); + timezone = await GetLocalTimeZoneAsync(); } var currentTimezoneProvider = context.RequestServices.GetRequiredService(); @@ -67,4 +67,16 @@ public class AbpTimeZoneMiddleware : AbpMiddlewareBase, ITransientDependency return null; } + + protected virtual Task GetLocalTimeZoneAsync() + { + if (TimeZoneInfo.Local.HasIanaId) + { + return Task.FromResult(TimeZoneInfo.Local.Id); + } + + return TimeZoneInfo.TryConvertWindowsIdToIanaId(TimeZoneInfo.Local.Id, out var ianaName) + ? Task.FromResult(ianaName) + : Task.FromResult(null); + } } diff --git a/framework/src/Volo.Abp.Timing/Volo/Abp/Timing/ITimezoneProvider.cs b/framework/src/Volo.Abp.Timing/Volo/Abp/Timing/ITimezoneProvider.cs index 8c649619e5..89b59724a4 100644 --- a/framework/src/Volo.Abp.Timing/Volo/Abp/Timing/ITimezoneProvider.cs +++ b/framework/src/Volo.Abp.Timing/Volo/Abp/Timing/ITimezoneProvider.cs @@ -14,8 +14,4 @@ public interface ITimezoneProvider string IanaToWindows(string ianaTimeZoneName); TimeZoneInfo GetTimeZoneInfo(string windowsOrIanaTimeZoneId); - - string GetCurrentWindowsTimezoneName(); - - string GetCurrentIanaTimezoneName(); } diff --git a/framework/src/Volo.Abp.Timing/Volo/Abp/Timing/TZConvertTimezoneProvider.cs b/framework/src/Volo.Abp.Timing/Volo/Abp/Timing/TZConvertTimezoneProvider.cs index a009f64e9e..565c04c6fa 100644 --- a/framework/src/Volo.Abp.Timing/Volo/Abp/Timing/TZConvertTimezoneProvider.cs +++ b/framework/src/Volo.Abp.Timing/Volo/Abp/Timing/TZConvertTimezoneProvider.cs @@ -32,15 +32,4 @@ public class TZConvertTimezoneProvider : ITimezoneProvider, ITransientDependency { return TZConvert.GetTimeZoneInfo(windowsOrIanaTimeZoneId); } - - public virtual string GetCurrentWindowsTimezoneName() - { - return TimeZoneInfo.Local.StandardName;; - } - - public virtual string GetCurrentIanaTimezoneName() - { - var timezone = GetCurrentWindowsTimezoneName(); - return TZConvert.WindowsToIana(timezone); - } } diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Timing/AbpTimeZoneMiddleware_Tests.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Timing/AbpTimeZoneMiddleware_Tests.cs index 7168c6c4a7..7f12e5a07d 100644 --- a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Timing/AbpTimeZoneMiddleware_Tests.cs +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/Timing/AbpTimeZoneMiddleware_Tests.cs @@ -1,15 +1,11 @@ using System; using System.Collections.Generic; -using System.Net; using System.Net.Http; -using System.Net.Http.Headers; -using System.Security.Claims; using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.Extensions.DependencyInjection; +using NSubstitute; using Shouldly; -using Volo.Abp.AspNetCore.Mvc.Authorization; -using Volo.Abp.Security.Claims; +using Volo.Abp.Settings; using Volo.Abp.Timing; using Xunit; @@ -19,13 +15,10 @@ public class AbpTimeZoneMiddleware_Tests : AspNetCoreMvcTestBase { private readonly ICurrentTimezoneProvider _currentTimezoneProvider; private readonly ITimezoneProvider _timezoneProvider; - private readonly FakeUserClaims _fakeRequiredService; - public AbpTimeZoneMiddleware_Tests() { _currentTimezoneProvider = GetRequiredService(); _timezoneProvider = GetRequiredService(); - _fakeRequiredService = GetRequiredService(); } protected override void ConfigureServices(IServiceCollection services) @@ -42,7 +35,7 @@ public class AbpTimeZoneMiddleware_Tests : AspNetCoreMvcTestBase using (_currentTimezoneProvider.Change("UTC")) { var result = await Client.GetStringAsync("api/timing-test"); - result.ShouldBe(_timezoneProvider.GetCurrentIanaTimezoneName()); + result.ShouldBe(GetLocalTimeZone()); } // Query string @@ -78,51 +71,79 @@ public class AbpTimeZoneMiddleware_Tests : AspNetCoreMvcTestBase } } - [Fact] - public async Task Should_Not_Override_TimeZone_Setting_By_Request_For_Authenticated_User() + private string GetLocalTimeZone() { - _fakeRequiredService.Claims.AddRange(new[] + if (TimeZoneInfo.Local.HasIanaId) { - new Claim(AbpClaimTypes.UserId, AuthTestController.FakeUserId.ToString()) + return TimeZoneInfo.Local.Id; + } + + return TimeZoneInfo.TryConvertWindowsIdToIanaId(TimeZoneInfo.Local.Id, out var ianaName) + ? ianaName + : null; + } +} + + +public class AbpTimeZoneMiddleware_With_SettingValue_Tests : AspNetCoreMvcTestBase +{ + private readonly ICurrentTimezoneProvider _currentTimezoneProvider; + public AbpTimeZoneMiddleware_With_SettingValue_Tests() + { + _currentTimezoneProvider = GetRequiredService(); + } + + protected override void ConfigureServices(IServiceCollection services) + { + var settingStore = Substitute.For(); + settingStore.GetOrNullAsync(TimingSettingNames.TimeZone).Returns("Asia/Shanghai"); + services.AddSingleton(settingStore); + + services.Configure(options => + { + options.Kind = DateTimeKind.Utc; }); + } - using (_currentTimezoneProvider.Change("Europe/Berlin")) + [Fact] + public async Task Should_Not_Override_TimeZone_Setting_By_Request() + { + using (_currentTimezoneProvider.Change("UTC")) { var result = await Client.GetStringAsync("api/timing-test"); - - result.ShouldBe(_timezoneProvider.GetCurrentIanaTimezoneName()); + result.ShouldBe("Asia/Shanghai"); } // Query string - using (_currentTimezoneProvider.Change("Europe/Berlin")) + using (_currentTimezoneProvider.Change("UTC")) { var result = await Client.GetStringAsync("api/timing-test?__timezone=Europe/Istanbul"); - result.ShouldBe(_timezoneProvider.GetCurrentIanaTimezoneName()); + result.ShouldBe("Asia/Shanghai"); } // Header - using (_currentTimezoneProvider.Change("Europe/Berlin")) + using (_currentTimezoneProvider.Change("UTC")) { - Client.DefaultRequestHeaders.Add("__timezone", "Asia/Shanghai"); + Client.DefaultRequestHeaders.Add("__timezone", "Europe/Istanbul"); var result = await Client.GetStringAsync("api/timing-test"); - result.ShouldBe("UTC"); + result.ShouldBe("Asia/Shanghai"); } // Form - using (_currentTimezoneProvider.Change("Europe/Berlin")) + using (_currentTimezoneProvider.Change("UTC")) { Client.DefaultRequestHeaders.Remove("__timezone"); - var result = await Client.PostAsync("api/timing-test", new FormUrlEncodedContent(new[] {new KeyValuePair("__timezone", "Europe/Germany")})); - (await result.Content.ReadAsStringAsync()).ShouldBe("UTC"); + var result = await Client.PostAsync("api/timing-test", new FormUrlEncodedContent(new[] {new KeyValuePair("__timezone", "Europe/Istanbul")})); + (await result.Content.ReadAsStringAsync()).ShouldBe("Asia/Shanghai"); } // Cookie - using (_currentTimezoneProvider.Change("Europe/Berlin")) + using (_currentTimezoneProvider.Change("UTC")) { Client.DefaultRequestHeaders.Remove("__timezone"); Client.DefaultRequestHeaders.Add("Cookie", "__timezone=Europe/Istanbul"); var result = await Client.GetStringAsync("api/timing-test"); - result.ShouldBe("UTC"); + result.ShouldBe("Asia/Shanghai"); } } }