public IActionResult Index(string key) { int number = 6, fontSize = 16, height = 32; if (Request.Query.TryGetValue("n", out var qs) && int.TryParse(qs, out var value)) { number = value; } if (Request.Query.TryGetValue("s", out qs) && int.TryParse(qs, out value)) { fontSize = value; } if (Request.Query.TryGetValue("h", out qs) && int.TryParse(qs, out value)) { height = value; } using var ms = Verifiers.Create(out var code, number, fontSize, height); Response.Cookies.Append(key, Verifiers.Hashed(code), new CookieOptions { Expires = DateTimeOffset.Now.AddMinutes(3) }); Response.Body.Dispose(); return(File(ms.ToArray(), @"image/png")); }
/// <summary> /// 判断验证码。 /// </summary> /// <param name="key">当前唯一键。</param> /// <param name="code">验证码。</param> /// <returns>返回判断结果。</returns> protected virtual bool IsValidateCode(string key, string code) { if (string.IsNullOrEmpty(code) || !Request.Cookies.TryGetValue(key, out var value)) { return(false); } code = Verifiers.Hashed(code); return(string.Equals(value, code, StringComparison.OrdinalIgnoreCase)); }