登陆带验证码

Posted Prozkb

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了登陆带验证码相关的知识,希望对你有一定的参考价值。

<script>--脚本代码
function getVCode() {
var vcode = document.getElementById("vcode");--获取元素
vcode.src = "ValidateCode.ashx?i="+Math.random();
}
</script>


<form id="form1" runat="server">
<div id="login">
<div class="title">用户登录</div>
<div class="content">
<p>用户名:<input type="text" name="txtuser" class="txt" /></p>
<p>密&nbsp;&nbsp;&nbsp;码:<input type="password" name="txtpwd" class="txt" /></p>

<p>验证码:<input type="text" name="txtvcode" class="txt" />
<img src="ValidateCode.ashx" id="vcode" align="middle" alt="看不清,换一张" onclick="getVCode()" /><%--后台生成的图片--%>
<a href="javascript:getVCode()" class="hyz">换一张</a>
</p>

<p><input type="submit" class="sub" value="登录" /></p>
</div>
</div>
</form>

 

 

using System.Drawing;
using System.Text;
using System.IO;
using System.Drawing.Drawing2D;
using System.Web.SessionState;

public class ValidateCode : IHttpHandler,IRequiresSessionState
{

public void ProcessRequest(HttpContext context)
{
Random random = new Random();
context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
//1.创建画板或画布
Bitmap bitmap = new Bitmap(60, 30);
//2.创建画图工具
Graphics g = Graphics.FromImage(bitmap);

//设置画布背景颜色
g.Clear(Color.White);
//设置边框
g.DrawRectangle(new Pen(Color.Red), 0, 0, 59, 29);
//设置像干扰素点,噪音点
for (int i = 0; i < 100; i++)
{
int x1 = random.Next(bitmap.Width);
int y1 = random.Next(bitmap.Height);
bitmap.SetPixel(x1, y1, Color.Brown);
}
//设置干扰折线,噪音线
for (int i = 0; i < 25; i++)
{
int x1 = random.Next(bitmap.Width);
int x2 = random.Next(bitmap.Width);
int y1 = random.Next(bitmap.Height);
int y2 = random.Next(bitmap.Height);
g.DrawLine(new Pen(Color.Green), x1, y1, x2, y2);
}
//3.设置画图内容
string s = "0123456789abcdefghjkmnpkrstuvwxyz";
StringBuilder builder = new StringBuilder();

for (int i = 0; i < 4; i++)
{
int index= random.Next(s.Length);
builder.Append(s[index]);
}
context.Session["vcode"] = builder;
//4.画图

g.DrawString(builder.ToString(), new Font("宋体", 18),new LinearGradientBrush(new Rectangle(0,0,bitmap.Width,bitmap.Height),Color.Blue,Color.Red,1.2f),0,0);

//5.保存
//string path=context.Request.MapPath("image");
//bitmap.Save(path + "/" + "11.jpg");
//context.Response.Write("生成成功!");
bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}

public bool IsReusable
{
get
{
return false;
}
}
}

以上是关于登陆带验证码的主要内容,如果未能解决你的问题,请参考以下文章

登陆带验证码

[转载]python实现带验证码网站的自动登陆

JSP 设计教师与学生不同登陆界面(带验证码)

php 模拟登陆(不带验证码)采集数据

vb.net如何使用HttpWebRequest模拟登陆带验证码的网站

js实现随机验证码(带干扰线)