public async Task <ActionResult <string> > PostLogin(UserModel usermodel) { JwtTokenUtil jwtTokenUtil = new JwtTokenUtil(_configuration); string token = jwtTokenUtil.GetToken(usermodel.Name); //生成token return(token); }
public IActionResult UserLogin([FromBody] User user) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } User result = _context.User.SingleOrDefault(u => u.Username == user.Username); if (result == null || result.Password != user.Password) { return(BadRequest(new { msg = "用户名或者密码错误!请稍后再试!" })); } JwtTokenUtil jwtUtil = new JwtTokenUtil(_configuration); string token = jwtUtil.GetToken(result); return(Ok(new { token = token, id = result.Id, avatar = result.Avatar, username = result.Username, msg = "登陆成功" })); }
public async Task <ActionResult <ExcutedResult> > SignIn(LoginUser userinfo) { try { QuartzService.StartJob <QuartzJob>("jobWork1", 1); //请求状态 int statecode = (int)ExcutedResult.status.成功; var pwd = MD5Helper.MD5Encrypt32(userinfo.PassWord); //验证密码是否正确 var user = await _userService.SignIn(userinfo.UserName, pwd); //不正确 if (user == null) { statecode = (int)ExcutedResult.status.账号密码错误; return(ExcutedResult.SuccessResult("账号密码错误", statecode)); } //从redis里面读取token 如果有值就继续用以前的Token 如果没有就重新生成 var Token = _memoryCacheService.Get <Blog_Users>(user.UserToken); if (Token == null) { //登录成功 生成Token JwtTokenUtil jwtTokenUtil = new JwtTokenUtil(_configuration); //生成Token string token = jwtTokenUtil.GetToken(user); user.UserToken = token; //修改用户token var updatetoken = await _userService.UpdateUserToken(user); //修改成功后 并存在redis把用户存在redis缓存当中 if (updatetoken > 0) { _memoryCacheService.AddObject(token, user, new TimeSpan(1, 0, 0), false); } } return(ExcutedResult.SuccessResult(user, statecode)); } catch (Exception ex) { return(ExcutedResult.FailedResult(ex.Message, (int)ExcutedResult.status.请求失败)); } }
public OkObjectResult GetJWT([FromBody] UserInfo user) { if (user.Name == "admin" && user.Password == "12345") { JwtTokenUtil jwtTokenUtil = new JwtTokenUtil(_configuration); string token = jwtTokenUtil.GetToken(user); //生成token return(Ok(new JsonBase() { IsSuccess = true, Message = "登录成功", Token = token })); } else { return(Ok(new JsonBase() { IsSuccess = false, Message = "登陆失败" })); } }