|
@@ -1,12 +1,11 @@
|
|
|
|
|
|
-using Microsoft.Extensions.Caching.Distributed;
|
|
|
using OASystem.Infrastructure.Repositories.Login;
|
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
|
using System.Security.Claims;
|
|
|
-using StackExchange.Redis;
|
|
|
using OASystem.Domain.Dtos.UserDto;
|
|
|
-using OASystem.RedisRepository;
|
|
|
-using OASystem.Infrastructure.Repositories.System;
|
|
|
+using OASystem.API.OAMethodLib;
|
|
|
+using Serilog.Parsing;
|
|
|
+using OASystem.Domain.Dtos.System;
|
|
|
|
|
|
namespace OASystem.API.Controllers
|
|
|
{
|
|
@@ -19,17 +18,13 @@ namespace OASystem.API.Controllers
|
|
|
private readonly IMapper _mapper;
|
|
|
private readonly IConfiguration _config;
|
|
|
private readonly LoginRepository _loginRep;
|
|
|
- private readonly UsersRepository _usersRepository;
|
|
|
-
|
|
|
- //private IDatabase _redis;
|
|
|
- //private RedisHelper _redisHelper;
|
|
|
- public AuthController(IConfiguration config, LoginRepository loginRep, IMapper mapper)
|
|
|
+ private readonly MessageRepository _message;
|
|
|
+ public AuthController(IConfiguration config, LoginRepository loginRep, IMapper mapper,MessageRepository message)
|
|
|
{
|
|
|
_config = config;
|
|
|
_loginRep = loginRep;
|
|
|
_mapper = mapper;
|
|
|
-
|
|
|
- //_usersRepository = usersRepository;
|
|
|
+ _message = message;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -54,27 +49,55 @@ namespace OASystem.API.Controllers
|
|
|
#endregion
|
|
|
var view = new LoginView
|
|
|
{
|
|
|
- Expires = DateTime.Now.AddMinutes(30),
|
|
|
UserInfo = userData.Result.Data
|
|
|
};
|
|
|
|
|
|
+
|
|
|
+ DateTime createZebraTime = DateTime.UtcNow;
|
|
|
string authorId = dto.Number + "Token";
|
|
|
+ string authorToken = await RedisRepository.RedisFactory.CreateRedisRepository().StringGetAsync<string>(authorId);//string 取
|
|
|
+ if (authorToken != null)
|
|
|
+ {
|
|
|
+ #region 解析出过期时间
|
|
|
+ var jwtHandler = new JwtSecurityTokenHandler();
|
|
|
+ JwtSecurityToken securityToken = jwtHandler.ReadJwtToken(authorToken);
|
|
|
+ DateTime expDt = (securityToken.Payload[JwtRegisteredClaimNames.Exp] ?? 0).GetInt().GetTimeSpmpToDate();
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ if (expDt >= createZebraTime) //超时重新获取token
|
|
|
+ {
|
|
|
+ authorToken = GeneralMethod.GetToken(_config, dto.Number, createZebraTime);
|
|
|
+ }
|
|
|
+
|
|
|
+ view.Expires = expDt;
|
|
|
+ view.Token = authorToken;
|
|
|
+
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ view.Expires = createZebraTime.AddMinutes(30);
|
|
|
+ view.Token = GeneralMethod.GetToken(_config, dto.Number, createZebraTime);
|
|
|
+ TimeSpan ts = view.Expires.AddMinutes(-1) - createZebraTime; //设置redis 过期时间 比 jwt 时间 快一分钟
|
|
|
+ await RedisRepository.RedisFactory.CreateRedisRepository().StringSetAsync<string>(authorId, view.Token, ts);//string 存
|
|
|
+ }
|
|
|
|
|
|
- var claims = new[] { new Claim(ClaimTypes.NameIdentifier, "Future") };
|
|
|
- var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["JwtSecurityKey"]));
|
|
|
- var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
|
|
|
- var token = new JwtSecurityToken(
|
|
|
- issuer: "OASystem.com",
|
|
|
- audience: "OASystem.com",
|
|
|
- claims: claims,
|
|
|
- expires: view.Expires,
|
|
|
- signingCredentials: creds);
|
|
|
- view.Token = new JwtSecurityTokenHandler().WriteToken(token);
|
|
|
-
|
|
|
- //Redis String使用示例
|
|
|
- //TimeSpan ts = new TimeSpan(0, 30, 0);
|
|
|
- //await RedisRepository.RedisFactory.CreateRedisRepository().StringSetAsync<string>("key01", "value01", ts);//string 存
|
|
|
- //string redisString = await RedisRepository.RedisFactory.CreateRedisRepository().StringGetAsync<string>("key01");//string 取
|
|
|
+
|
|
|
+ #region 测试添加系统消息
|
|
|
+
|
|
|
+ //await _message.AddMsg(new MessageDto()
|
|
|
+ //{
|
|
|
+ // Type = 1,
|
|
|
+ // IssuerId = 208,
|
|
|
+ // Title = "测试添加消息标题",
|
|
|
+ // Content = "消息体测试",
|
|
|
+ // ReleaseTime = DateTime.Now,
|
|
|
+ // UIdList = new List<int> {
|
|
|
+ // 5,
|
|
|
+ // 208,
|
|
|
+ // 219
|
|
|
+ // }
|
|
|
+ //});
|
|
|
+ #endregion
|
|
|
|
|
|
return Ok(JsonView(view));
|
|
|
}
|
|
@@ -134,7 +157,7 @@ namespace OASystem.API.Controllers
|
|
|
/// </summary>
|
|
|
/// <param name="dto"></param>
|
|
|
/// <returns></returns>
|
|
|
- [Authorize]
|
|
|
+ [OASystemAuthentication]
|
|
|
[HttpPost("TestToken")]
|
|
|
[ProducesResponseType(typeof(LoginView), StatusCodes.Status200OK)]
|
|
|
public async Task<IActionResult> TestToken(LoginDto dto)
|