前言
网上大部分验证码都是PHP的,基于C# .Net开发的很少,举荐一款很漂亮且实用的C#图形验证码,能够自定义背景图库,性能还是挺弱小的。
前端接入脚本
<script src="captcha.js?appid=xxx"></script><script>kg.captcha({ // 绑定元素,验证框显示区域 bind: "#captchaBox", // 验证胜利事务处理 success: function(e) { console.log(e); }, // 验证失败事务处理 failure: function(e) { console.log(e); }, // 点击刷新按钮时触发 refresh: function(e) { console.log(e); }});</script><div id="captchaBox">载入中 ...</div>
C# 代码
using System;using KgCaptchaSDK;public partial class _Default : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { // 后端解决 string html, appId, appSecret, Token; if (Request.Form.ToString().Length > 0){ // 有数据处理 // 填写你的 AppId,在利用治理中获取 appId = "appId"; // 填写你的 AppSecret,在利用治理中获取 appSecret = "appSecret"; var request = new kgCaptcha(appId, appSecret); // 前端验证胜利后颁发的 token,有效期两分钟 request.token = Request.Form["kgCaptchaToken"]; // 填写应用服务域名,在利用治理中获取 request.appCdn = "https://cdn.kgcaptcha.com"; // 当安全策略中的防控等级为3时必须填写,个别状况下能够疏忽 // 能够填写用户输出的登录帐号(如:Request.Form["username"]),可拦挡同一帐号屡次尝试等行为 request.userId = "kgCaptchaDemo"; // 申请超时工夫,秒 request.connectTimeout = 5; // 发送验证申请 var requestResult = request.sendRequest(); if (requestResult.code == 0) { // 验签胜利逻辑解决 *** // 这里做验证通过后的数据处理 // 如登录/注册场景,这里通常查询数据库、校验明码、进行登录或注册等动作解决 // 如短信场景,这里能够开始向用户发送短信等动作解决 // ... html = "<script>alert(´验证通过´);history.back();</script>"; } else { // 验签失败逻辑解决 html = "<script>alert(\"" + requestResult.msg + " - " + requestResult.code + "\");history.back();</script>"; } // 输入后果 Response.Write(html); } Response.Redirect("index.html"); }}
最初
SDK开源地址:https://github.com/KgCaptcha,顺便做了一个演示:https://www.kgcaptcha.com/demo/