refactor: 消除二义性错误

This commit is contained in:
Argo-Tianyi 2021-10-21 12:54:56 +08:00
parent 42d56b9dbc
commit edac104fbc
19 changed files with 48 additions and 43 deletions

View File

@ -9,12 +9,12 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="BootstrapBlazor" Version="5.6.5" /> <PackageReference Include="BootstrapBlazor" Version="5.16.0" />
<PackageReference Include="Exceptionless.AspNetCore" Version="4.6.2" /> <PackageReference Include="Exceptionless.AspNetCore" Version="4.6.2" />
<PackageReference Include="Longbow.Logging" Version="5.2.0" /> <PackageReference Include="Longbow.Logging" Version="5.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.0.0" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.0.0" />
<PackageReference Include="Sentry.AspNetCore" Version="3.8.3" /> <PackageReference Include="Sentry.AspNetCore" Version="3.10.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.1.5" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -110,7 +110,7 @@ namespace Bootstrap.Admin.Controllers
if (string.IsNullOrEmpty(phone) || string.IsNullOrEmpty(code)) return RedirectLogin(); if (string.IsNullOrEmpty(phone) || string.IsNullOrEmpty(code)) return RedirectLogin();
var auth = provider.Validate(phone, code); var auth = provider.Validate(phone, code);
HttpContext.Log(phone, auth); await HttpContext.Log(phone, auth);
if (auth) if (auth)
{ {
var user = UserHelper.Retrieves().FirstOrDefault(u => u.UserName == phone); 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(); if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password)) return RedirectLogin();
var auth = UserHelper.Authenticate(userName, password); 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 }); return auth ? await SignInAsync(userName, remember == "true") : LoginView("", new LoginModel() { AuthFailed = true });
} }

View File

@ -25,7 +25,7 @@ namespace Bootstrap.Admin.Controllers.Api
/// <returns></returns> /// <returns></returns>
[Authorize] [Authorize]
[HttpGet] [HttpGet]
public QueryData<LoginUser> Get([FromQuery]QueryLoginOption value) => value.RetrieveData(); public QueryData<LoginUser> Get([FromQuery] QueryLoginOption value) => value.RetrieveData();
/// <summary> /// <summary>
/// JWT 登陆认证接口 /// JWT 登陆认证接口
@ -34,9 +34,9 @@ namespace Bootstrap.Admin.Controllers.Api
/// <param name="user"></param> /// <param name="user"></param>
/// <returns></returns> /// <returns></returns>
[HttpPost] [HttpPost]
public string? Post([FromServices]IConfiguration config, [FromBody]User user) public async Task<string?> Post([FromServices] IConfiguration config, [FromBody] User user)
{ {
var token = string.Empty; string? token = null;
string userName = user.UserName; string userName = user.UserName;
string password = user.Password; string password = user.Password;
if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password) && UserHelper.Authenticate(userName, 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; op.SecurityKey = tokenOption.SecurityKey;
}); });
} }
HttpContext.Log(userName, !string.IsNullOrEmpty(token)); await HttpContext.Log(userName, !string.IsNullOrEmpty(token));
return token; return token;
} }
@ -61,7 +61,7 @@ namespace Bootstrap.Admin.Controllers.Api
/// <param name="phone"></param> /// <param name="phone"></param>
/// <returns></returns> /// <returns></returns>
[HttpPut] [HttpPut]
public async Task<SMSResult> Put([FromServices]ISMSProvider provider, [FromQuery]string phone) => string.IsNullOrEmpty(phone) ? new SMSResult() { Result = false, Msg = "手机号不可为空" } : await provider.SendCodeAsync(phone); public async Task<SMSResult> Put([FromServices] ISMSProvider provider, [FromQuery] string phone) => string.IsNullOrEmpty(phone) ? new SMSResult() { Result = false, Msg = "手机号不可为空" } : await provider.SendCodeAsync(phone);
/// <summary> /// <summary>
/// 跨域握手协议 /// 跨域握手协议

View File

