public ActionResult <ResultMsg> GetToken(string staffId) { ResultMsg resultMsg = null; int id = 0; //判断参数是否合法 if (string.IsNullOrEmpty(staffId) || (!int.TryParse(staffId, out id))) { resultMsg = new ResultMsg(); resultMsg.StatusCode = (int)StatusCodeEnum.ParameterError; resultMsg.Info = StatusCodeEnum.ParameterError.GetEnumText(); resultMsg.Data = ""; return(resultMsg); } //先从缓存中获取获取不到插入缓存 Token token = (Token)CacheHelper.CacheValue(id.ToString()); if (CacheHelper.CacheValue(id.ToString()) == null) { token = new Token(); token.StaffId = id; token.SignToken = Guid.NewGuid(); token.ExpireTime = DateTime.Now.AddDays(1); CacheHelper.CacheInsertAddMinutes(token.StaffId.ToString(), token, 10); } //返回token信息 resultMsg = new ResultMsg(); resultMsg.StatusCode = (int)StatusCodeEnum.Success; resultMsg.Info = ""; resultMsg.Data = token; return(resultMsg); }
private void VerifyJti(string jti) { if (!string.IsNullOrEmpty(jti)) { if (CacheHelper.CacheValue <string>(jti) == null) { CacheHelper.CacheInsertAddMinutes <string>(jti, jti, 10); return; } } throw new UnauthorizedException(); }
public MyResult Post([FromBody] LoginInfo user) { if (string.IsNullOrWhiteSpace(user.UserName)) { return(MyResult.Error("用户名不能为空!")); } if (string.IsNullOrWhiteSpace(user.Password)) { return(MyResult.Error("密码不能为空!")); } // 读取用户信息 UserInfo userInfo = UserBLL.GetUserDetail(user.UserName, user.Password); if (userInfo == null) { throw new MsgException("用户名或密码错误!"); } // 缓存一天 userInfo.Token = JObject.FromObject(user).ToString().ToMD5(); CacheHelper.CacheInsertAddMinutes(userInfo.Token, user, 24 * 60); return(MyResult.OK(userInfo)); }