aspnet zero 自带的验证码是基于Google ,国内当前无法使用,只能替换国内的。实现后的界面如下图:

| Install-Package Lazy.Captcha.Core |
| public interface ICaptchaAppService : IApplicationService |
| { |
| |
| |
| |
| |
| Stream Captcha(string id); |
| |
| |
| |
| |
| |
| |
| |
| Task ValidateAsync(string id,string code); |
| } |
| |
| public class CaptchaAppService : SISAppServiceBase, ICaptchaAppService |
| { |
| private readonly IHttpContextAccessor _httpContextAccessor; |
| private readonly ICaptcha _captcha; |
| |
| public CaptchaAppService(ICaptcha captcha, IHttpContextAccessor httpContextAccessor) |
| { |
| _captcha = captcha; |
| _httpContextAccessor = httpContextAccessor; |
| } |
| |
| public Stream Captcha(string id) |
| { |
| var captcha = _captcha.Generate(id, 30); |
| var stream = new MemoryStream(captcha.Bytes); |
| return stream; |
| } |
| |
| |
| public async Task ValidateAsync(string id, string code) |
| { |
| var httpContext = _httpContextAccessor.HttpContext; |
| if (httpContext == null) |
| { |
| throw new Exception("RecaptchaValidator should be used in a valid HTTP context!"); |
| } |
| |
| if (id.IsNullOrEmpty() || code.IsNullOrEmpty()) |
| { |
| throw new UserFriendlyException(L("CaptchaCanNotBeEmpty")); |
| } |
| |
| var result = _captcha.Validate(id, code); |
| |
| if (!result) |
| { |
| throw new UserFriendlyException(L("IncorrectCaptchaAnswer")); |
| } |
| |
| await Task.CompletedTask; |
| |
| } |
| } |
| [HttpPost] |
| public virtual async Task<JsonResult> Login(LoginViewModel loginModel, string returnUrl = "", |
| string returnUrlHash = "", string ss = "") |
| { |
| returnUrl = NormalizeReturnUrl(returnUrl); |
| if (!string.IsNullOrWhiteSpace(returnUrlHash)) |
| { |
| returnUrl = returnUrl + returnUrlHash; |
| } |
| |
| string id = HttpContext.Request.Form["id"]; |
| string captcha = HttpContext.Request.Form["captcha"]; |
| await _captchaAppservice.ValidateAsync(id, captcha); |
| |
| var loginResult = await GetLoginResultAsync( |
| loginModel.UsernameOrEmailAddress, |
| loginModel.Password, |
| GetTenancyNameOrNull() |
| ); |
| |
| |
| } |
| <div class="mb-5"> |
| <div class="row"> |
| <div class="col-6"> |
| <input type="text" class="form-control form-control-solid h-auto px-6 rounded-lg fs-6" |
| placeholder="验证码" name="captcha" id="captcha" |
| style="padding-top:1.0rem !important;padding-bottom:1.0rem !important;"/> |
| </div> |
| <div class="col-6"> |
| <img src="/" width="100" height="50" id="captchaImage"/> |
| </div> |
| </div> |
| </div> |
| function guid() { |
| function S4() { |
| return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1); |
| } |
| return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4()); |
| } |
| |
| function bingImage() { |
| |
| var id = guid(); |
| $("#guid").val(id); |
| var src = abp.appPath + "captcha/captcha?id="+ id; |
| $("#captchaImage").attr("src", src); |
| } |
| |
| $("#captchaImage").click(function () { |
| bingImage(); |
| }); |
| return { |
| init: function () { |
| bingImage(); |
| handleLogin(); |
| }, |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架