asp.net登录验证码实现方法

时间:2021-05-28

前端添加的标签和方法:
验证码:复制代码 代码如下:<input id="txtVerifyCode" type="text" maxlength="5" style="line-height: 30px; height: 30px; width: 80px;border:solid 1px #d4d4d4;" class="input"/>&nbsp<img src="" alt="点击刷新" id="imgValidateCode" style="width: 100px; height: 30px; line-height: 30px; vertical-align: middle;" />&nbsp 点击图片刷新</p>//标签

$(function () { $("#imgValidateCode").click(function () { DoFresh(); }); DoFresh(); })function DoFresh() { var img = $("#imgValidateCode"); img.attr("src", "VerifyCode.aspx?random=" + Math.random()); } //添加的方法,src是生成图片的aspx的地址

然后在项目中在新建一个VerifyCode.aspx,下面是aspx的代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="VerifyCode.aspx.cs" Inherits="Form.VerifyCode" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ic Sans MS", "Arial", "宋体" }; for (int k = 0; k <= checkCode.Length - 1; k++) { int cindex = random.Next(7); int findex = random.Next(5); Font drawFont = new Font(f[findex], 26, (System.Drawing.FontStyle.Bold)); SolidBrush drawBrush = new SolidBrush(c[cindex]); float x = 5.0F; float y = 0.0F; float width = 42.0F; float height = 48.0F; int sjx = random.Next(10); int sjy = random.Next(image.Height - (int)height); RectangleF drawRect = new RectangleF(x + sjx + (k * 25), y + sjy, width, height); StringFormat drawFormat = new StringFormat(); drawFormat.Alignment = StringAlignment.Center; g.DrawString(checkCode[k], drawFont, drawBrush, drawRect, drawFormat); } //画图片的前景噪音点 for (int i = 0; i < 500; i++) { int x = random.Next(image.Width); int y = random.Next(image.Height); image.SetPixel(x, y, Color.FromArgb(random.Next())); } int cindex1 = random.Next(7); //画图片的边框线 g.DrawRectangle(new Pen(c[cindex1]), 0, 0, image.Width - 1, image.Height - 1); System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); Response.ClearContent(); Response.ContentType = "image/Gif"; Response.BinaryWrite(ms.ToArray()); } finally { g.Dispose(); image.Dispose(); } } }}

于是!就可以生成验证码了,然后自己再把编写验证版的判断逻辑写好就可以啦!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章