在ASP.NET MVC项目中使用极验验证(geetest)
geetest开发体验,写一下快速搭建geetst的步骤(极简模式)
首先,我们去geetest注册一个账号 ,拿到验证所需的captcha_id和private_key(不想注册的同学可以使用下面提供测试id和key)
captcha_id:b46d1900d0a894591916ea94ea91bd2c
private_key:36fc3fe98530eea08dfc6ce76e3d24c4
我们一般在做项目时为了保证提交的数据是真实用户数据而非机器人提交一般都会添加验证码操作来让用户在提交表单的同时填上服务端生成的验证码,这样在一定程度上能够防止暴力提交或者暴力破解。但是却在一定程度上增加了开发的成本。
现在的互联网上各种产品都能够独立开发使用,今天小猪就为大家介绍 极验验证 这个工具。
目前网络中已经可以发现大量使用该产品的应用,用户只需简单的滑动滑块来进行验证( 体验地址 ),一定程度上提高了用户体验。
极验官方提供了C#版本的服务端 SDK 。并且提供了一个简单的 DEMO 。可惜该DEMO是Web Form版本的。而目前大家使用的比较多的是ASP.NET MVC项目,所以今天小猪就在其官方DEMO的基础上改成了ASP.NET MVC项目。
- 新建ASP.NET MVC项目,并在项目中引用C#版本的SDK。
- 申请极验账号并添加验证。如图:
成功添加后我们可以看到对应的ID和KEY。
新建测试控制器 AccountController
- 在控制器中随意添加一个Action,例如Login
- 在对应的View里添加代码(例如Login.cshtml):
<div id="geetest-container"> </div>
<script src="http://static.geetest.com/static/tools/gt.js"></script> <script>
window.addEventListener('load',processGeeTest); function processGeeTest() {
$.ajax({
// 获取id,challenge,success(是否启用failback)
url: "/Account/GeekTest",
type: "get",
dataType: "json", // 使用jsonp格式
success: function (data) {
// 使用initGeetest接口
// 参数1:配置参数,与创建Geetest实例时接受的参数一致
// 参数2:回调,回调的第一个参数验证码对象,之后可以使用它做appendTo之类的事件
initGeetest({
gt: data.gt,
challenge: data.challenge,
product: "float", // 产品形式
offline: !data.success
}, handler);
}
});
} var handler = function (captchaObj) {
// 将验证码加到id为captcha的元素里
captchaObj.appendTo("#geetest-container"); captchaObj.onSuccess = function(e) {
console.log(e);
} };
</script>
<divid="geetest-container">
</div>
<scriptsrc="http://static.geetest.com/static/tools/gt.js"></script>
<script>
window.addEventListener('load',processGeeTest);
function processGeeTest() {
$.ajax({
// 获取id,challenge,success(是否启用failback)
url: "/Account/GeekTest",
type: "get",
dataType: "json", // 使用jsonp格式
success: function (data) {
// 使用initGeetest接口
// 参数1:配置参数,与创建Geetest实例时接受的参数一致
// 参数2:回调,回调的第一个参数验证码对象,之后可以使用它做appendTo之类的事件
initGeetest({
gt: data.gt,
challenge: data.challenge,
product: "float", // 产品形式
offline: !data.success
}, handler);
}
});
}
var handler = function (captchaObj) {
// 将验证码加到id为captcha的元素里
captchaObj.appendTo("#geetest-container");
captchaObj.onSuccess = function(e) {
console.log(e);
}
};
</script>
上述代码中使用了JQuery库,需要同时引用。其中我们使用Ajax来动态请求后台AccountController的GeeTest方法,代码如下:
public ActionResult GeekTest()
{
string result = getCaptcha(); return Content(result, "application/json");
} private String getCaptcha()
{
GeetestLib geetest = new GeetestLib(geetest_publicKey, geetest_privateKey);
Byte gtServerStatus = geetest.preProcess();
Session[GeetestLib.gtServerStatusSessionKey] = gtServerStatus;
return geetest.getResponseStr();
}
public ActionResultGeekTest()
{
string result = getCaptcha();
return Content(result, "application/json");
}
private String getCaptcha()
{
GeetestLibgeetest = new GeetestLib(geetest_publicKey, geetest_privateKey);
Byte gtServerStatus = geetest.preProcess();
Session[GeetestLib.gtServerStatusSessionKey] = gtServerStatus;
return geetest.getResponseStr();
}
上述代码中的 geetest_publicKey
和 geetest_privateKey
分别对应极验后台申请到的ID和KEY。
这样我们就可以运行程序看到效果。接下来的工作就是确认用户在提交对应的表单的时候有没有通过极验的验证了。
在接受表单提交的POST地址里面输入代码,例如:
//
// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (!CheckGeeTestResult())
{
ModelState.AddModelError("", "请先完成验证操作。");
return View(model);
}
if (!ModelState.IsValid)
{
return View(model);
} //其他验证逻辑
...
}
} private bool CheckGeeTestResult()
{
GeetestLib geetest = new GeetestLib(geetest_publicKey, geetest_privateKey);
Byte gt_server_status_code = (Byte)Session[GeetestLib.gtServerStatusSessionKey];
String userID = (String)Session["userID"]; int result = 0;
String challenge = Request.Form.Get(GeetestLib.fnGeetestChallenge);
String validate = Request.Form.Get(GeetestLib.fnGeetestValidate);
String seccode = Request.Form.Get(GeetestLib.fnGeetestSeccode);
if (gt_server_status_code == 1) result = geetest.enhencedValidateRequest(challenge, validate, seccode, userID);
else result = geetest.failbackValidateRequest(challenge, validate, seccode);
if (result != 1) return false; return true;
}
//
// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModelmodel, string returnUrl)
{
if (!CheckGeeTestResult())
{
ModelState.AddModelError("", "请先完成验证操作。");
return View(model);
}
if (!ModelState.IsValid)
{
return View(model);
}
//其他验证逻辑
...
}
}
private bool CheckGeeTestResult()
{
GeetestLibgeetest = new GeetestLib(geetest_publicKey, geetest_privateKey);
Byte gt_server_status_code = (Byte)Session[GeetestLib.gtServerStatusSessionKey];
String userID = (String)Session["userID"];
int result = 0;
String challenge = Request.Form.Get(GeetestLib.fnGeetestChallenge);
String validate = Request.Form.Get(GeetestLib.fnGeetestValidate);
String seccode = Request.Form.Get(GeetestLib.fnGeetestSeccode);
if (gt_server_status_code == 1) result = geetest.enhencedValidateRequest(challenge, validate, seccode, userID);
else result = geetest.failbackValidateRequest(challenge, validate, seccode);
if (result != 1) return false;
return true;
}
上述代码中大部分引用了极验官方DEMO的代码。这样我们在前台view中可以使用
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
来提示用户首先需要通过滑块验证才能继续操作。
这样我们就完成了将极验验证集成到我们的ASP.NET MVC项目中。并且可以在极验的后台中查看具体的数据报表:
整个产品的使用我们并没有编写太多的代码,而且将复杂的验证过程交给了第三方,而我们自己只需要关心自己的业务逻辑
在ASP.NET MVC项目中使用极验验证(geetest)的更多相关文章
- 在 ASP.NET MVC 项目中使用 WebForm、 HTML
原文地址:http://www.cnblogs.com/snowdream/archive/2009/04/17/winforms-in-mvc.html ASP.NET MVC和WebForm各有各 ...
- 在已有的Asp.net MVC项目中引入Taurus.MVC
Taurus.MVC是一个优秀的框架,如果要应用到已有的Asp.net MVC项目中,需要修改一下. 1.前提约定: 走Taurus.MVC必须指定后缀.如.api 2.原项目修改如下: web.co ...
- ASP.NET MVC项目中App_Code目录在程序应用
学习ASP.NET MVC,如果你是开发ASP.NET MVC项目的,也许你去为项目添加前ASP.NET项目的APP_Code目录,在这里创建与添加的Class类,也许你无法在MVC项目所引用. 那这 ...
- 在ASP.NET MVC项目中使用React
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:最近在开发钉钉的微应用,考虑到性能和UI库的支持,遂采用了React来开发前端. 目前 ...
- 在ASP.NET MVC 项目中 使用 echarts 画统计图
echarts 官方地址:http://echarts.baidu.com/ 一.根据图中的数据怎么从数据库中获取并组装成对应格式: 从数据库中获取对应数据,然后在项目中引用Newtonsoft.Js ...
- 如何在node和vue前后端分离的项目中使用极客验证,用node的方式
1.用express的脚手架和vue-cli的脚手架搭建的项目目录如下图 2.在vue-client的src/component新建一个login.vue文件,作为登录页面,代码如下 <temp ...
- ASP.NET MVC项目中EntityFramework"代码优先方法"的使用步骤
EF提供了三种方式来实现项目,分别是: (1)代码优先方法: (2)模型优先方法: (3)数据库优先方法: 本篇主要记录在Vs2010环境下使用代码优先的方式实现数据库和后端代码数据交互,语言为C#, ...
- nginx在asp.net mvc项目中 配置 初步快速入门
nginx 官方下载地址 http://nginx.org/en/download.html 一般.net项目要运行在IIS环境下,自然选择windows版下载 我这里下载了nginx/Windows ...
- ASP.NET MVC 项目中 一般处理程序ashx 获取Session
1-在 aspx和aspx.cs中,都是以Session["xxx"]="aaa"和aaa=Session["xxx"].ToString( ...
随机推荐
- 鸟哥私房菜基础篇:例行性工作排程 (crontab)习题
猫宁!!! 参考:http://cn.linux.vbird.org/linux_basic/0430cron.php 1-今天假设我有一个命令程序,名称为: ping.sh 这个档名!我想要让系统每 ...
- PJzhang:如何缓解Mimikatz从Windows2008 R2内存中读取域控密码?
猫宁!!! 参考: https://xz.aliyun.com/t/4180 https://www.anquanke.com/post/id/156299 https://www.cnblogs.c ...
- springboot使用elasticsearch的客户端操作eslaticsearch
一 ES客户端 ES提供多种不同的客户端: 1.TransportClient ES提供的传统客户端,官方计划8.0版本删除此客户端. 2.RestClient RestClient是官方推荐使用的 ...
- CentOS7之yum仓库配置
操作系统版本:CentOS Linux release 7.2.1511 (Core) Yum软件版本:yum-3.4.3-132.el7.centos.0.1.noarch Yum主配置文件:/ ...
- Spring5的总体架构图
Spring5的主体架构图 主要是四大部分:Web.Data Access/Integration.Core Container.中间层,具体见下图:
- Arrays.asList()方法注意事项
1.Arrays.asList()底层数组作为物理层实现.所以返回的List大小不可更改,即不可以做add().remove()操作,并且对List所做的任何变动都会致使原数组发生变动. public ...
- 老贾的幸福生活day03 之思维导图
思维导图 层级关系 从大范围到具体 编程语言 编译型 C C++ ...... 解释型 python php ......... python 基础语法 基础数据类 ...
- spark内核篇-task数与并行度
每一个 spark job 根据 shuffle 划分 stage,每个 stage 形成一个或者多个 taskSet,了解了每个 stage 需要运行多少个 task,有助于我们优化 spark 运 ...
- linux lkm rootkit常用技巧
简介 搜集一下linux lkm rootkit中常用的一些技巧 1.劫持系统调用 遍历地址空间 根据系统调用中的一些导出函数,比如sys_close的地址来寻找 unsigned long ** g ...
- golang作用域问题
//参考 https://segmentfault.com/a/1190000012214571 //参考 https://studygolang.com/articles/2215 func bar ...