81 lines
3.1 KiB
C#
81 lines
3.1 KiB
C#
using Crm.Core.Identity.Domain.Config;
|
|
using Crm.Core.Identity.Domain.Dto;
|
|
using Crm.Core.Identity.Domain.Impl;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Web;
|
|
|
|
namespace Crm.Core.Identity.Domain
|
|
{
|
|
internal class IdentityDomain : IIdentityDomain
|
|
{
|
|
private readonly IBaseRepository<WeworkDbContext> _weworkRepository;
|
|
private readonly IRedisManager _redisManager;
|
|
private readonly SystemConfig _systemConfig;
|
|
private readonly IConfiguration _configuration;
|
|
private readonly IHttpContextAccessor _contextAccessor;
|
|
|
|
public IdentityDomain(IBaseRepository<WeworkDbContext> weworkRepository,
|
|
IRedisManager redisManager,
|
|
IConfiguration configuration,
|
|
IHttpContextAccessor contextAccessor)
|
|
{
|
|
_weworkRepository = weworkRepository;
|
|
_redisManager = redisManager;
|
|
_systemConfig = configuration.GetSection("SystemConfig").Get<SystemConfig>();
|
|
_contextAccessor = contextAccessor;
|
|
}
|
|
|
|
public async Task<EncryptResultDto> Encrypt(EncryptDto encryptDto)
|
|
{
|
|
var key = _systemConfig.EncryptKey;
|
|
var vi = _systemConfig.Vi;
|
|
var dueMinutes = _systemConfig.DueMinutes;
|
|
var request = _contextAccessor?.HttpContext?.Request;
|
|
var ip = IPHelper.GetIP(request);
|
|
var decryptString = AesUtil.EncryptByAES(encryptDto.Content, key, vi);
|
|
var now = DateTime.Now.AddMinutes(dueMinutes).Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds.ToString("F0");
|
|
var duetime = AesUtil.EncryptByAES(now, key, vi);
|
|
// todo记录逻辑
|
|
|
|
Log.Information($"{ip}");
|
|
await Task.Delay(1);
|
|
var result = new EncryptResultDto
|
|
{
|
|
Duetime = duetime,
|
|
Content = decryptString,
|
|
};
|
|
return result;
|
|
|
|
}
|
|
|
|
public async Task<string> Decrypt(DecryptDto decryptDto)
|
|
{
|
|
var key = _systemConfig.EncryptKey;
|
|
var vi = _systemConfig.Vi;
|
|
var request = _contextAccessor?.HttpContext?.Request;
|
|
var ip = IPHelper.GetIP(request);
|
|
//decryptDto.Content = HttpUtility.UrlDecode(decryptDto.Content);
|
|
//decryptDto.Duetime = HttpUtility.UrlDecode(decryptDto.Duetime);
|
|
var decryptString = AesUtil.DecryptByAES(decryptDto.Content, key, vi);
|
|
|
|
var config = await _weworkRepository.GetRepository<BasConfig>().Query().FirstOrDefaultAsync(x => x.Code == "IdentityWhiteList");
|
|
var whiteList = JsonHelper.FromJson<List<string>>(config?.Value ?? "[]");
|
|
if (!whiteList.Contains(ip))
|
|
{
|
|
var duetime = AesUtil.DecryptByAES(decryptDto.Duetime, key, vi);
|
|
var time = TimeHelper.GetTimeFromLinuxTime(long.Parse(duetime));
|
|
if (time < DateTime.Now)
|
|
{
|
|
throw new ApiException("已过期,请重新请求!");
|
|
}
|
|
}
|
|
|
|
return decryptString;
|
|
}
|
|
}
|
|
}
|