@ -5,6 +5,7 @@ using Longbow.Web.Mvc;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System.Net; using System.Net;
using System.Threading.Tasks;
namespace Bootstrap.Admin.Controllers.Api namespace Bootstrap.Admin.Controllers.Api
{ {
@ -22,7 +23,7 @@ namespace Bootstrap.Admin.Controllers.Api
/// <param name="value"></param> /// <param name="value"></param>
/// <returns></returns> /// <returns></returns>
[HttpGet] [HttpGet]
public QueryData<Log> Get([FromQuery]QueryLogOption value) public QueryData<Log> Get([FromQuery] QueryLogOption value)
{ {
return value.RetrieveData(); return value.RetrieveData();
} }
@ -34,14 +35,14 @@ namespace Bootstrap.Admin.Controllers.Api
/// <param name="value"></param> /// <param name="value"></param>
/// <returns></returns> /// <returns></returns>
[HttpPost] [HttpPost]
public bool Post([FromServices]IIPLocatorProvider ipLocator, [FromBody]Log value) public async Task<bool> Post([FromServices] IIPLocatorProvider ipLocator, [FromBody] Log value)
{ {
value.UserAgent = Request.Headers["User-Agent"]; value.UserAgent = Request.Headers["User-Agent"];
var agent = new UserAgent(value.UserAgent); var agent = new UserAgent(value.UserAgent);
value.Ip = HttpContext.Connection.RemoteIpAddress?.ToIPv4String() ?? ""; value.Ip = HttpContext.Connection.RemoteIpAddress?.ToIPv4String() ?? "";
value.Browser = $"{agent.Browser?.Name} {agent.Browser?.Version}"; value.Browser = $"{agent.Browser?.Name} {agent.Browser?.Version}";
value.OS = $"{agent.OS?.Name} {agent.OS?.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; value.UserName = User.Identity?.Name ?? string.Empty;
return LogHelper.Save(value); return LogHelper.Save(value);
} }

View File

@ -4,6 +4,7 @@ using Microsoft.Extensions.Logging.Configuration;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using System; using System;
using System.Net.Http; using System.Net.Http;
using System.Net.Http.Json;
namespace Microsoft.Extensions.DependencyInjection namespace Microsoft.Extensions.DependencyInjection
{ {

View File

@ -44,7 +44,11 @@ namespace Microsoft.AspNetCore.Http
FirstAccessTime = DateTime.Now, FirstAccessTime = DateTime.Now,
Referer = context.Request.Headers["Referer"] 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<OnlineUser>(v, 1000 * 60, __ => onlineUserSvr.TryRemove(key, out _)), null); return proxy(new AutoExpireCacheEntry<OnlineUser>(v, 1000 * 60, __ => onlineUserSvr.TryRemove(key, out _)), null);
}, (key, v) => proxy(v, () => v.Reset())); }, (key, v) => proxy(v, () => v.Reset()));
} }

View File

@ -2,7 +2,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Bootstrap.Security" Version="5.2.0" /> <PackageReference Include="Bootstrap.Security" Version="5.2.0" />
<PackageReference Include="MongoDB.Driver" Version="2.13.1" /> <PackageReference Include="MongoDB.Driver" Version="2.13.2" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -15,11 +15,11 @@
<PackageReference Include="Longbow.Security.Cryptography" Version="5.2.0" /> <PackageReference Include="Longbow.Security.Cryptography" Version="5.2.0" />
<PackageReference Include="Longbow.Tasks" Version="5.1.0" /> <PackageReference Include="Longbow.Tasks" Version="5.1.0" />
<PackageReference Include="Longbow.TencentAuth" Version="5.2.0" /> <PackageReference Include="Longbow.TencentAuth" Version="5.2.0" />
<PackageReference Include="Longbow.Web" Version="5.2.0" /> <PackageReference Include="Longbow.Web" Version="5.2.1" />
<PackageReference Include="Longbow.WeChatAuth" Version="5.2.0" /> <PackageReference Include="Longbow.WeChatAuth" Version="5.2.0" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="5.0.9" /> <PackageReference Include="Microsoft.Data.Sqlite" Version="5.0.11" />
<PackageReference Include="PetaPoco.Extensions" Version="5.2.0" /> <PackageReference Include="PetaPoco.Extensions" Version="5.2.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.2" /> <PackageReference Include="System.Data.SqlClient" Version="4.8.3" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -180,25 +180,25 @@ namespace Bootstrap.DataAccess
/// 程序异常时长 默认1月 /// 程序异常时长 默认1月
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
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);
/// <summary> /// <summary>
/// 操作日志时长 默认12月 /// 操作日志时长 默认12月
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
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);
/// <summary> /// <summary>
/// 登录日志时长 默认12月 /// 登录日志时长 默认12月
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
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);
/// <summary> /// <summary>
/// Cookie保存时长 默认7天 /// Cookie保存时长 默认7天
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
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);
/// <summary> /// <summary>
/// 获得 IP地理位置 /// 获得 IP地理位置
@ -229,13 +229,13 @@ namespace Bootstrap.DataAccess
/// 获得 访问日志保留时长 默认为1个月 /// 获得 访问日志保留时长 默认为1个月
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
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);
/// <summary> /// <summary>
/// 获得 是否为演示系统 默认为 false 不是演示系统 /// 获得 是否为演示系统 默认为 false 不是演示系统
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
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";
/// <summary> /// <summary>
/// 获得 是否为演示系统 默认为 false 不是演示系统 /// 获得 是否为演示系统 默认为 false 不是演示系统
@ -282,7 +282,7 @@ namespace Bootstrap.DataAccess
/// 获得自动锁屏时长 默认 30 秒 /// 获得自动锁屏时长 默认 30 秒
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
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);
/// <summary> /// <summary>
/// 获得自动锁屏是否开启 默认关闭 /// 获得自动锁屏是否开启 默认关闭

View File

