熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

ASP.NET 做驗證碼

2022-06-13   來源: .NET編程 

  顯示驗證碼的界面
前台
<td class=style>
                    驗證碼</td>
                <td class=style>
                    <asp:TextBox ID=TextBox runat=server Height=px></asp:TextBox>
                    <asp:Image ID=Image runat=server ImageUrl=~/Defaultaspx/>
                    &nbsp;<asp:HyperLink ID=HyperLink runat=server
                        NavigateUrl=~/Defaultaspx>看不清換張圖</asp:HyperLink>
                </td>
後台
using System;
using SystemCollectionsGeneric;
using SystemLinq;
using SystemWeb;
using SystemWebUI;
using SystemWebUIWebControls;
using SystemData;
using SystemDataSqlClient;
using SystemConfiguration;
public partial class _Default : SystemWebUIPage
{
    protected void Page_Load(object sender EventArgs e)
    {

  }
    protected void Button_Click(object sender EventArgs e)
    {
        string str = ConfigurationManagerConnectionStrings[sqlstr]ConnectionString;
        using (SqlConnection sqlCnn=new SqlConnection(str))
        {
            using (SqlCommand sqlCmm=sqlCnnCreateCommand())
            {
                sqlCmmCommandText = select * from Login;
                sqlCnnOpen();
                SqlDataReader reader = sqlCmmExecuteReader();
                bool bl = false;
                if (reader!=null)
                {
                    while (readerRead())
                    {
                        if (thisTextBoxText == reader[name]ToString() && thisTextBoxText == reader[password]ToString() && thisTextBoxText == Session[code]ToString())
                        {
                            bl = true;
                        }
                    }
                    if (bl==true)
                    {
                        ClientScriptRegisterClientScriptBlock(GetType() 提示 <script>alert(登陸成功!)</script>);
                    }
                    else
                    {
                        ClientScriptRegisterClientScriptBlock(GetType() 提示 <script>alert(登陸失敗!)</script>);
                    }
                }
            }
        }
    }
}
做驗證碼的界面
using System;
using SystemCollectionsGeneric;
using SystemLinq;
using SystemWeb;
using SystemWebUI;
using SystemWebUIWebControls;
using SystemDrawing;
using SystemIO;
using SystemText;
public partial class Default : SystemWebUIPage
{
    protected void Page_Load(object sender EventArgs e)
    {
        SystemDrawingImage img = new Bitmap();
        Graphics g = GraphicsFromImage(img);
        thisDealImage(img);
        gDrawLine(PensYellow );
        string code = thisGenerateCode();

  Font font = new Font(宋體 FontStyleBold | FontStyleItalic | FontStyleStrikeout | FontStyleUnderline);
        Session[code] = code;                                 //保存到Session中
        gDrawString(codefontBrushesYellowGreen);
        thisResponseClear();
        MemoryStream ms = new MemoryStream();
        imgSave(msSystemDrawingImagingImageFormatJpeg);
        thisResponseBinaryWrite(msToArray());
        thisResponseFlush();
        thisResponseEnd();

  }
    private void DealImage(SystemDrawingImage imgint nums)
    {
        Bitmap b = img as Bitmap;
        Random ran = new Random();
        for (int i = ; i < nums; i++)
        {
            bSetPixel(ranNext( imgWidth) ranNext(imgHeight)ColorWhite);
        }
    }
    private string GenerateCode()   //產生隨機數
    {
        Random ran = new Random(DateTimeNowMillisecond);
        StringBuilder sb = new StringBuilder();
        for (int i  = ; i  <; i ++)
        {
            sbAppend(ranNext());
        }
        return sbToString();
    }
}


From:http://tw.wingwit.com/Article/program/net/201311/12053.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.