From b391d109cd38908ce0f8f79b94fba7cf0faf984c Mon Sep 17 00:00:00 2001 From: zhangpeihang <948869991@qq.com> Date: Fri, 14 Jan 2022 23:36:41 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=AE=B0=E5=BD=95=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/AccountController.cs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/blazor/admin/BootstrapAdmin.Web/Controllers/AccountController.cs b/src/blazor/admin/BootstrapAdmin.Web/Controllers/AccountController.cs index ced22ae6..4cae6aaa 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/Controllers/AccountController.cs +++ b/src/blazor/admin/BootstrapAdmin.Web/Controllers/AccountController.cs @@ -1,11 +1,13 @@ using BootstrapAdmin.Web.Core; using BootstrapAdmin.Web.Services; using BootstrapAdmin.Web.Services.SMS; +using Longbow.Web; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.WebUtilities; +using System.Net; using System.Security.Claims; namespace BootstrapAdmin.Web.Controllers @@ -50,7 +52,12 @@ namespace BootstrapAdmin.Web.Controllers // Cookie 持久化 period = dictService.GetCookieExpiresPeriod(); } - await loginService.Log(userName, auth); + var ipLocator = HttpContext.RequestServices.GetRequiredService(); + var ip = HttpContext.Connection.RemoteIpAddress?.ToIPv4String() ?? ""; + var userAgent = HttpContext.Request.Headers["User-Agent"]; + var agent = new UserAgent(userAgent); + var address = await ipLocator.Locate(ip); + await loginService.Log(userName, DateTime.Now, ip, address, $"{agent.Browser.Name} {agent.Browser.Version}", userAgent, $"{agent.OS.Name} {agent.OS.Version}", auth); return auth ? await SignInAsync(userName, returnUrl ?? GetAppHomeUrl(dictService, appId), persistent, period) : RedirectLogin(returnUrl); } @@ -144,13 +151,19 @@ namespace BootstrapAdmin.Web.Controllers // Cookie 持久化 period = dictService.GetCookieExpiresPeriod(); } - await loginService.Log(phone, auth); + var ipLocator = HttpContext.RequestServices.GetRequiredService(); + var ip = HttpContext.Connection.RemoteIpAddress?.ToIPv4String() ?? ""; + var userAgent = HttpContext.Request.Headers["User-Agent"]; + var agent = new UserAgent(userAgent); + var address = await ipLocator.Locate(ip); + await loginService.Log(phone, DateTime.Now, ip, address, $"{agent.Browser.Name} {agent.Browser.Version}", userAgent, $"{agent.OS.Name} {agent.OS.Version}", auth); if (auth) { userService.TryCreateUserByPhone(phone, code, context.AppId, provider.Options.Roles); } return auth ? await SignInAsync(phone, returnUrl ?? GetAppHomeUrl(dictService, appId), persistent, period, MobileSchema) : RedirectLogin(returnUrl); } + #endregion /////