準備工作
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;
}