Files
OpenAuth.Net/OpenAuth.App/SSO/LoginParse.cs
2026-07-06 21:14:28 +08:00

130 lines
4.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* 登录解析
* 处理登录逻辑,验证客户段提交的账号密码,保存登录信息
*/
using System;
using Infrastructure;
using Infrastructure.Cache;
using Infrastructure.Helpers;
using OpenAuth.Repository;
using OpenAuth.Repository.Domain;
using OpenAuth.Repository.Interface;
using SqlSugar;
using Infrastructure.Extensions.AutofacManager;
using Infrastructure.Utilities;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
namespace OpenAuth.App.SSO
{
public class LoginParse
{
//这个地方使用IRepository<User> 而不使用UserManagerApp是防止循环依赖
protected ISqlSugarClient SugarClient;
private ICacheContext _cacheContext;
private AppManager _appInfoService;
private IOptions<AppSetting> _appConfiguration;
public LoginParse( AppManager infoService, ICacheContext cacheContext, ISqlSugarClient client, IOptions<AppSetting> appConfiguration)
{
_appInfoService = infoService;
_cacheContext = cacheContext;
_appConfiguration = appConfiguration;
string tenantId = Define.DEFAULT_TENANT_ID;
var httpContextAccessor = AutofacContainerModule.GetService<IHttpContextAccessor>();
if (httpContextAccessor != null)
{
tenantId = httpContextAccessor.GetTenantId();
}
if(tenantId != Define.DEFAULT_TENANT_ID) //如果不是默认租户则使用租户id获取连接
{
client = client.AsTenant().GetConnection(tenantId);
}
SugarClient = client;
}
public LoginResult Do(PassportLoginRequest model)
{
var result = new LoginResult();
try
{
model.Trim();
//todo:如果需要判定应用,可以取消该注释
// var appInfo = _appInfoService.GetByAppKey(model.AppKey);
// if (appInfo == null)
// {
// throw new Exception("应用不存在");
// }
//获取用户信息
SysUser sysUserInfo = null;
if (model.Account == Define.SYSTEM_USERNAME)
{
sysUserInfo = new SysUser
{
Id = Guid.Empty.ToString(),
Account = Define.SYSTEM_USERNAME,
Name ="超级管理员",
Password = Define.SYSTEM_USERPWD
};
}
else
{
sysUserInfo = SugarClient.Queryable<SysUser>().First(u =>u.Account == model.Account);
}
if (sysUserInfo == null)
{
throw new Exception("用户不存在");
}
if (sysUserInfo.Password != model.Password)
{
throw new Exception("密码错误");
}
if (sysUserInfo.Status != 0)
{
throw new Exception("账号状态异常,可能已停用");
}
// 生成唯一的会话ID作为缓存Key
var sessionId = Guid.NewGuid().ToString("N");
var expireDays = _appConfiguration.Value.JwtExpireDays;
var currentSession = new UserAuthSession
{
Account = model.Account,
Name = sysUserInfo.Name,
Token = sessionId,
AppKey = model.AppKey,
CreateTime = TimeHelper.Now
};
//创建Session缓存用于登出时失效判定
_cacheContext.Set(sessionId, currentSession, TimeHelper.Now.AddDays(expireDays));
// 生成JWT Token
var jwtToken = JwtTokenHelper.GenerateToken(
model.Account,
sysUserInfo.Name,
model.AppKey,
sessionId,
_appConfiguration.Value.JwtSecret,
expireDays);
result.Code = 200;
result.Token = jwtToken;
}
catch (Exception ex)
{
result.Code = 500;
result.Message = ex.Message;
}
return result;
}
}
}