如题:随机验证码生成后,保存在session里了。那么,如何在用户进行登录时,进行无刷新的验证。应该使用到ajax,最好有简单的示例代码。
解决方案 »
- Repeter 写的图片循环?c#
- Coolite ExtJS鼠标移动到gridpanel行上 变色问题 高亮如何改颜色?
- 不显示删除回复显示所有回复显示星级回复显示得分回复 用户控件中使用 AjaxPro 问题,无法识别类名
- asp.net 截取用分号隔开的字符串
- 控制session超时
- 从无滚动条A页面转到B页面,怎么让B页面出现滚动条?
- C#新手,求教怎样在C#2005里面添加页面的UnLoad事件处理函数?
- 使用SQLDMO备份还原数据库,自定义路径的问题
- 如何弹出一个模态的叶面???
- 请教各位,如何在ASP.net的应用程序中画简单线条
- 我在网上下了一个网站后台,很好看,该怎样应用到我的网站中
- 不允许用于访问路径“/Web/images/upimages/2.jpg”的 HTTP 谓词 POST
using System.Web;
using System.Drawing;
public class HandlerCheck : IHttpHandler,System.Web.SessionState.IRequiresSessionState{
/// <summary>
/// 随机验证码sssssss
/// </summary>
/// <param name="context"></param>
public void ProcessRequest(HttpContext context)
{
string CheckCode = RandomNum(4);//随机生成4个字符
//把生成的随机字符串保存到Session对象里面去
context.Session["CheckCode"] = CheckCode;
Random rand = new Random();
int iwidth = (int)(CheckCode.Length * 15);
System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 22);
Graphics g = Graphics.FromImage(image);
g.Clear(Color.White);
//画图片的背景噪音线20条
for (int i = 0; i < 20; i++)
{
int x1 = rand.Next(image.Width);
int x2 = rand.Next(image.Width);
int y1 = rand.Next(image.Height);
int y2 = rand.Next(image.Height);
g.DrawLine(new Pen(Color.Silver), x1, x2, y1, y2);
}
//定义颜色
Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
//定义字体
string[] font = { "Verdana", "Microsoft Sans Serif",
"Comic Sans MS", "Arial", "宋体","新宋体" }; //随机输出噪点
for (int i = 0; i < 50; i++)
{
int x = rand.Next(image.Width);
int y = rand.Next(image.Height);
g.DrawRectangle(new Pen(Color.LightGray, 0), x, y, 1, 1);
} //输出不同字体和颜色的验证码字符
for (int i = 0; i < CheckCode.Length; i++)
{
int cindex = rand.Next(7);
int findex = rand.Next(6);
Font f = new System.Drawing.Font(font[findex], 12, System.Drawing.FontStyle.Bold);
Brush b = new System.Drawing.SolidBrush(c[cindex]);
int ii = 4;
if ((i + 1) % 2 == 0)
{
ii = 2;
}
g.DrawString(CheckCode.Substring(i, 1), f, b, 3 + (i * 12), ii);
}
//画一个边框
g.DrawRectangle(new Pen(Color.Black, 0), 0, 0, image.Width - 1, image.Height - 1);
//输出到浏览器
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ContentType = "image/Png";
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
g.Dispose();
image.Dispose(); }
/// <summary>
/// 根据指定的数量生成随机字符串
/// </summary>
/// <param name="n"></param>
/// <returns></returns>
public string RandomNum(int n)
{
string strchar = "1,2,3,4,5,6,7,8,9,0,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
string[] VcArray = strchar.Split(',');
string VNum = "";
int temp = -1;
Random rand = new Random();
for (int i = 1; i < n + 1; i++)
{
if (temp != -1)
{
rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
}
int t = rand.Next(61);
if (temp != -1 && temp == t)
{
return RandomNum(n);
}
temp = t;
VNum += VcArray[t];
}
return VNum;
} public bool IsReusable
{
get
{
return false;
}
}
}这个是一般处理程序 获取带带数字图片的验证码
<head id="Head1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
BODY
{
font-size: 12px;
color: #ffffff;
font-family: 宋体;
}
TD
{
font-size: 12px;
color: #ffffff;
font-family: 宋体;
}
</style>
<meta content="MSHTML 6.00.6000.16809" name="GENERATOR">
</head>
<body>
<form id="form1" runat="server">
<contenttemplate>
<div id="UpdatePanel2">
<div id="div1"
style="left: 0px; position: absolute; top: 0px; background-color: #0066ff">
</div>
<div id="div2"
style="left: 0px; position: absolute; top: 0px; background-color: #0066ff">
</div> <script language="javascript"> var speed = 20;
var temp = new Array();
var clipright = document.body.clientWidth / 2, clipleft = 0
for (i = 1; i <= 2; i++) {
temp[i] = eval("document.all.div" + i + ".style");
temp[i].width = document.body.clientWidth / 2;
temp[i].height = document.body.clientHeight;
temp[i].left = (i - 1) * parseInt(temp[i].width);
}
function openit() {
clipright -= speed;
temp[1].clip = "rect(0 " + clipright + " auto 0)";
clipleft += speed;
temp[2].clip = "rect(0 auto auto " + clipleft + ")";
if (clipright <= 0)
clearInterval(tim);
}
tim = setInterval("openit()", 100);
</script> <script type="text/javascript"> function ChangeImg() {
i++;
document.getElementById("img").src = "../HandlerCheck.ashx?i=" + i;
}
// function ChanageImage() {
// i++;
// document.getElementById("ctl00_ContentPlaceHolder1_img").src = "../ValidateCodeHandler.ashx?i=" + i;
// }
</script> <div>
<table align="center" border="0" cellpadding="0"
cellspacing="0" width="900">
<tr>
<td style="height: 105px">
<img border="0" src="../Images/login_1.gif" />
</td>
</tr>
<tr>
<td background="../Images/login_2.jpg" height="300">
<table border="0" cellpadding="0" height="300"
width="900">
<tr>
<td colspan="2" height="35">
</td>
</tr>
<tr>
<td width="360">
</td>
<td>
<table border="0" cellpadding="2" cellspacing="0">
<tr>
<td style="height: 28px" width="80">
登 录 名:
</td>
<td style="height: 28px" width="150">
<asp:TextBox ID="txtName" runat="server" name="txtName"
Style="width: 130px"></asp:TextBox>
</td>
<td style="height: 28px" width="370">
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtName" Display="Dynamic"
ErrorMessage="用户名不能为空!"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td style="height: 28px">
登录密码:
</td>
<td style="height: 28px">
<asp:TextBox ID="txtPwd" runat="server" name="txtName"
Style="width: 130px" TextMode="Password"></asp:TextBox>
</td>
<td style="height: 28px">
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtPwd" Display="Dynamic"
ErrorMessage="密码不能为空!"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td style="height: 28px">
验证码:
</td>
<td style="height: 28px">
<asp:TextBox ID="txtcode" runat="server" name="txtcode"
Style="width: 130px"></asp:TextBox>
</td>
<td style="height: 28px">
<asp:Label ID="lberr" runat="server"></asp:Label>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="txtcode" Display="Dynamic"
ErrorMessage="验证码不能为空"></asp:RequiredFieldValidator>
<asp:Image ID="img" runat="server"
ImageUrl="~/HandlerCheck.ashx" onclick="ChangeImg()" />
<a href="#" onclick="ChangeImg()">看不清楚,换一个</a>
</td>
</tr>
<tr>
<td style="height: 18px">
</td>
<td style="height: 18px">
</td>
<td style="height: 18px">
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:ImageButton ID="IbtnLogin" runat="server"
ImageUrl="~/Images/login_button.gif" OnClick="IbtnLogin_Click" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<img border="0" src="../Images/login_3.jpg" />
</td>
</tr>
</table>
</div>
</div>
</contenttemplate>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using Model;
public partial class Admin_Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void IbtnLogin_Click(object sender, ImageClickEventArgs e)
{ //首先检测验证码是否通过
if (txtcode.Text.ToLower() == ((string)Session["CheckCode"]).ToLower())
{
//检测用户名 和密码是否匹配
if (BLL.UserInfo.Validate(txtName.Text, txtPwd.Text))
{
Model.UserInfo u = BLL.UserInfo.GetUserInfoByUserName(txtName.Text.Trim());
Session["username"] = u.UserName;
Model.UserRoleInfo role = BLL.UserRoleInfo.GetUserRoleNameByUserRoleID((int)u.UserRoleID);
Session["roleName"] = role.RoleName;
Session["roleId"] = role.UserRoleID;
Response.Redirect("~/Main.aspx");
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('用户名或密码有误');", true);
}
}
else
{
lberr.Text = "验证码不匹配";
lberr.ForeColor = Color.Red;
} }
}
function show(im)
{
im.src="ValidateCode1.aspx?"+new Date;
}
</script>ValidateCode1.aspx为验证码文件
<img src="ValidateCode1.aspx" onclick ="show(this)" /></td>
假设你费尽心机抄来了“使用到ajax”的代码,我要告诉你,这只是一个锦上添花的摆设。不管有没有这类代码,在你服务器端真正进行用户登录时,在登录那个程序中要首先再次在纯粹服务器端调用验证程序,而什么ajax程序的结果都可以完全作废不考虑。