ASP.NET验证码实例

                                                    效果图(点击验证码可以更换):

                          

 验证码的实现思路是:用一个页面产生编码,并输出,然后用一个页面接受.

ValidateCode.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Drawing;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _validate : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {   //生成验证码
        if (Session.Contents["validate"] == null)
            Session.Add("validate", null);
        Random rand = new Random();
        string vaildate = rand.Next(1000, 10000).ToString();
        Session.Contents["validate"] = vaildate;
        System.Drawing.Image image = new System.Drawing.Bitmap(30, 16);
        System.Drawing.Graphics formGraphice = System.Drawing.Graphics.FromImage(image);
        System.Drawing.Font font = new System.Drawing.Font("Arial", 10, FontStyle.Regular);//字体与大小
        System.Drawing.SolidBrush brush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
        formGraphice.Clear(System.Drawing.Color.White);
        System.Drawing.StringFormat format = new System.Drawing.StringFormat();
        formGraphice.DrawString(vaildate, font, brush, 0, 00, format);
        font.Dispose();
        brush.Dispose();
        formGraphice.Dispose();
        image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
    }
}

ValidateCode.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ValidateCode.aspx.cs" Inherits="ValidateCode" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Image ID="Image1" runat="server" Height="32px" Style="z-index: 100; left: 328px;
            position: absolute; top: 176px" Width="80px" ImageUrl ="~/ValidateCode.aspx" />
        &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            </div>
    </form>
</body>
</html>

Default页面接受验证码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>无标题页</title>

    <link href="../App_Themes/Style.css" rel="stylesheet" type="text/css" />
    <link href="../css/Css.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">

    <div style="width:569px; height:347px; border:green 1px solid; margin-top:80px;background-image:url('../images/admin.jpg')">
        <div style="margin-top:100px; margin-left:160px;">
            <div style="text-align:left;">用&nbsp;&nbsp;&nbsp;&nbsp;户:<asp:TextBox ID="Text_Admin" runat="server" Width="130px"></asp:TextBox></div>
            <div style="text-align:left;">密&nbsp;&nbsp;&nbsp;&nbsp;码:<asp:TextBox ID="Text_PassWord" runat="server" TextMode="Password" Width="130px"></asp:TextBox></div>
            <div style="text-align:left;">验证码:<asp:TextBox ID="Text_Validate" runat="server" MaxLength="4" Width="130px"></asp:TextBox><img alt="点击刷新" src="ValidateCode.aspx?time=' + Math.random();" style="cursor:pointer;" οnclick="this.src='validate.aspx?time=' + Math.random();" /></div>
            <div style="margin-top:10px; margin-left:-180px;">
                &nbsp;
                &nbsp;<asp:Button ID="button_Login" runat="server" Text="登 陆" Width="50px"  Height="20px" CssClass="ButtonCss" /> <asp:Button ID="CanCel" runat="server" CssClass="ButtonCss" Text ="修改密码" Width="60px" /></div>
    </div> 
    </form>
</body>
</html>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值