public async Task <ApiResult> Logout(string token) { var r = new ApiResult(); try { if (!string.IsNullOrEmpty(token)) { var tokenModel = JwtHelper.SerializeToken(token); if (tokenModel != null && JwtHelper.CustomSafeVerify(token) && tokenModel.Uid > 0) { if (_redis.Exist($"Token:{token}")) { await _redis.RemoveAsync($"Token:{token}"); } } } r.msg = "退出登录成功"; return(r); } catch (Exception ex) { r.status = 500; r.msg = ex.Message; } return(r); }
public async Task <ApiResult <TokenInfoViewModel> > RefreshToken(string token) { var r = new ApiResult <TokenInfoViewModel>(); try { if (string.IsNullOrEmpty(token)) { r.status = 400; r.msg = "token无效,请重新登录!"; return(r); } var tokenModel = JwtHelper.SerializeToken(token); if (tokenModel != null && JwtHelper.CustomSafeVerify(token) && tokenModel.Uid > 0) { var user = await _userServices.GetById(tokenModel.Uid); if (user != null) { var userRole = await _roleServices.GetList(_ => _.UserID == user.Id); var role = string.Empty; if (userRole != null && userRole.Any()) { role = string.Join(",", userRole.Select(_ => _.RoleID).Distinct()); } //创建声明数组 var claims = new Claim[] { new Claim("uid", user.Id.ToString()), new Claim(ClaimTypes.Name, user.UserName), new Claim(ClaimTypes.Role, role), new Claim(JwtRegisteredClaimNames.Email, user.Email) }; var responseJson = JwtToken.BuildJwtToken(claims); if (responseJson != null) { r.status = 200; r.msg = "刷新Token成功!"; r.data = responseJson; new AuthHelper(_accessor, _redis).SaveCurrSessionAndUserRole(responseJson, new AuthModel() { UserID = user.Id, UserName = user.UserName, RoleIDs = userRole?.Select(_ => _.Id).Distinct().ToList() ?? new List <int>() }); return(r); } } } r.status = 400; r.msg = "刷新token失败请重新登录!"; return(r); } catch (Exception ex) { r.status = 500; r.msg = ex.Message; } return(r); }