public override async Task InvokeAsync(HttpContext context) { VCodeResponseModel responseModel = await _service.VCode(); string responseJsonStr = _jsonHelper.Serialize(responseModel); context.Response.ContentType = "application/json"; await context.Response.WriteAsync(responseJsonStr, Encoding.UTF8); // Response.Write 开始, 不要再 Call next // Call the next delegate/middleware in the pipeline //await _next(context); }
/// <summary> /// 响应验证码,用户会话唯一标识 /// </summary> /// <returns></returns> public Task <VCodeResponseModel> VCode() { VCodeResponseModel rtnResult = new VCodeResponseModel(); try { VCodeImgModel model = CreateVCodeImg(); string userId = Guid.NewGuid().ToString(); rtnResult.code = 0; rtnResult.message = "获取验证码成功"; rtnResult.data = new VCodeResponseModel.DataModel { userId = userId, vCodeImg = model.ImgBase64, vCodeTip = model.VCodeTip, words = model.Words }; // 生成 vCodeKey: 转为json字符串 -> AES加密 string vCodekeyJsonStr = JsonHelper.Serialize(new VCodeKeyModel { ErrorNum = 0, TS = DateTimeHelper.NowTimeStamp13(), VCodePos = model.VCodePos }); string vCodeKey = _encryptHelper.Encrypt(vCodekeyJsonStr, _options.EncryptKey); // 答案 保存到 此次用户会话对应的 Cache 中 _cacheHelper.Insert <string>(CachePrefixVCodeKey + userId, vCodeKey); } catch (Exception ex) { rtnResult.code = -1; rtnResult.message = "获取验证码失败"; _logHelper?.Write(ex.ToString()); } // TODO: 在.net framework 4.0下未测试 // Task 参考: https://www.cnblogs.com/yaopengfei/p/8183530.html #if NETFULL40 return(Task.Factory.StartNew(() => { return rtnResult; })); #else return(Task.FromResult(rtnResult)); #endif }
public async Task <IActionResult> VCodeImg() { VCodeResponseModel rtnResult = await _service.VCode(); return(Ok(rtnResult)); }