asp.net core配合vue实现后端验证码逻辑(2)

/// <summary> /// 获取验证码 /// </summary> /// <returns></returns> [HttpGet("getCaptchaImage/{width:int}/{height:int}/{roomId}")] public IActionResult GetCaptchaImage(int width, int height, string roomId) { Console.WriteLine(roomId); //int width = 100; //int height = 36; var captchaCode = Captcha.GenerateCaptchaCode(); var result = Captcha.GenerateCaptchaImage(width, height, captchaCode); string redisKey = "VerifyCode_" + roomId; Startup.redisDb.StringSet(redisKey, captchaCode, new TimeSpan(0, 5, 0)); Stream s = new MemoryStream(result.CaptchaByteData); return new FileStreamResult(s, "image/png"); } /// <summary> /// 登录 /// </summary> /// <returns></returns> [HttpPost("login")] public ApiResponseData Login(LoginDto loginInfo) { if (string.IsNullOrWhiteSpace(loginInfo.username)) return ApiResponse.Error("用户名不能为空"); if (string.IsNullOrWhiteSpace(loginInfo.password)) return ApiResponse.Error("密码不能为空"); Entity.BaseOperator operInfo = Db .Select<BaseOperator>() .Where(a => a.OperatorCode == loginInfo.username && a.Password == loginInfo.password.ToLower() && a.Status == 1 && a.IsDel == false).ToOne(); if (operInfo == null) return ApiResponse.Error("用户名或者密码不正确"); bool verifyResult = Captcha.ValidateCaptchaCode(loginInfo.RoomId, loginInfo.VerifyCode); if(verifyResult == false) return ApiResponse.Error("验证码不正确"); //登录时重新生成token,一个用户只能在一个地方登录 string token = System.Guid.NewGuid().ToString().Replace("-", ""); Db.Update<BaseOperator>(operInfo.OperatorId) .Set(a => a.Token, token) .ExecuteAffrows(); dynamic outJson = new ExpandoObject();//初始化一个不包含任何成员的ExpandoObject outJson.token = token; //存入redis string redisKey = "UserInfo_" + token; Startup.redisDb.StringSet(redisKey, operInfo.OperatorId, new TimeSpan(24, 0, 0)); return ApiResponse.Succ(outJson); }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyfdgy.html