增加功能:SQLServer 数据库下登录信息记录到数据库中 link #IS7RM

This commit is contained in:
Argo Zhang 2019-03-04 01:27:04 +08:00
parent 9e1941edf1
commit a912362488
6 changed files with 151 additions and 6 deletions

View File

@ -2,9 +2,11 @@
using Bootstrap.DataAccess;
using Longbow;
using Longbow.Configuration;
using Longbow.Web;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
@ -34,13 +36,14 @@ namespace Bootstrap.Admin.Controllers
/// Login the specified userName, password and remember.
/// </summary>
/// <returns>The login.</returns>
/// <param name="onlineUserSvr"></param>
/// <param name="userName">User name.</param>
/// <param name="password">Password.</param>
/// <param name="remember">Remember.</param>
[HttpPost]
public async Task<IActionResult> Login(string userName, string password, string remember)
public async Task<IActionResult> Login([FromServices]IOnlineUsers onlineUserSvr, string userName, string password, string remember)
{
if (UserHelper.Authenticate(userName, password))
if (UserHelper.Authenticate(userName, password, loginUser => CreateLoginUser(onlineUserSvr, HttpContext, loginUser)))
{
var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
identity.AddClaim(new Claim(ClaimTypes.Name, userName));
@ -51,6 +54,21 @@ namespace Bootstrap.Admin.Controllers
return Redirect(originUrl);
}
/// <summary>
///
/// </summary>
/// <param name="onlineUserSvr"></param>
/// <param name="context"></param>
/// <param name="loginUser"></param>
internal static void CreateLoginUser(IOnlineUsers onlineUserSvr, HttpContext context, LoginUser loginUser)
{
var agent = new UserAgent(context.Request.Headers["User-Agent"]);
loginUser.Ip = context.Connection.RemoteIpAddress?.ToString();
loginUser.City = onlineUserSvr.RetrieveLocaleByIp(loginUser.Ip);
loginUser.Browser = $"{agent.Browser.Name} {agent.Browser.Version}";
loginUser.OS = $"{agent.OS.Name} {agent.OS.Version}";
}
/// <summary>
/// Logout this instance.
/// </summary>

View File

@ -19,17 +19,18 @@ namespace Bootstrap.Admin.Controllers.Api
/// <summary>
///
/// </summary>
/// <param name="onlineUserSvr"></param>
/// <param name="value"></param>
/// <returns></returns>
[AllowAnonymous]
[HttpPost]
public string Post([FromBody]JObject value)
public string Post([FromServices]IOnlineUsers onlineUserSvr, [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))
if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password) && UserHelper.Authenticate(userName, password, loginUser => AccountController.CreateLoginUser(onlineUserSvr, HttpContext, loginUser)))
{
token = BootstrapAdminJwtTokenHandler.CreateToken(userName);
}

View File

@ -0,0 +1,17 @@
using Longbow.Data;
namespace Bootstrap.DataAccess
{
/// <summary>
///
/// </summary>
public static class LoginHelper
{
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public static bool Log(LoginUser user) => DbContextManager.Create<LoginUser>().Log(user);
}
}

View File

@ -1,6 +1,7 @@
using Bootstrap.Security;
using Longbow.Cache;
using Longbow.Data;
using System;
using System.Collections.Generic;
namespace Bootstrap.DataAccess
@ -29,7 +30,21 @@ namespace Bootstrap.DataAccess
/// <param name="userName"></param>
/// <param name="password"></param>
/// <returns></returns>
public static bool Authenticate(string userName, string password) => DbContextManager.Create<User>().Authenticate(userName, password);
public static bool Authenticate(string userName, string password, Action<LoginUser> config)
{
var loginUser = new LoginUser()
{
UserName = userName,
LoginTime = DateTime.Now,
Result = "登录失败"
};
config(loginUser);
if (string.IsNullOrEmpty(loginUser.Ip)) loginUser.Ip = System.Net.IPAddress.IPv6Loopback.ToString();
var ret = DbContextManager.Create<User>().Authenticate(userName, password);
if (ret) loginUser.Result = "登录成功";
LoginHelper.Log(loginUser);
return ret;
}
/// <summary>
/// 查询所有的新注册用户

View File

@ -0,0 +1,64 @@
using PetaPoco;
using System;
namespace Bootstrap.DataAccess
{
/// <summary>
///
/// </summary>
[TableName("LoginLogs")]
public class LoginUser
{
/// <summary>
///
/// </summary>
public string Id { get; set; }
/// <summary>
///
/// </summary>
public string UserName { get; set; }
/// <summary>
///
/// </summary>
public DateTime LoginTime { get; set; }
/// <summary>
///
/// </summary>
public string Ip { get; set; }
/// <summary>
///
/// </summary>
public string Browser { get; set; }
/// <summary>
///
/// </summary>
public string OS { get; set; }
/// <summary>
///
/// </summary>
public string City { get; set; }
/// <summary>
///
/// </summary>
public string Result { get; set; }
/// <summary>
///
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public virtual bool Log(LoginUser user)
{
var db = DbManager.Create();
db.Save(user);
return true;
}
}
}

View File

@ -474,3 +474,33 @@ GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'注册时间' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'RejectUsers', @level2type=N'COLUMN',@level2name=N'RegisterTime'
GO
/****** Object: Table [dbo].[LoginLogs] Script Date: 03/03/2019 20:05:42 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[LoginLogs](
[ID] [int] IDENTITY(1,1) NOT NULL,
[UserName] [varchar](50) NOT NULL,
[LoginTime] [datetime] NOT NULL,
[Ip] [varchar](15) NOT NULL,
[OS] [varchar](50) NULL,
[Browser] [varchar](50) NULL,
[City] [nvarchar](50) NULL,
[Result] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_LoginLogs] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO