diff --git a/src/admin/Bootstrap.Admin/Bootstrap.Admin.csproj b/src/admin/Bootstrap.Admin/Bootstrap.Admin.csproj index 11577ce1..06d4ee18 100644 --- a/src/admin/Bootstrap.Admin/Bootstrap.Admin.csproj +++ b/src/admin/Bootstrap.Admin/Bootstrap.Admin.csproj @@ -9,12 +9,12 @@ - + - - + + diff --git a/src/admin/Bootstrap.Admin/Controllers/AccountController.cs b/src/admin/Bootstrap.Admin/Controllers/AccountController.cs index 3d605133..148a3393 100644 --- a/src/admin/Bootstrap.Admin/Controllers/AccountController.cs +++ b/src/admin/Bootstrap.Admin/Controllers/AccountController.cs @@ -110,7 +110,7 @@ namespace Bootstrap.Admin.Controllers if (string.IsNullOrEmpty(phone) || string.IsNullOrEmpty(code)) return RedirectLogin(); var auth = provider.Validate(phone, code); - HttpContext.Log(phone, auth); + await HttpContext.Log(phone, auth); if (auth) { var user = UserHelper.Retrieves().FirstOrDefault(u => u.UserName == phone); @@ -161,7 +161,7 @@ namespace Bootstrap.Admin.Controllers if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password)) return RedirectLogin(); var auth = UserHelper.Authenticate(userName, password); - HttpContext.Log(userName, auth); + await HttpContext.Log(userName, auth); return auth ? await SignInAsync(userName, remember == "true") : LoginView("", new LoginModel() { AuthFailed = true }); } diff --git a/src/admin/Bootstrap.Admin/Controllers/Api/LoginController.cs b/src/admin/Bootstrap.Admin/Controllers/Api/LoginController.cs index fca3795e..0f7c3e8a 100644 --- a/src/admin/Bootstrap.Admin/Controllers/Api/LoginController.cs +++ b/src/admin/Bootstrap.Admin/Controllers/Api/LoginController.cs @@ -25,7 +25,7 @@ namespace Bootstrap.Admin.Controllers.Api /// [Authorize] [HttpGet] - public QueryData Get([FromQuery]QueryLoginOption value) => value.RetrieveData(); + public QueryData Get([FromQuery] QueryLoginOption value) => value.RetrieveData(); /// /// JWT 登陆认证接口 @@ -34,9 +34,9 @@ namespace Bootstrap.Admin.Controllers.Api /// /// [HttpPost] - public string? Post([FromServices]IConfiguration config, [FromBody]User user) + public async Task Post([FromServices] IConfiguration config, [FromBody] User user) { - var token = string.Empty; + string? token = null; string userName = user.UserName; string password = user.Password; if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password) && UserHelper.Authenticate(userName, password)) @@ -50,7 +50,7 @@ namespace Bootstrap.Admin.Controllers.Api op.SecurityKey = tokenOption.SecurityKey; }); } - HttpContext.Log(userName, !string.IsNullOrEmpty(token)); + await HttpContext.Log(userName, !string.IsNullOrEmpty(token)); return token; } @@ -61,7 +61,7 @@ namespace Bootstrap.Admin.Controllers.Api /// /// [HttpPut] - public async Task Put([FromServices]ISMSProvider provider, [FromQuery]string phone) => string.IsNullOrEmpty(phone) ? new SMSResult() { Result = false, Msg = "手机号不可为空" } : await provider.SendCodeAsync(phone); + public async Task Put([FromServices] ISMSProvider provider, [FromQuery] string phone) => string.IsNullOrEmpty(phone) ? new SMSResult() { Result = false, Msg = "手机号不可为空" } : await provider.SendCodeAsync(phone); /// /// 跨域握手协议 diff --git a/src/admin/Bootstrap.Admin/Controllers/Api/LogsController.cs b/src/admin/Bootstrap.Admin/Controllers/Api/LogsController.cs index 2be32e71..5a5e7eb3 100644 --- a/src/admin/Bootstrap.Admin/Controllers/Api/LogsController.cs +++ b/src/admin/Bootstrap.Admin/Controllers/Api/LogsController.cs @@ -5,6 +5,7 @@ using Longbow.Web.Mvc; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System.Net; +using System.Threading.Tasks; namespace Bootstrap.Admin.Controllers.Api { @@ -22,7 +23,7 @@ namespace Bootstrap.Admin.Controllers.Api /// /// [HttpGet] - public QueryData Get([FromQuery]QueryLogOption value) + public QueryData Get([FromQuery] QueryLogOption value) { return value.RetrieveData(); } @@ -34,14 +35,14 @@ namespace Bootstrap.Admin.Controllers.Api /// /// [HttpPost] - public bool Post([FromServices]IIPLocatorProvider ipLocator, [FromBody]Log value) + public async Task Post([FromServices] IIPLocatorProvider ipLocator, [FromBody] Log value) { value.UserAgent = Request.Headers["User-Agent"]; var agent = new UserAgent(value.UserAgent); value.Ip = HttpContext.Connection.RemoteIpAddress?.ToIPv4String() ?? ""; value.Browser = $"{agent.Browser?.Name} {agent.Browser?.Version}"; value.OS = $"{agent.OS?.Name} {agent.OS?.Version}"; - value.City = ipLocator.Locate(value.Ip); + value.City = await ipLocator.Locate(value.Ip); value.UserName = User.Identity?.Name ?? string.Empty; return LogHelper.Save(value); } diff --git a/src/admin/Bootstrap.Admin/Extensions/CloudLoggerExtensions.cs b/src/admin/Bootstrap.Admin/Extensions/CloudLoggerExtensions.cs index ce067b7f..a913de36 100644 --- a/src/admin/Bootstrap.Admin/Extensions/CloudLoggerExtensions.cs +++ b/src/admin/Bootstrap.Admin/Extensions/CloudLoggerExtensions.cs @@ -4,6 +4,7 @@ using Microsoft.Extensions.Logging.Configuration; using Microsoft.Extensions.Options; using System; using System.Net.Http; +using System.Net.Http.Json; namespace Microsoft.Extensions.DependencyInjection { diff --git a/src/admin/Bootstrap.Admin/Pages/Extensions/HttpContextExtensions.cs b/src/admin/Bootstrap.Admin/Pages/Extensions/HttpContextExtensions.cs index 68438dd3..67a4b887 100644 --- a/src/admin/Bootstrap.Admin/Pages/Extensions/HttpContextExtensions.cs +++ b/src/admin/Bootstrap.Admin/Pages/Extensions/HttpContextExtensions.cs @@ -44,7 +44,11 @@ namespace Microsoft.AspNetCore.Http FirstAccessTime = DateTime.Now, Referer = context.Request.Headers["Referer"] }; - v.Location = locator?.Locate(v.Ip) ?? ""; + var t = locator.Locate(v.Ip); + if (t.IsCompletedSuccessfully) + { + v.Location = t.Result; + } return proxy(new AutoExpireCacheEntry(v, 1000 * 60, __ => onlineUserSvr.TryRemove(key, out _)), null); }, (key, v) => proxy(v, () => v.Reset())); } diff --git a/src/admin/Bootstrap.DataAccess.MongoDB/Bootstrap.DataAccess.MongoDB.csproj b/src/admin/Bootstrap.DataAccess.MongoDB/Bootstrap.DataAccess.MongoDB.csproj index f77d63fb..778491fe 100644 --- a/src/admin/Bootstrap.DataAccess.MongoDB/Bootstrap.DataAccess.MongoDB.csproj +++ b/src/admin/Bootstrap.DataAccess.MongoDB/Bootstrap.DataAccess.MongoDB.csproj @@ -2,7 +2,7 @@ - + diff --git a/src/admin/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj b/src/admin/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj index b021bec5..5df00e1f 100644 --- a/src/admin/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj +++ b/src/admin/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj @@ -15,11 +15,11 @@ - + - + - + diff --git a/src/admin/Bootstrap.DataAccess/Dict.cs b/src/admin/Bootstrap.DataAccess/Dict.cs index 320f7294..cd4c97ab 100644 --- a/src/admin/Bootstrap.DataAccess/Dict.cs +++ b/src/admin/Bootstrap.DataAccess/Dict.cs @@ -180,25 +180,25 @@ namespace Bootstrap.DataAccess /// 程序异常时长 默认1月 /// /// - public int RetrieveExceptionsLogPeriod() => LgbConvert.ReadValue(DictHelper.RetrieveDicts().FirstOrDefault(d => d.Category == "网站设置" && d.Name == "程序异常保留时长" && d.Define == 0)?.Code, 1); + public int RetrieveExceptionsLogPeriod() => LgbConvert.ReadValue(DictHelper.RetrieveDicts().FirstOrDefault(d => d.Category == "网站设置" && d.Name == "程序异常保留时长" && d.Define == 0)?.Code ?? "1", 1); /// /// 操作日志时长 默认12月 /// /// - public int RetrieveLogsPeriod() => LgbConvert.ReadValue(DictHelper.RetrieveDicts().FirstOrDefault(d => d.Category == "网站设置" && d.Name == "操作日志保留时长" && d.Define == 0)?.Code, 12); + public int RetrieveLogsPeriod() => LgbConvert.ReadValue(DictHelper.RetrieveDicts().FirstOrDefault(d => d.Category == "网站设置" && d.Name == "操作日志保留时长" && d.Define == 0)?.Code ?? "12", 12); /// /// 登录日志时长 默认12月 /// /// - public int RetrieveLoginLogsPeriod() => LgbConvert.ReadValue(DictHelper.RetrieveDicts().FirstOrDefault(d => d.Category == "网站设置" && d.Name == "登录日志保留时长" && d.Define == 0)?.Code, 12); + public int RetrieveLoginLogsPeriod() => LgbConvert.ReadValue(DictHelper.RetrieveDicts().FirstOrDefault(d => d.Category == "网站设置" && d.Name == "登录日志保留时长" && d.Define == 0)?.Code ?? "12", 12); /// /// Cookie保存时长 默认7天 /// /// - public int RetrieveCookieExpiresPeriod() => LgbConvert.ReadValue(DictHelper.RetrieveDicts().FirstOrDefault(d => d.Category == "网站设置" && d.Name == "Cookie保留时长" && d.Define == 0)?.Code, 7); + public int RetrieveCookieExpiresPeriod() => LgbConvert.ReadValue(DictHelper.RetrieveDicts().FirstOrDefault(d => d.Category == "网站设置" && d.Name == "Cookie保留时长" && d.Define == 0)?.Code ?? "7", 7); /// /// 获得 IP地理位置 @@ -229,13 +229,13 @@ namespace Bootstrap.DataAccess /// 获得 访问日志保留时长 默认为1个月 /// /// - public int RetrieveAccessLogPeriod() => LgbConvert.ReadValue(DictHelper.RetrieveDicts().FirstOrDefault(d => d.Category == "网站设置" && d.Name == "访问日志保留时长" && d.Define == 0)?.Code, 1); + public int RetrieveAccessLogPeriod() => LgbConvert.ReadValue(DictHelper.RetrieveDicts().FirstOrDefault(d => d.Category == "网站设置" && d.Name == "访问日志保留时长" && d.Define == 0)?.Code ?? "1", 1); /// /// 获得 是否为演示系统 默认为 false 不是演示系统 /// /// - public bool RetrieveSystemModel() => LgbConvert.ReadValue(DictHelper.RetrieveDicts().FirstOrDefault(d => d.Category == "网站设置" && d.Name == "演示系统" && d.Define == 0)?.Code, "0") == "1"; + public bool RetrieveSystemModel() => LgbConvert.ReadValue(DictHelper.RetrieveDicts().FirstOrDefault(d => d.Category == "网站设置" && d.Name == "演示系统" && d.Define == 0)?.Code ?? "0", "0") == "1"; /// /// 获得 是否为演示系统 默认为 false 不是演示系统 @@ -282,7 +282,7 @@ namespace Bootstrap.DataAccess /// 获得自动锁屏时长 默认 30 秒 /// /// - public int RetrieveAutoLockScreenPeriod() => LgbConvert.ReadValue(DictHelper.RetrieveDicts().FirstOrDefault(d => d.Category == "网站设置" && d.Name == "自动锁屏时长" && d.Define == 0)?.Code, 30); + public int RetrieveAutoLockScreenPeriod() => LgbConvert.ReadValue(DictHelper.RetrieveDicts().FirstOrDefault(d => d.Category == "网站设置" && d.Name == "自动锁屏时长" && d.Define == 0)?.Code ?? "30", 30); /// /// 获得自动锁屏是否开启 默认关闭 diff --git a/src/admin/Bootstrap.DataAccess/Helper/LoginHelper.cs b/src/admin/Bootstrap.DataAccess/Helper/LoginHelper.cs index 2d6303c6..13875f99 100644 --- a/src/admin/Bootstrap.DataAccess/Helper/LoginHelper.cs +++ b/src/admin/Bootstrap.DataAccess/Helper/LoginHelper.cs @@ -6,6 +6,7 @@ using PetaPoco; using System; using System.Collections.Generic; using System.Net; +using System.Threading.Tasks; namespace Bootstrap.DataAccess { @@ -21,7 +22,7 @@ namespace Bootstrap.DataAccess /// 登录用户名 /// 是否登录成功 /// - public static bool Log(this HttpContext context, string userName, bool auth) + public static async Task Log(this HttpContext context, string userName, bool auth) { var ipLocator = context.RequestServices.GetRequiredService(); var ip = context.Connection.RemoteIpAddress?.ToIPv4String() ?? ""; @@ -35,11 +36,11 @@ namespace Bootstrap.DataAccess LoginTime = DateTime.Now, UserAgent = userAgent, Ip = ip, - City = ipLocator.Locate(ip), Browser = $"{agent.Browser?.Name} {agent.Browser?.Version}", OS = $"{agent.OS?.Name} {agent.OS?.Version}", Result = auth ? "登录成功" : "登录失败" }; + loginUser.City = await ipLocator.Locate(ip); return DbContextManager.Create()?.Log(loginUser) ?? false; } diff --git a/src/admin/Bootstrap.DataAccess/Helper/OAuthHelper.cs b/src/admin/Bootstrap.DataAccess/Helper/OAuthHelper.cs index 8b47912e..5df86777 100644 --- a/src/admin/Bootstrap.DataAccess/Helper/OAuthHelper.cs +++ b/src/admin/Bootstrap.DataAccess/Helper/OAuthHelper.cs @@ -49,7 +49,7 @@ namespace Bootstrap.DataAccess SaveUser(user, option.Roles); // 记录登陆日志 - context.HttpContext.Log(user.UserName, true); + await context.HttpContext.Log(user.UserName, true); }; } diff --git a/src/blazor/Bootstrap.Client.Blazor.Shared/Bootstrap.Client.Blazor.Shared.csproj b/src/blazor/Bootstrap.Client.Blazor.Shared/Bootstrap.Client.Blazor.Shared.csproj index 7dc7a820..e4f6f94b 100644 --- a/src/blazor/Bootstrap.Client.Blazor.Shared/Bootstrap.Client.Blazor.Shared.csproj +++ b/src/blazor/Bootstrap.Client.Blazor.Shared/Bootstrap.Client.Blazor.Shared.csproj @@ -5,9 +5,9 @@ - - - + + + diff --git a/src/blazor/Bootstrap.Client.Blazor/Bootstrap.Client.Blazor.csproj b/src/blazor/Bootstrap.Client.Blazor/Bootstrap.Client.Blazor.csproj index ed6c13d4..99aeec79 100644 --- a/src/blazor/Bootstrap.Client.Blazor/Bootstrap.Client.Blazor.csproj +++ b/src/blazor/Bootstrap.Client.Blazor/Bootstrap.Client.Blazor.csproj @@ -5,7 +5,7 @@ - + diff --git a/src/client/Bootstrap.Client.DataAccess.MongoDB/Bootstrap.Client.DataAccess.MongoDB.csproj b/src/client/Bootstrap.Client.DataAccess.MongoDB/Bootstrap.Client.DataAccess.MongoDB.csproj index fdce57b1..8fb752e2 100644 --- a/src/client/Bootstrap.Client.DataAccess.MongoDB/Bootstrap.Client.DataAccess.MongoDB.csproj +++ b/src/client/Bootstrap.Client.DataAccess.MongoDB/Bootstrap.Client.DataAccess.MongoDB.csproj @@ -1,7 +1,7 @@  - + diff --git a/src/client/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj b/src/client/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj index a1454671..616ef9c1 100644 --- a/src/client/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj +++ b/src/client/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj @@ -8,8 +8,8 @@ - - + + diff --git a/src/client/Bootstrap.Client/Bootstrap.Client.csproj b/src/client/Bootstrap.Client/Bootstrap.Client.csproj index c46c7dad..f87b90e4 100644 --- a/src/client/Bootstrap.Client/Bootstrap.Client.csproj +++ b/src/client/Bootstrap.Client/Bootstrap.Client.csproj @@ -5,9 +5,9 @@ - + - + diff --git a/src/client/Bootstrap.Client/Tasks/AppVeyorHttpClient.cs b/src/client/Bootstrap.Client/Tasks/AppVeyorHttpClient.cs index 31f84d74..71366d1f 100644 --- a/src/client/Bootstrap.Client/Tasks/AppVeyorHttpClient.cs +++ b/src/client/Bootstrap.Client/Tasks/AppVeyorHttpClient.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Net; using System.Net.Http; using System.Net.Http.Headers; +using System.Net.Http.Json; using System.Threading.Tasks; namespace Bootstrap.Client.Tasks diff --git a/test/UnitTest/Bootstrap.Admin/Api/InterfaceTest.cs b/test/UnitTest/Bootstrap.Admin/Api/InterfaceTest.cs index 9d7e3f5b..33a65eca 100644 --- a/test/UnitTest/Bootstrap.Admin/Api/InterfaceTest.cs +++ b/test/UnitTest/Bootstrap.Admin/Api/InterfaceTest.cs @@ -1,9 +1,6 @@ using Bootstrap.Security; -using Microsoft.Extensions.DependencyInjection; using System.Collections.Generic; -using System.Net.Http; using System.Net.Http.Json; -using System.Text.Json; using Xunit; namespace Bootstrap.Admin.Api diff --git a/test/UnitTest/UnitTest.csproj b/test/UnitTest/UnitTest.csproj index a75a041b..0e18ec0e 100644 --- a/test/UnitTest/UnitTest.csproj +++ b/test/UnitTest/UnitTest.csproj @@ -17,10 +17,10 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - - + + - +