• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#之asp.net及MVC生成动态验证码:

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

1.生成验证码字符串

// 随机生成指定长度的验证码字符串
private
string RandomCode(int length) { string s = "0123456789zxcvbnmasdfghjklqwertyuiop"; StringBuilder sb = new StringBuilder(); Random rand = new Random(); int index; for(int i = 0; i < length; i++) { index = rand.Next(0, s.Length); sb.Append(s[index]); } return sb.ToString(); }

 2.绘制干扰线

private void PaintInterLine(Graphics g,int num,int width,int height)
{
    Random r = new Random();
    int startX, startY, endX, endY;
    for(int i = 0; i < num; i++)
    {
        startX = r.Next(0, width);
        startY = r.Next(0, height);
        endX = r.Next(0, width);
        endY = r.Next(0, height);
        g.DrawLine(new Pen(Brushes.Red), startX, startY, endX, endY);
    }
}

 

3.生成验证码

public ActionResult GetValidateCode()
{
    byte[] data = null;
    string code = RandomCode(5);
    TempData["code"] = code;
    //定义一个画板
    MemoryStream ms = new MemoryStream();
    using(Bitmap map=new Bitmap(100, 40))
    {
        //画笔,在指定画板画板上画图
        //g.Dispose();
        using (Graphics g = Graphics.FromImage(map))
        {
            g.Clear(Color.White);
            g.DrawString(code,new Font("黑体",18.0F),Brushes.Blue,new Point(10,8));
            //绘制干扰线
            PaintInterLine(g, 30, map.Width, map.Height);
        }
        map.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
    }
    data = ms.GetBuffer();
    return File(data, "image/jpeg");
}

 

4.前段获取验证码

<form method="post" id="form1" action="/ValidateCode/login">
    <div class="code">
        <input type="text" name="code" />
        <img id="code" src="/ValidateCode/GetValidateCode/" />
        <a style="text-decoration:none; cursor:pointer" id="chCode">看不清?换一个</a>
    </div>
    <div >
        <input type="submit"  value="登录" />
    </div>
</form>

 

 5.后台验证

public ActionResult Login()
{
    string code = Request.Form["code"].ToString();
    if (string.IsNullOrEmpty(code))
    {
        return Content("验证输不能为空");
    }
    if (!code.Equals(TempData["code"]))
    {
        return Content("验证输不正确");
    }
    return Content("验证输入正确");
}
 

 

1.生成验证码字符串

// 随机生成指定长度的验证码字符串
private
string RandomCode(int length) { string s = "0123456789zxcvbnmasdfghjklqwertyuiop"; StringBuilder sb = new StringBuilder(); Random rand = new Random(); int index; for(int i = 0; i < length; i++) { index = rand.Next(0, s.Length); sb.Append(s[index]); } return sb.ToString(); }

 2.绘制干扰线

private void PaintInterLine(Graphics g,int num,int width,int height)
{
    Random r = new Random();
    int startX, startY, endX, endY;
    for(int i = 0; i < num; i++)
    {
        startX = r.Next(0, width);
        startY = r.Next(0, height);
        endX = r.Next(0, width);
        endY = r.Next(0, height);
        g.DrawLine(new Pen(Brushes.Red), startX, startY, endX, endY);
    }
}

 

3.生成验证码

public ActionResult GetValidateCode()
{
    byte[] data = null;
    string code = RandomCode(5);
    TempData["code"] = code;
    //定义一个画板
    MemoryStream ms = new MemoryStream();
    using(Bitmap map=new Bitmap(100, 40))
    {
        //画笔,在指定画板画板上画图
        //g.Dispose();
        using (Graphics g = Graphics.FromImage(map))
        {
            g.Clear(Color.White);
            g.DrawString(code,new Font("黑体",18.0F),Brushes.Blue,new Point(10,8));
            //绘制干扰线
            PaintInterLine(g, 30, map.Width, map.Height);
        }
        map.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
    }
    data = ms.GetBuffer();
    return File(data, "image/jpeg");
}

 

4.前段获取验证码

<form method="post" id="form1" action="/ValidateCode/login">
    <div class="code">
        <input type="text" name="code" />
        <img id="code" src="/ValidateCode/GetValidateCode/" />
        <a style="text-decoration:none; cursor:pointer" id="chCode">看不清?换一个</a>
    </div>
    <div >
        <input type="submit"  value="登录" />
    </div>
</form>

 

 5.后台验证

public ActionResult Login()
{
    string code = Request.Form["code"].ToString();
    if (string.IsNullOrEmpty(code))
    {
        return Content("验证输不能为空");
    }
    if (!code.Equals(TempData["code"]))
    {
        return Content("验证输不正确");
    }
    return Content("验证输入正确");
}

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
c#水晶报表总结发布时间:2022-07-10
下一篇:
C#实现自动升级(附源码)发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap