Extjs4.0.7 关于验证码的C#实现
直接上传原码,大家自己去慢慢理解吧。该文章只是讲解了验证码的简单实现和原理,希望大家通过它设计出更绚丽的界面。
如有不对的地方希望大家指正。
1、css文件checkcode.css (定义验证码图片的显示大小)
#CheckCode{ float:left;}
.x-form-code{width:73px;height:20px;vertical-align:middle;cursor:pointer; float:left; margin-left:7px;}
2、html文件(建立登陆页面)
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>V3系统登陆</title>
<link rel="stylesheet" type="text/css" href="/extjs4/resources/css/ext-all.css" />
<link rel="stylesheet" type="text/css" href="/CSS/css.css" />
<link rel="stylesheet" type="text/css" href="/CSS/checkcode.css" />
<script type="text/javascript" src="/extjs4/locale/ext-lang-zh_CN.js"></script>
<script type="text/javascript" src="/extjs4/ext-all.js"></script>
<script type="text/javascript" src="/js/v3login.js"></script>
<style type="text/css">body{
background-image:url(/images/eg_bg_06.gif);
background-repeat: repeat
}
</style>
</head>
<body>
</body>
</html>
3、Extjs文件 v3login.js (Extjs的具体实现,登陆采用Form的形式)
using System;using System.Collections.Generic;using System.Web.SessionState;using System.Web;using BLL.System;using Model;using System.Data;namespace V3WEB.ashx{ /// <summary> /// login 的摘要说明 /// </summary> public class login : IHttpHandler, IRequiresSessionState { public void ProcessRequest(HttpContext context) { Comm_Grant_UserBLL obj = new Comm_Grant_UserBLL(); string _JsonData = null; int count = 0; if (context.Session["CheckCode"].ToString().ToUpper() != context.Request["CheckCode"].ToString().ToUpper()) { _JsonData = "{success:true,msg:'出错!验证码错误!'}"; } else { try { string _userid = context.Request["UserID"].ToString(); string _userpwd = context.Request["UserPwd"].ToString(); DataTable u = obj.getIsRight(_userid, _userpwd); if (u.Rows.Count > 0) { context.Session["UserID"] = u.Rows[0]["UserID"].ToString(); context.Session["UserName"] = u.Rows[0]["UserName"].ToString(); context.Session["Login"] = "Yes"; count = 1; } else { count = 0; } if (count == 1) { _JsonData = "{success:true,msg:'OK'}"; } else { _JsonData = "{success:true,msg:'用户或密码错误!'}"; } } catch (Exception e) { _JsonData = "{success:false,msg:'" + e.Message + "'}"; } } context.Response.ContentType = "text/plain"; // "text/plain"; context.Response.Write(_JsonData); context.Response.End(); } public bool IsReusable { get { return false; } } }}
完成以上过程就可以产生简单的验证码登陆界面了。希望对有需要的朋友其他抛砖引玉的作用。