public async Task <IResponseOutput> Refresh([BindRequired] string token) { var userClaims = _userToken.Decode(token); if (userClaims == null || userClaims.Length == 0) { return(ResponseOutput.NotOk()); } var refreshExpires = userClaims.FirstOrDefault(a => a.Type == ClaimAttributes.RefreshExpires)?.Value; if (refreshExpires.IsNull()) { return(ResponseOutput.NotOk()); } if (refreshExpires.ToLong() <= DateTime.Now.ToTimestamp()) { return(ResponseOutput.NotOk("登录信息已过期")); } var userId = userClaims.FirstOrDefault(a => a.Type == ClaimAttributes.UserId)?.Value; if (userId.IsNull()) { return(ResponseOutput.NotOk()); } var output = await _userServices.GetLoginUserAsync(userId.ToLong()); return(GetToken(output)); }
public void CreateTest() { Claim[] claims = { new Claim(UserOptions.UserId, "001"), new Claim(UserOptions.Role, "admin"), new Claim(UserOptions.DBName, "zxsccore"), new Claim(UserOptions.UserName, "admin") }; var token = _userToken.Create(claims); claims = _userToken.Decode(token); Assert.IsNotNull(claims); Assert.IsTrue(claims.Any()); }
/// <summary> /// 刷新Token /// </summary> /// <param name="token"></param> /// <returns></returns> public Task <TokenOutput> RefreshTokenAsync(string token) { var userClaims = _userToken.Decode(token); var refexp = userClaims.FirstOrDefault(x => x.Type == UserOptions.RefreshToken)?.Value ?? throw new HandelException(MessageCodeOption.Bad_Format, "Token"); if (long.TryParse(refexp, out var refexplong)) { _ = refexplong > DateTime.Now.ToTimestamp() ? true : throw new HandelException(MessageCodeOption.Bad_Token); return(TokenAsync(new LoginInput { UserName = userClaims.FirstOrDefault(x => x.Type == UserOptions.UserName)?.Value ?? string.Empty, DbName = userClaims.FirstOrDefault(x => x.Type == UserOptions.DBName)?.Value ?? string.Empty, })); } throw new HandelException(MessageCodeOption.Bad_Format, $"Token中{UserOptions.RefreshToken}"); }