c ajax 验证码,asp.net ajax实现无刷新验证码

1、首先是在后台验证码的aspx文件的Page_Load中的事件代码: using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Drawing;

namespace 学生在线考试系统

{

public partial class AjaxAutoCode : System.Web.UI.Page

{

//验证数字

public string authcode = string.Empty;

protected void Page_Load(object sender, EventArgs e)

{

#region 第一种产生验证码的方法

Random random = new Random();

authcode = random.Next(1111, 9999).ToString();

//构造图片

Bitmap image = new Bitmap(authcode.Length * 12, 25);

//创建画布

Graphics g = Graphics.FromImage(image);

try

{

g.Clear(Color.White);

for (int i = 0; i < 25; i++)

{

int x1 = random.Next(image.Width);

int x2 = random.Next(image.Width);

int y1 = random.Next(image.Height);

int y2 = random.Next(image.Height);

//链接两点的线条

g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);

}

Font font = new Font("Arial", 12, FontStyle.Bold | FontStyle.Italic);

System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(

new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkBlue, 1.2f, true);

g.DrawString(authcode, font, brush, 2, 2);

//画图片的前景噪点

for (int i = 0; i < 100; i++)

{

int x = random.Next(image.Width);

int y = random.Next(image.Height);

image.SetPixel(x, y, Color.FromArgb(random.Next()));

}

g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);

System.IO.MemoryStream ms = new System.IO.MemoryStream();

image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);

ms.WriteTo(this.Response.OutputStream);

ms.Close();

this.Response.ContentType = "image/gif";

}

finally

{

image.Dispose();

g.Dispose();

}

#endregion

}

}

}

2、其次在显示验证码的页面定义一个JS函数function fGetCode()

{

document.getElementById("getcode").src="Default2.aspx?"+Math.random();

}

3.再编辑前台页面aspx,下面是前台页面的代码片段验证码

看不清楚?更换验证码

更多asp.net ajax实现无刷新验证码相关文章请关注PHP中文网!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
采用ajax技术实现的图形验证码,在前端进行验证。验证码信息为图片。每一行代码均有注释,通俗易懂。 实现步骤: 1、创建web工程 2、在jsp页面,编写一个 3、编写一个servlet,在servlet中产生图形验证码 ------------------------------------------------------------ //1、给客户端作出的回应是以图片的方式来回应 response.setContentType("image/jpeg"); //2、创建一个图形缓冲区,用于绘制图形 (宽度,高度,颜色的生成方案) BufferedImage image = new BufferedImage(800,600,BufferedImage.TYPE_INT_RGB); //3、创建一支画笔(图形设备接口)用于绘图 Graphics g = image.getGraphics(); //4、指定图笔的颜色 g.setColor(getColor(200,256)); //5、绘制一个矩形框,作为验证码的背景 g.fillRect(0,0, 800,600); //产生一个输出流,准备把图片以流的方式,输出到客户端 OutputStream out = response.getOutputStream(); //输出在图形缓冲区中,绘制的图片 ImageIO.write(image,"jpg",out); //关闭流 out.close(); //随机生成背景颜色 private Random rd = new Random(); //产生随机数类 public Color getColor(int start,int end){ int r = start+rd.nextInt(end-start); int g = start+rd.nextInt(end-start); int b = start+rd.nextInt(end-start); return new Color(r, g, b);//根据三原色的值,随机在指定范围内,生成一种颜色 } --------------------------------------------------------------------------- 0-120 比较适合文字的颜色 100-200 适合干扰线条的颜色 200-255 适合背景颜色 --------------------------------------------------------------------------- 生成图片中的文字: 1、先编写一个字符串,包含:数字,大小字母 private String s = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 2、在产生背景之后,编写如下代码,产生四个字符(并且,把生成字符,保存在session中,在登录的时候用来做判断) String code=""; //用于保存生成的四个字符 for(int i=0;i<4;i++){ //生成一个随机数,它的取值范围,一定要在s这个字符串的长度范围之内 int index = rd.nextInt(s.length()); //2 //把index作为下标,来取得字符串的中某一个字符 char c = s.charAt(index); //指定文字的颜色----深色段 g.setColor(getColor(0,120)); //创建一个字体 Font f = new Font("隶书",Font.ITALIC|Font.BOLD,60+rd.nextInt(60)); //把字体关联到画笔 g.setFont(f); code+=c; //把生成的字符连接成一个字符串 //把文字输出到图片上 g.drawString(String.valueOf(c), 100+i*80+rd.nextInt(100),200+rd.nextInt(150)); } request.getSession().setAttribute("code",code);//把生成的验证码信息,存储到session中,登录的时候,用来作判断 ------------------
ASP.NET可以利用Ajax技术实现刷新页面。Ajax是一种异步的JavaScript和XML通信技术,它可以在不刷新整个页面的情况下,向服务器提交或获取数据,并将返回的数据局部地更新到页面上。在ASP.NET中,可以通过以下步骤来实现刷新页面: 1. 引入jQuery库。 在ASP.NET页面中,需要通过<script>标签引入jQuery库,例如: ``` <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> ``` 2. 编写JavaScript代码。 通过jQuery库提供的$.ajax()方法,可以向服务器提交或获取数据,并在成功返回数据后更新页面。例如,下面的代码用于向服务器提交数据: ``` $.ajax({ type: "POST", url: "Handler.ashx", data: { name: "张三", age: 18 }, success: function (data) { $("#result").html(data); } }); ``` 其中,type表示提交数据的方式,可以是POST或GET;url表示提交数据的目标地址;data表示提交的数据,可以是JSON对象、字符串或FormData对象;success表示成功返回数据后的回调函数,data参数表示服务器返回的数据。 3. 编写服务器端代码。 在ASP.NET中,可以通过Generic Handler(通用处理程序)来处理Ajax请求。Generic Handler是一种不包含HTML标记的ASP.NET页面,可以处理各种类型的请求。例如,下面的代码用于处理上面的Ajax请求: ``` public class Handler : IHttpHandler { public void ProcessRequest(HttpContext context) { string name = context.Request.Form["name"]; int age = int.Parse(context.Request.Form["age"]); // 处理请求,返回数据 context.Response.Write("提交成功!"); } public bool IsReusable { get { return false; } } } ``` 在该代码中,通过Request.Form[]属性获取提交的数据,处理请求后通过Response.Write()方法返回数据。 通过以上步骤,就可以在ASP.NET中利用Ajax技术实现刷新页面了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值