电竞比分网-中国电竞赛事及体育赛事平台

分享

ASP.NET中動(dòng)態(tài)生成驗(yàn)證碼的一則方法

 熱血奇跡 2013-04-11

現(xiàn)在不少網(wǎng)站中都使用了驗(yàn)證碼的技術(shù),實(shí)現(xiàn)方式也是多種多樣,這里主要介紹ASP.NET中可以采用的一種動(dòng)態(tài)生成驗(yàn)證碼的方法,可能并不十分完美,但實(shí)現(xiàn)難度是屬于較低的。

該方法是利用了普通的動(dòng)態(tài)圖片生成技術(shù),但比較特別的一點(diǎn)是圖片的生成是在一個(gè)Page類(lèi)型的子類(lèi)的Page_Load方法中執(zhí)行的。所以Response的ContentType為image/Gif,而非text/html。

GraphicalText.aspx.cs代碼:

復(fù)制代碼
using System;
using System.Drawing;
using System.Drawing.Imaging;
 
namespace WebApplication1
{
    public partial class GraphicalText : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            using (Bitmap image = new Bitmap(30, 20))
            {
                using (Graphics g = Graphics.FromImage(image))
                {
                    g.FillRectangle(Brushes.Yellow, 0, 0, 30, 20);
                    g.DrawRectangle(Pens.Red, 0, 0, 29, 19);
                    Font f = new Font("Arial", 9, FontStyle.Italic);
                    string code = Request.QueryString["code"];
                    g.DrawString(code, f, Brushes.Blue, 0, 0);
                    Response.ContentType = "image/Gif";
                    image.Save(Response.OutputStream, ImageFormat.Gif);
                }
            }
        }
    }
}
復(fù)制代碼

注意,必須要加上這句代碼——“Response.ContentType = “image/Gif”;”,否則在IE之外的瀏覽器中無(wú)法正確顯示。

對(duì)應(yīng)的GraphicalText.aspx代碼很簡(jiǎn)單,只有一行,因?yàn)椴恍枰蠬TML的輸出,例如:http://:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GraphicalText.aspx.cs" Inherits="WebApplication1.GraphicalText" %>

在主頁(yè)面中關(guān)鍵要將圖片的ImageUrl賦值為之前的頁(yè)面地址,并且為了令圖片內(nèi)容發(fā)生變化,將4位隨機(jī)數(shù)字作為它的參數(shù)。

Default.aspx.cs代碼:

復(fù)制代碼
using System;
using System.Text;
 
namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder("~/GraphicalText.aspx?code=");
            Random d = new Random();
            sb.Append((char)d.Next(48, 58));
            sb.Append((char)d.Next(48, 58));
            sb.Append((char)d.Next(48, 58));
            sb.Append((char)d.Next(48, 58));
            Image1.ImageUrl = sb.ToString();
        }
    }
}
復(fù)制代碼

最后在頁(yè)面中加上必要的驗(yàn)證代碼,所有工作就都完成了。

http://的Default.aspx代碼:

復(fù)制代碼
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www./1999/xhtml" >
<head runat="server">
    <title>http://</title>
<script type="text/javascript">
    function ClientValidate(sender, args) {
        var url = form1.Image1.src;
        var index = url.lastIndexOf("=");
        var code = url.substring(index + 1);
        if (code == form1.TextBox1.value) {
            args.IsValid = true;
        }
        else {
            args.IsValid = false;
        }
    }
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1"  runat="server">
                <ContentTemplate>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    <asp:Image ID="Image1" runat="server"/>
                    <asp:Button ID="Button1" runat="server" Text="刷新"  />                              
                    <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="驗(yàn)證碼錯(cuò)誤!"
                        ControlToValidate="TextBox1" ClientValidationFunction="ClientValidate" ValidateEmptyText="true" ValidationGroup="validation"></asp:CustomValidator>                              
                </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    <div>
        <asp:Button ID="Button2" runat="server" Text="提交" ValidationGroup="validation"/>//http://
    </div>
    </form>
</body>
</html>
復(fù)制代碼

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀(guān)點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多