diff --git a/src/admin/Bootstrap.Admin/Controllers/AccountController.cs b/src/admin/Bootstrap.Admin/Controllers/AccountController.cs
index b8f441a6..af2ce010 100644
--- a/src/admin/Bootstrap.Admin/Controllers/AccountController.cs
+++ b/src/admin/Bootstrap.Admin/Controllers/AccountController.cs
@@ -2,7 +2,6 @@
using Bootstrap.DataAccess;
using Longbow.GiteeAuth;
using Longbow.GitHubAuth;
-using Longbow.Security.Cryptography;
using Longbow.Web;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
@@ -11,11 +10,9 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System;
-using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security.Claims;
-using System.Security.Principal;
using System.Threading.Tasks;
namespace Bootstrap.Admin.Controllers
@@ -47,13 +44,16 @@ namespace Bootstrap.Admin.Controllers
///
/// 系统锁屏界面
///
- ///
///
///
///
[HttpPost]
[IgnoreAntiforgeryToken]
- public Task Lock([FromServices]IIPLocatorProvider ipLocator, string userName, string password) => Login(ipLocator, userName, password, string.Empty);
+ public Task Lock(string userName, string password)
+ {
+ // 根据不同的登陆方式
+ return Login(userName, password, string.Empty);
+ }
///
/// 系统登录方法
@@ -79,10 +79,12 @@ namespace Bootstrap.Admin.Controllers
///
///
[HttpPost()]
- public async Task Mobile([FromServices]IIPLocatorProvider ipLocator, [FromServices]IConfiguration configuration, [FromQuery]string phone, [FromQuery]string code)
+ public async Task Mobile([FromServices]IConfiguration configuration, [FromQuery]string phone, [FromQuery]string code)
{
var option = configuration.GetSection(nameof(SMSOptions)).Get();
- if (SMSHelper.Validate(phone, code, option.MD5Key))
+ var auth = SMSHelper.Validate(phone, code, option.MD5Key);
+ HttpContext.Log(phone, auth);
+ if (auth)
{
var user = UserHelper.Retrieves().FirstOrDefault(u => u.UserName == phone);
if (user == null)
@@ -104,25 +106,24 @@ namespace Bootstrap.Admin.Controllers
var roles = RoleHelper.Retrieves().Where(r => option.Roles.Any(rl => rl.Equals(r.RoleName, StringComparison.OrdinalIgnoreCase))).Select(r => r.Id);
RoleHelper.SaveByUserId(user.Id, roles);
}
- else
- {
- // update password
- UserHelper.Update(user.Id, code, user.DisplayName);
- }
}
- return await Login(ipLocator, phone, code, "true");
+ return auth ? await SignInAsync(phone, true) : View("Login", new LoginModel() { AuthFailed = true });
}
///
/// Login the specified userName, password and remember.
///
/// The login.
- ///
/// User name.
/// Password.
/// Remember.
[HttpPost]
- public async Task Login([FromServices]IIPLocatorProvider ipLocator, string userName, string password, string remember) => UserHelper.Authenticate(userName, password, loginUser => CreateLoginUser(ipLocator, HttpContext, loginUser)) ? await SignInAsync(userName, remember == "true") : View("Login", new LoginModel() { AuthFailed = true });
+ public async Task Login(string userName, string password, string remember)
+ {
+ var auth = UserHelper.Authenticate(userName, password);
+ HttpContext.Log(userName, auth);
+ return auth ? await SignInAsync(userName, remember == "true") : View("Login", new LoginModel() { AuthFailed = true });
+ }
private async Task SignInAsync(string userName, bool persistent)
{
diff --git a/src/admin/Bootstrap.Admin/Controllers/Api/LoginController.cs b/src/admin/Bootstrap.Admin/Controllers/Api/LoginController.cs
index 979a3435..59562a0d 100644
--- a/src/admin/Bootstrap.Admin/Controllers/Api/LoginController.cs
+++ b/src/admin/Bootstrap.Admin/Controllers/Api/LoginController.cs
@@ -1,7 +1,6 @@
using Bootstrap.Admin.Query;
using Bootstrap.DataAccess;
using Bootstrap.Security;
-using Longbow.Web;
using Longbow.Web.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
@@ -30,21 +29,21 @@ namespace Bootstrap.Admin.Controllers.Api
///
/// JWT 登陆认证接口
///
- ///
///
///
[AllowAnonymous]
[HttpPost]
- public string Post([FromServices]IIPLocatorProvider ipLocator, [FromBody]JObject value)
+ public string Post([FromBody]JObject value)
{
string token = null;
dynamic user = value;
string userName = user.userName;
string password = user.password;
- if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password) && UserHelper.Authenticate(userName, password, loginUser => AccountController.CreateLoginUser(ipLocator, HttpContext, loginUser)))
+ if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password) && UserHelper.Authenticate(userName, password))
{
token = BootstrapAdminJwtTokenHandler.CreateToken(userName);
}
+ HttpContext.Log(userName, token != null);
return token;
}
diff --git a/src/admin/Bootstrap.Admin/Startup.cs b/src/admin/Bootstrap.Admin/Startup.cs
index e5432b16..ce3f2689 100644
--- a/src/admin/Bootstrap.Admin/Startup.cs
+++ b/src/admin/Bootstrap.Admin/Startup.cs
@@ -1,5 +1,4 @@
using Bootstrap.DataAccess;
-using Longbow.GiteeAuth;
using Longbow.Web;
using Longbow.Web.SignalR;
using Microsoft.AspNetCore.Builder;
diff --git a/src/admin/Bootstrap.DataAccess/Helper/LoginHelper.cs b/src/admin/Bootstrap.DataAccess/Helper/LoginHelper.cs
index 0a86570c..b1035bac 100644
--- a/src/admin/Bootstrap.DataAccess/Helper/LoginHelper.cs
+++ b/src/admin/Bootstrap.DataAccess/Helper/LoginHelper.cs
@@ -1,7 +1,11 @@
-using Longbow.Web.Mvc;
+using Longbow.Web;
+using Longbow.Web.Mvc;
+using Microsoft.AspNetCore.Http;
+using Microsoft.Extensions.DependencyInjection;
using PetaPoco;
using System;
using System.Collections.Generic;
+using System.Net;
namespace Bootstrap.DataAccess
{
@@ -13,12 +17,28 @@ namespace Bootstrap.DataAccess
///
/// 记录登陆日志方法
///
- ///
+ ///
///
- public static bool Log(LoginUser user)
+ public static bool Log(this HttpContext context, string userName, bool auth)
{
- if (string.IsNullOrEmpty(user.UserName)) user.UserName = user.Ip;
- return DbContextManager.Create().Log(user);
+ var ipLocator = context.RequestServices.GetRequiredService();
+ var ip = context.Connection.RemoteIpAddress.ToIPv4String();
+ var userAgent = context.Request.Headers["User-Agent"];
+ var agent = new UserAgent(userAgent);
+
+ if (string.IsNullOrEmpty(userName)) userName = ip;
+ var loginUser = new LoginUser
+ {
+ UserName = userName,
+ 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 ? "登陆成功" : "登录失败"
+ };
+ return DbContextManager.Create().Log(loginUser);
}
///
diff --git a/src/admin/Bootstrap.DataAccess/Helper/OAuthHelper.cs b/src/admin/Bootstrap.DataAccess/Helper/OAuthHelper.cs
index 072565be..f6b896c2 100644
--- a/src/admin/Bootstrap.DataAccess/Helper/OAuthHelper.cs
+++ b/src/admin/Bootstrap.DataAccess/Helper/OAuthHelper.cs
@@ -47,6 +47,9 @@ namespace Bootstrap.DataAccess
requestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
await context.Backchannel.SendAsync(requestMessage, context.HttpContext.RequestAborted);
}
+
+ // 记录登陆日志
+ context.HttpContext.Log(user.Name, true);
};
}
diff --git a/src/admin/Bootstrap.DataAccess/Helper/UserHelper.cs b/src/admin/Bootstrap.DataAccess/Helper/UserHelper.cs
index bdf783ef..a5360faa 100644
--- a/src/admin/Bootstrap.DataAccess/Helper/UserHelper.cs
+++ b/src/admin/Bootstrap.DataAccess/Helper/UserHelper.cs
@@ -58,23 +58,8 @@ namespace Bootstrap.DataAccess
///
///
///
- ///
/// 返回真表示认证通过
- public static bool Authenticate(string userName, string password, Action configure)
- {
- if (!UserChecker(new User { UserName = userName, Password = password })) return false;
- var loginUser = new LoginUser
- {
- UserName = userName,
- LoginTime = DateTime.Now,
- Result = "登录失败"
- };
- configure(loginUser);
- var ret = string.IsNullOrEmpty(userName) ? false : DbContextManager.Create().Authenticate(userName, password);
- if (ret) loginUser.Result = "登录成功";
- LoginHelper.Log(loginUser);
- return ret;
- }
+ public static bool Authenticate(string userName, string password) => DbContextManager.Create().Authenticate(userName, password);
///
/// 查询所有的新注册用户