feat(account): 添加登录退出日志记录和SSO优化
- 在登录页面添加访问日志记录,包括IP地址和跳过SSO参数 - 增加天风SSO启用状态的日志输出 - 添加密码登录禁用配置的检查日志 - 在退出登录时记录用户ID、用户名和IP地址 - 优化退出登录流程,避免禁用密码登录时的循环跳转问题 - 为SSO登录添加重新登录按钮样式和功能 - 统一使用远程IP地址变量避免重复获取 - 增加异常处理的日志记录功能
This commit is contained in:
@@ -34,6 +34,10 @@ namespace YLErp.Web.Controllers
|
||||
|
||||
public ActionResult Login(bool skipSso = false)
|
||||
{
|
||||
var logger = LogFactory.GetLogger<AccountController>();
|
||||
var remoteIp = HttpContext.Connection.RemoteIpAddress?.ToString();
|
||||
logger.Info($"[登录页面] 访问登录页,IP: {remoteIp}, skipSso: {skipSso}");
|
||||
|
||||
var keys = Request.Cookies.Keys.ToArray();
|
||||
|
||||
foreach (var key in keys)
|
||||
@@ -44,18 +48,27 @@ namespace YLErp.Web.Controllers
|
||||
}
|
||||
}
|
||||
|
||||
// 检查天风SSO
|
||||
if (SsoLoginConfig.SSO_Enable && PS.Config.Company == CompanyEnum.天风)
|
||||
{
|
||||
logger.Info($"[登录页面] 天风SSO已启用,跳转到天风登录页");
|
||||
return View("TianFengLogin");
|
||||
}
|
||||
|
||||
if (PS.Config.ErpElement.DisablePasswordLogin)
|
||||
// 检查是否禁用密码登录
|
||||
var disablePasswordLogin = PS.Config.ErpElement.DisablePasswordLogin;
|
||||
var strictAuthorize = PS.Config.ErpElement.StrictAuthorize;
|
||||
logger.Info($"[登录页面] 配置检查 - DisablePasswordLogin: {disablePasswordLogin}, StrictAuthorize: {strictAuthorize}, Company: {PS.Config.Company}");
|
||||
|
||||
if (disablePasswordLogin)
|
||||
{
|
||||
var ssoUrl = YLErp.Web.Models.GeneralSSOConfig.AuthUrl;
|
||||
logger.Info($"[登录页面] 密码登录已禁用,准备跳转到SSO: {ssoUrl}");
|
||||
if (!string.IsNullOrEmpty(ssoUrl))
|
||||
{
|
||||
return Redirect(ssoUrl);
|
||||
}
|
||||
logger.Error($"[登录页面] SSO地址未配置,无法跳转");
|
||||
return Content("SSO登录未配置,请联系管理员");
|
||||
}
|
||||
|
||||
@@ -265,8 +278,12 @@ namespace YLErp.Web.Controllers
|
||||
|
||||
public ActionResult LogOut()
|
||||
{
|
||||
var logger = LogFactory.GetLogger<AccountController>();
|
||||
var userId = User.GetUserId();
|
||||
var loginName = User.GetUserName();
|
||||
var remoteIp = HttpContext.Connection.RemoteIpAddress?.ToString();
|
||||
|
||||
logger.Info($"[退出登录] 用户请求退出 - UserId: {userId}, LoginName: {loginName}, IP: {remoteIp}");
|
||||
|
||||
if (userId > 0)
|
||||
{
|
||||
@@ -283,7 +300,7 @@ namespace YLErp.Web.Controllers
|
||||
}
|
||||
db.SystemUserLoginfo.Add(new SystemUserLoginfo
|
||||
{
|
||||
IPAddress = HttpContext.Connection.RemoteIpAddress?.ToString(),
|
||||
IPAddress = remoteIp,
|
||||
LogTime = DateTime.Now,
|
||||
LogType = "登出",
|
||||
UserId = userId,
|
||||
@@ -291,15 +308,17 @@ namespace YLErp.Web.Controllers
|
||||
MACAddress = string.Empty
|
||||
});
|
||||
db.SaveChanges();
|
||||
logger.Info($"[退出登录] 用户退出记录已保存 - UserId: {userId}, LoginName: {loginName}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogFactory.GetLogger("用户登出").Error(ex);
|
||||
logger.Error($"[退出登录] 保存退出记录失败 - UserId: {userId}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
HttpContext.SignOutAsync(IdentityConstants.ApplicationScheme);
|
||||
logger.Info($"[退出登录] SignOutAsync 已调用");
|
||||
|
||||
if (SsoLoginConfig.SSO_Enable)
|
||||
{
|
||||
@@ -316,6 +335,17 @@ namespace YLErp.Web.Controllers
|
||||
}
|
||||
}
|
||||
|
||||
// 如果禁用了密码登录,显示退出页面(避免循环跳转到SSO)
|
||||
var disablePasswordLogin = PS.Config.ErpElement.DisablePasswordLogin;
|
||||
logger.Info($"[退出登录] 跳转判断 - DisablePasswordLogin: {disablePasswordLogin}, SsoLoginConfig.SSO_Enable: {SsoLoginConfig.SSO_Enable}");
|
||||
|
||||
if (disablePasswordLogin)
|
||||
{
|
||||
logger.Info($"[退出登录] 密码登录已禁用,返回LogOut视图");
|
||||
return View("LogOut");
|
||||
}
|
||||
|
||||
logger.Info($"[退出登录] 跳转到登录页");
|
||||
return RedirectToAction("Login");
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
@{
|
||||
Layout = null;
|
||||
ViewBag.Title = "登出";
|
||||
var disablePasswordLogin = PS.Config?.ErpElement?.DisablePasswordLogin ?? false;
|
||||
}
|
||||
|
||||
<!DOCTYPE html>
|
||||
@@ -10,11 +11,31 @@
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>@GlobalConfig.ProjectTitle</title>
|
||||
<style>
|
||||
.sso-login-btn {
|
||||
display: inline-block;
|
||||
margin-top: 15px;
|
||||
padding: 8px 20px;
|
||||
background: #1890ff;
|
||||
color: white;
|
||||
text-decoration: none;
|
||||
border-radius: 4px;
|
||||
font-size: 14px;
|
||||
}
|
||||
.sso-login-btn:hover {
|
||||
background: #40a9ff;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body style="height:100%;max-height:100%;overflow:hidden;box-sizing:border-box;">
|
||||
<div id="mainCont">
|
||||
<div style="width:500px;height:50px;border:1px dashed #bbb;padding:15px;margin:0 auto;box-sizing:border-box">
|
||||
<div style="width:500px;border:1px dashed #bbb;padding:15px;margin:0 auto;box-sizing:border-box;text-align:center;">
|
||||
已经登出系统,为安全性考虑,浏览器窗口将在 <a href="#" id="aSecond">5秒</a> 后自动关闭!
|
||||
@if (disablePasswordLogin)
|
||||
{
|
||||
<br /><br />
|
||||
<a href="@YLErp.Web.Models.GeneralSSOConfig.AuthUrl" class="sso-login-btn">重新登录</a>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user