@ -6,6 +6,7 @@ using PetaPoco;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net; using System.Net;
using System.Threading.Tasks;
namespace Bootstrap.DataAccess namespace Bootstrap.DataAccess
{ {
@ -21,7 +22,7 @@ namespace Bootstrap.DataAccess
/// <param name="userName">登录用户名</param> /// <param name="userName">登录用户名</param>
/// <param name="auth">是否登录成功</param> /// <param name="auth">是否登录成功</param>
/// <returns></returns> /// <returns></returns>
public static bool Log(this HttpContext context, string userName, bool auth) public static async Task<bool> Log(this HttpContext context, string userName, bool auth)
{ {
var ipLocator = context.RequestServices.GetRequiredService<IIPLocatorProvider>(); var ipLocator = context.RequestServices.GetRequiredService<IIPLocatorProvider>();
var ip = context.Connection.RemoteIpAddress?.ToIPv4String() ?? ""; var ip = context.Connection.RemoteIpAddress?.ToIPv4String() ?? "";
@ -35,11 +36,11 @@ namespace Bootstrap.DataAccess
LoginTime = DateTime.Now, LoginTime = DateTime.Now,
UserAgent = userAgent, UserAgent = userAgent,
Ip = ip, Ip = ip,
City = ipLocator.Locate(ip),
Browser = $"{agent.Browser?.Name} {agent.Browser?.Version}", Browser = $"{agent.Browser?.Name} {agent.Browser?.Version}",
OS = $"{agent.OS?.Name} {agent.OS?.Version}", OS = $"{agent.OS?.Name} {agent.OS?.Version}",
Result = auth ? "登录成功" : "登录失败" Result = auth ? "登录成功" : "登录失败"
}; };
loginUser.City = await ipLocator.Locate(ip);
return DbContextManager.Create<LoginUser>()?.Log(loginUser) ?? false; return DbContextManager.Create<LoginUser>()?.Log(loginUser) ?? false;
} }

View File

@ -49,7 +49,7 @@ namespace Bootstrap.DataAccess
SaveUser(user, option.Roles); SaveUser(user, option.Roles);
// 记录登陆日志 // 记录登陆日志
context.HttpContext.Log(user.UserName, true); await context.HttpContext.Log(user.UserName, true);
}; };
} }

View File

@ -5,9 +5,9 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="BootstrapBlazor" Version="5.6.5" /> <PackageReference Include="BootstrapBlazor" Version="5.16.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="5.0.9" /> <PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="5.0.11" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="5.0.9" /> <PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="5.0.11" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -5,7 +5,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Data.Sqlite" Version="5.0.9" /> <PackageReference Include="Microsoft.Data.Sqlite" Version="5.0.11" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<ItemGroup> <ItemGroup>
<PackageReference Include="MongoDB.Driver" Version="2.13.1" /> <PackageReference Include="MongoDB.Driver" Version="2.13.2" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -8,8 +8,8 @@
<PackageReference Include="Longbow.ComponentModel" Version="5.2.0" /> <PackageReference Include="Longbow.ComponentModel" Version="5.2.0" />
<PackageReference Include="Longbow.Data" Version="5.2.0" /> <PackageReference Include="Longbow.Data" Version="5.2.0" />
<PackageReference Include="Longbow.Security.Cryptography" Version="5.2.0" /> <PackageReference Include="Longbow.Security.Cryptography" Version="5.2.0" />
<PackageReference Include="Longbow.Web" Version="5.2.0" /> <PackageReference Include="Longbow.Web" Version="5.2.1" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.2" /> <PackageReference Include="System.Data.SqlClient" Version="4.8.3" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -5,9 +5,9 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="BootstrapBlazor" Version="5.6.5" /> <PackageReference Include="BootstrapBlazor" Version="5.16.0" />
<PackageReference Include="Longbow.Logging" Version="5.2.0" /> <PackageReference Include="Longbow.Logging" Version="5.2.0" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="5.0.9" /> <PackageReference Include="Microsoft.Data.Sqlite" Version="5.0.11" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Bootstrap.Client.Tasks namespace Bootstrap.Client.Tasks

View File

@ -1,9 +1,6 @@
using Bootstrap.Security; using Bootstrap.Security;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Json; using System.Net.Http.Json;
using System.Text.Json;
using Xunit; using Xunit;
namespace Bootstrap.Admin.Api namespace Bootstrap.Admin.Api

View File

@ -17,10 +17,10 @@
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="5.0.9" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="5.0.11" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="5.0.9" /> <PackageReference Include="Microsoft.Data.Sqlite" Version="5.0.11" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="MySql.Data" Version="8.0.26" /> <PackageReference Include="MySql.Data" Version="8.0.27" />
<PackageReference Include="System.Net.Http.Json" Version="5.0.0" /> <PackageReference Include="System.Net.Http.Json" Version="5.0.0" />
<PackageReference Include="xunit" Version="2.4.1" /> <PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3"> <PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">