准备工作
gt.js文件 jquery-1.7.1min.js或更高版本
1、 js验证及 获取验证码
<script type="text/javascript">
window.addEventListener('load', processGeeTest);
function processGeeTest() {
$.ajax({
// 获取id,challenge,success(是否启用failback)
url: "/Login/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);
}
});
} UsersBll bllUser = new UsersBll();
UserRegionBll bllUserRegion = new UserRegionBll();
public ActionResult Index()
{
return View();
}
[HttpPost]
//登录 根据登录的用户名和密码查询是否有该角色 并且验证码必须正确 跳到主页
public ActionResult Index(LoginEntity entity)
{
if (ModelState.IsValid)
{
usersEntity u = bllUser.GetUserByAccountAndPassword(entity);
if (u == null)
{
return Content("<script>alert('用户名或密码不正确');window.location.href='/Login/Index'</script>");
}
if (u != null)
{
if (!CheckGeeTestResult())
{
return Content("<script>alert('请点击按钮进行验证');window.location.href='/Login/Index'</script>");
}
CurrentUserEntity currentUser = new CurrentUserEntity();
currentUser.id = u.id;
currentUser.account = u.account;
currentUser.createdate = u.createdate;
currentUser.parentid = u.parentid;
currentUser.password = u.password;
currentUser.role = u.role;
currentUser.truename = u.truename;
List<user_regionEntity> listUserRegion = bllUserRegion.GetUserRegionByUserId(u.id);
currentUser.userregion = listUserRegion;
Session["CurrentUser"] = currentUser;
return RedirectToAction("Index", "Home");
}
}
return View();
}
public ActionResult GeekTest()
{
return Content(GetCaptcha(), "application/json");
}
private string GetCaptcha()
{
GeetestLib geetest = new GeetestLib("898684e62c5424dde6f3d0010597bdec", "5435b2681a932a3c9af9296bf170e5bd");
String userID = "test";
Byte gtServerStatus = geetest.preProcess(userID, "web", "127.0.0.1");
Session[GeetestLib.gtServerStatusSessionKey] = gtServerStatus;
Session["userID"] = userID;
return geetest.getResponseStr();
}
public bool CheckGeeTestResult()
{
var geetest = new GeetestLib("898684e62c5424dde6f3d0010597bdec", "5435b2681a932a3c9af9296bf170e5bd");
var gtServerStatusCode = (byte)Session[GeetestLib.gtServerStatusSessionKey];
var userId = (string)Session["userID"];
var challenge = Request.Form.Get(GeetestLib.fnGeetestChallenge);
var validate = Request.Form.Get(GeetestLib.fnGeetestValidate);
var seccode = Request.Form.Get(GeetestLib.fnGeetestSeccode);
var result = gtServerStatusCode == 1 ? geetest.enhencedValidateRequest(challenge, validate, seccode, userId) : geetest.failbackValidateRequest(challenge, validate, seccode);
return result == 1;
}
var handler = function (captchaObj) { // 将验证码加到id为captcha的元素里 captchaObj.appendTo("#code"); captchaObj.onSuccess = function (e) { console.log(e); } }; document.onkeydown=function(event){ var e = event || window.event || arguments.callee.caller.arguments[0]; if(e && e.keyCode==13){ // enter 键 YanZhen() } }; function YanZhen() { var name = $("#username").val(); var pwd = $("#userpwd").val(); if (name=="Your name") { alert("请输入您的用户名!"); $("#username").focus(); return""; } if (pwd=="******") { alert("请输入您的密码!"); $("#userpwd").focus(); return ""; } $("#body form").submit(); } </script> 2、在form表单内 写验证码的div
<h2>验证码:</h2>
<div id="code"></div>
3、后台接受和验证
UsersBll bllUser = new UsersBll();
UserRegionBll bllUserRegion = new UserRegionBll();
public ActionResult Index()
{
return View();
}
[HttpPost]
//登录 根据登录的用户名和密码查询是否有该角色 并且验证码必须正确 跳到主页
public ActionResult Index(LoginEntity entity)
{
if (ModelState.IsValid)
{
usersEntity u = bllUser.GetUserByAccountAndPassword(entity);
if (u == null)
{
return Content("<script>alert('用户名或密码不正确');window.location.href='/Login/Index'</script>");
}
if (u != null)
{
if (!CheckGeeTestResult())
{
return Content("<script>alert('请点击按钮进行验证');window.location.href='/Login/Index'</script>");
}
CurrentUserEntity currentUser = new CurrentUserEntity();
currentUser.id = u.id;
currentUser.account = u.account;
currentUser.createdate = u.createdate;
currentUser.parentid = u.parentid;
currentUser.password = u.password;
currentUser.role = u.role;
currentUser.truename = u.truename;
List<user_regionEntity> listUserRegion = bllUserRegion.GetUserRegionByUserId(u.id);
currentUser.userregion = listUserRegion;
Session["CurrentUser"] = currentUser;
return RedirectToAction("Index", "Home");
}
}
return View();
}
public ActionResult GeekTest()
{
return Content(GetCaptcha(), "application/json");
}
private string GetCaptcha()
{
GeetestLib geetest = new GeetestLib("自己的公开key", "自己的私有key");
String userID = "test";
Byte gtServerStatus = geetest.preProcess(userID, "web", "127.0.0.1");
Session[GeetestLib.gtServerStatusSessionKey] = gtServerStatus;
Session["userID"] = userID;
return geetest.getResponseStr();
}
public bool CheckGeeTestResult()
{
var geetest = new GeetestLib("自己的公开key", "自己的私有key");
var gtServerStatusCode = (byte)Session[GeetestLib.gtServerStatusSessionKey];
var userId = (string)Session["userID"];
var challenge = Request.Form.Get(GeetestLib.fnGeetestChallenge);
var validate = Request.Form.Get(GeetestLib.fnGeetestValidate);
var seccode = Request.Form.Get(GeetestLib.fnGeetestSeccode);
var result = gtServerStatusCode == 1 ? geetest.enhencedValidateRequest(challenge, validate, seccode, userId) : geetest.failbackValidateRequest(challenge, validate, seccode);
return result == 1;
}