小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

.net實(shí)現(xiàn)驗(yàn)證碼功能

 櫻啊小櫻啊 2014-02-24
三、具體實(shí)例 
  (實(shí)例1) 

<body>
    <form id="form1" runat="server">
    <div>
       <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Image ID="Image1" runat="server" ImageUrl="png.aspx" /><br />
        <br />
       <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button" />
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
            <asp:ListItem Value="3">默認(rèn)</asp:ListItem>
            <asp:ListItem Value="1">文字</asp:ListItem>
            <asp:ListItem Value="2">數(shù)字</asp:ListItem>
            <asp:ListItem Value="3">混合</asp:ListItem>
        </asp:DropDownList></div>
    </form>
</body>


        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (DropDownList1.SelectedValue)
            {
                case "1":
                    Image1.ImageUrl = "png.aspx?aa=1";
                    break;
                case "2":
                    Image1.ImageUrl = "png.aspx?aa=2";
                    break;
                case "3":
                    Image1.ImageUrl = "png.aspx?aa=3";
                    break;
            }
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text == Session["gif"].ToString())
                Response.Write("OK,正確");
            else
                Response.Write("驗(yàn)證碼不符合");
        }


    public partial class png : System.Web.UI.Page
    {         
        protected void Page_Load(object sender, EventArgs e)
        {
            switch (Request.QueryString["aa"])
            {
                case "1":
                    gif = stxt(10);
                    Session["gif"] = stxt(10);
                    break;
                case "2":
                    gif = GetRandomint(4);
                    Session["gif"] = GetRandomint(4);
                    break;
                case "3":
                    gif = CreateRandomCode(4);
                    Session["gif"] = CreateRandomCode(4);
                    break;
                default:
                    gif = CreateRandomCode(4);
                    Session["gif"] = CreateRandomCode(4);
                    break;
            }
            CreateImage(Session["gif"].ToString());
        }

        private String GetRandomint(int codeCount)
        {
            Random random = new Random();
            string min = "";
            string max = "";
            for (int i = 0; i < codeCount; i++)
            {
                min +="1";
                max+="9";
            }
                return (random.Next(Convert.ToInt32(min),Convert.ToInt32(max)).ToString());
        }
        /**/
        /* 
    此函數(shù)在漢字編碼范圍內(nèi)隨機(jī)創(chuàng)建含兩個元素的十六進(jìn)制字節(jié)數(shù)組,每個字節(jié)數(shù)組代表一個漢字,并將 
    四個字節(jié)數(shù)組存儲在object數(shù)組中。 
    參數(shù):strlength,代表需要產(chǎn)生的漢字個數(shù) 
    
*/
        public static object[] CreateRegionCode(int strlength)
        {
            //定義一個字符串?dāng)?shù)組儲存漢字編碼的組成元素 
            string[] rBase = new String[16] { "0""1""2""3""4""5""6""7""8""9""a""b""c""d""e""f" };
            Random rnd = new Random();
            //定義一個object數(shù)組用來 
            object[] bytes = new object[strlength];

            /**/
            /*每循環(huán)一次產(chǎn)生一個含兩個元素的十六進(jìn)制字節(jié)數(shù)組,并將其放入bject數(shù)組中 
         每個漢字有四個區(qū)位碼組成 
         區(qū)位碼第1位和區(qū)位碼第2位作為字節(jié)數(shù)組第一個元素 
         區(qū)位碼第3位和區(qū)位碼第4位作為字節(jié)數(shù)組第二個元素 
        
*/
            for (int i = 0; i < strlength; i++)
            {
                //區(qū)位碼第1位 
                int r1 = rnd.Next(1114);
                string str_r1 = rBase[r1].Trim();

                //區(qū)位碼第2位 
                rnd = new Random(r1 * unchecked((int)DateTime.Now.Ticks) + i);//更換隨機(jī)數(shù)發(fā)生器的種子避免產(chǎn)生重復(fù)值 
                int r2;
                if (r1 == 13)
                {
                    r2 = rnd.Next(07);
                }
                else
                {
                    r2 = rnd.Next(016);
                }
                string str_r2 = rBase[r2].Trim();

                //區(qū)位碼第3位 
                rnd = new Random(r2 * unchecked((int)DateTime.Now.Ticks) + i);
                int r3 = rnd.Next(1016);
                string str_r3 = rBase[r3].Trim();

                //區(qū)位碼第4位 
                rnd = new Random(r3 * unchecked((int)DateTime.Now.Ticks) + i);
                int r4;
                if (r3 == 10)
                {
                    r4 = rnd.Next(116);
                }
                else if (r3 == 15)
                {
                    r4 = rnd.Next(015);
                }
                else
                {
                    r4 = rnd.Next(016);
                }
                string str_r4 = rBase[r4].Trim();

                //定義兩個字節(jié)變量存儲產(chǎn)生的隨機(jī)漢字區(qū)位碼 
                byte byte1 = Convert.ToByte(str_r1 + str_r2, 16);
                byte byte2 = Convert.ToByte(str_r3 + str_r4, 16);
                //將兩個字節(jié)變量存儲在字節(jié)數(shù)組中 
                byte[] str_r = new byte[] { byte1, byte2 };

                //將產(chǎn)生的一個漢字的字節(jié)數(shù)組放入object數(shù)組中 
                bytes.SetValue(str_r, i);
            }
            return bytes;
        }
        private string stxt(int num)
        {
            Encoding gb = Encoding.GetEncoding("gb2312");

            //調(diào)用函數(shù)產(chǎn)生10個隨機(jī)中文漢字編碼 
            object[] bytes = CreateRegionCode(num);
            string strtxt = "";

            //根據(jù)漢字編碼的字節(jié)數(shù)組解碼出中文漢字 
            for (int i = 0; i < num; i++)
            {
                strtxt += gb.GetString((byte[])Convert.ChangeType(bytes[i], typeof(byte[])));
            }
            return strtxt;
        }
        /// <summary>
        
/// 這個是使用字母,數(shù)字混合
        
/// </summary>
        
/// <param name="VcodeNum"></param>
        
/// <returns></returns>
        private string CreateRandomCode(int codeCount)
        {
            string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
            string[] allCharArray = allChar.Split(',');
            string randomCode = "";
            int temp = -1;
            Random rand = new Random();
            for (int i = 0; i < codeCount; i++)
            {
                if (temp != -1)
                {
                    rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
                }
                int t = rand.Next(61);
                if (temp == t)
                {
                    return CreateRandomCode(codeCount);
                }
                temp = t;
                randomCode += allCharArray[t];
            }
            return randomCode;
        }

        private void CreateImage(string checkCode)
        {
            int iwidth = (int)(checkCode.Length * 20);
            System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 25);
            Graphics g = Graphics.FromImage(image);
            Font f = new System.Drawing.Font("Arial"10, System.Drawing.FontStyle.Bold);
            Brush b = new System.Drawing.SolidBrush(Color.White);
            //g.FillRectangle(new System.Drawing.SolidBrush(Color.Blue),0,0,image.Width, image.Height);    
            g.Clear(Color.Black);
            g.DrawString(checkCode, f, b, 33);
            Pen blackPen = new Pen(Color.Black, 0);
            Random rand = new Random();
            //for (int i=0;i<5;i++)    
            
//{        
            
//    int y = rand.Next(image.Height);  
            
//    g.DrawLine(blackPen,0,y,image.W

  (實(shí)例2):20101018續(xù)加
復(fù)制代碼
        <script language="Javascript">
        
function Regist()
        {
            
var url = "";
            url 
= "/GZPI/Service/Registry/ViewProtocol.aspx";
            window.open(url);    
            
return false;
        }
        
function LoginClick()
        {
            
if(document.Login.UserName.value == "")
            {
                alert(
"用戶名不能為空!");
                
return false;
            }
            document.Login.submit();
            document.Login.Password.value 
= "";
            
return false;
        }
        
        
function f_refreshtype()
        {    
            
var Image1 = document.getElementById("IMGCheckCode");
              
if (Image1 != null
               {
                   Image1.src 
= Image1.src + "?";
             }
        }        
        
</script>
                        <TABLE cellSpacing="0" cellPadding="5" width="100%" border="0">
                            <TR>
                                <TD class="fontblack" align="right" width="30%">用 戶 名:</TD>
                                <TD vAlign="bottom" align="left" width="62%"><input id="Text1" style="BORDER-RIGHT: #43a7e2 1px solid; BORDER-TOP: #43a7e2 1px solid; BACKGROUND: #fcfcfc; BORDER-LEFT: #43a7e2 1px solid; BORDER-BOTTOM: #43a7e2 1px solid"
                                        size
="13" name="UserName" width="100px">
                                </TD>
                            </TR>
                            <TR>
                                <TD class="fontblack" align="right" height="23">密    碼:</TD>
                                <TD vAlign="bottom" align="left" height="23"><input id="Password1" style="BORDER-RIGHT: #43a7e2 1px solid; BORDER-TOP: #43a7e2 1px solid; BACKGROUND: #fcfcfc; BORDER-LEFT: #43a7e2 1px solid; BORDER-BOTTOM: #43a7e2 1px solid"
                                        type
="password" size="13" name="Password" width="100px">
                                </TD>
                            </TR>
                            <TR>
                                <TD class="fontblack" align="right" height="23"> 驗(yàn) 碼:</TD>
                                <TD valign="top" align="left" height="23"><FONT face="宋體"><INPUT id="CheckCode" style="BORDER-RIGHT: #43a7e2 1px solid; BORDER-TOP: #43a7e2 1px solid; BACKGROUND: #fcfcfc; BORDER-LEFT: #43a7e2 1px solid; BORDER-BOTTOM: #43a7e2 1px solid"
                                            width
="100px" size="13" name="CheckCode"> </FONT><style="CURSOR: hand" onclick="f_refreshtype()" title="看不清楚?點(diǎn)擊獲取新的校驗(yàn)碼!" border="0"><IMG id="IMGCheckCode" src="../GenerateCheckCode.aspx"></a>
                                </TD>
                            </TR>
                            <TR vAlign="bottom" align="center">
                                <TD class="dlbuttom" colSpan="2" height="23"><input id="Image1" style="CURSOR: hand" onclick="return LoginClick();" tabIndex="3" type="image"
                                        height
="17" width="55" src="images/login.gif" border="0" name="OkButton">   
                                    <input type="image" onClick="return Regist();" border='0' src='images/zuce.gif' width='55'
                                        
height='17' ID="Image2" NAME="Image2">
                                </TD>
                            </TR>
                        </TABLE>
復(fù)制代碼

 


復(fù)制代碼
    private void OK_Click(object sender, System.EventArgs e)
        {
            if (!CheckCodeSame(CheckCode.Text.Trim()))
            {                    
                System.Web.HttpContext.Current.Response.Write("<Script Language='JavaScript'>window.alert('校驗(yàn)碼錯誤!請重新輸入!');window.close();</script>");
                return;
            }        
            int userType = 1;
            try
            {
                userType = int.Parse(UserType.Text);
            }
            catch
            {
            }

            GatewayManager gateway = new GatewayManager(corpCode.Text,collegeCode.Text,chargeCode.Text,userName.Text,password.Text,userType);
            gateway.Login(this);
        }

        /// <summary>
        /// 判斷輸入的校驗(yàn)碼是否正確
        /// </summary>
        /// <param name="checkCode"></param>
        /// <returns></returns>
        public static bool CheckCodeSame(string checkCode)
        {
            bool bSame = false; ;

            if (HttpContext.Current.Session["CheckCode"] != null)
            {
                if (checkCode.ToUpper() == HttpContext.Current.Session["CheckCode"].ToString().ToUpper())
                {
                    bSame = true;
                }
            }
            return bSame;
        }
復(fù)制代碼

 

  
復(fù)制代碼
<HTML>
    <HEAD>
        <title>GenerateCheckCode</title>
        <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
        <meta name="CODE_LANGUAGE" Content="C#">
        <meta name="vs_defaultClientScript" content="JavaScript">
        <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body>
        <form id="form1" runat="server">
            <div><FONT face="宋體"></FONT>
            </div>
        </form>
    </body>
</HTML>
復(fù)制代碼

 


復(fù)制代碼
using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Drawing2D;


namespace GZPI.Gateway
{
    /// <summary>
    
/// GenerateCheckCode 的摘要說明。
    
/// </summary>
    public class GenerateCheckCode : System.Web.UI.Page
    {
        private void Page_Load(object sender, System.EventArgs e)
        {
            // 在此處放置用戶代碼以初始化頁面
            this.CreateCheckCodeImage(GenerateCheckCodes(4));
        }

        #region Web 窗體設(shè)計器生成的代碼
        override protected void OnInit(EventArgs e)
        {
            //
            
// CODEGEN: 該調(diào)用是 ASP.NET Web 窗體設(shè)計器所必需的。
            
//
            InitializeComponent();
            base.OnInit(e);
        }
        
        /// <summary>
        
/// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改
        
/// 此方法的內(nèi)容。
        
/// </summary>
        private void InitializeComponent()
        {    
            this.Load += new System.EventHandler(this.Page_Load);
        }
        #endregion

        private string GenerateCheckCodes(int iCount)
        {
            int number;
            string checkCode = String.Empty;
            int iSeed = DateTime.Now.Millisecond;
            System.Random random = new Random(iSeed);
            for (int i = 0; i < iCount; i++)
            {
                number = random.Next(10);
                checkCode += number.ToString();
            }

            Session["CheckCode"] = checkCode;
            return checkCode;
        }



        private void CreateCheckCodeImage(string checkCode)
        {
            if (checkCode == null || checkCode.Trim() == String.Empty)
                return;
            int iWordWidth = 15;
            int iImageWidth = checkCode.Length * iWordWidth;
            Bitmap image = new Bitmap(iImageWidth, 20);
            Graphics g = Graphics.FromImage(image);
            try
            {
                //生成隨機(jī)生成器 
                Random random = new Random();
                //清空圖片背景色 
                g.Clear(Color.White);

                //畫圖片的背景噪音點(diǎn)
                for (int i = 0; i < 20; i++)
                {
                    int x1 = random.Next(image.Width);
                    int x2 = random.Next(image.Width);
                    int y1 = random.Next(image.Height);
                    int y2 = random.Next(image.Height);
                    g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
                }

                //畫圖片的背景噪音線 
                for (int i = 0; i < 2; i++)
                {
                    int x1 = 0;
                    int x2 = image.Width;
                    int y1 = random.Next(image.Height);
                    int y2 = random.Next(image.Height);
                    if (i == 0)
                    {
                        g.DrawLine(new Pen(Color.Gray, 2), x1, y1, x2, y2);
                    }
            
                }


                for (int i = 0; i < checkCode.Length; i++)
                {

                    string Code = checkCode[i].ToString();
                    int xLeft = iWordWidth * (i);
                    random = new Random(xLeft);
                    int iSeed = DateTime.Now.Millisecond;
                    int iValue = random.Next(iSeed) % 4;
                    if (iValue == 0)
                    {
                        Font font = new Font("Arial"13, (FontStyle.Bold | System.Drawing.FontStyle.Italic));
                        Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height);
                        LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Blue, Color.Red, 1.5ftrue);
                        g.DrawString(Code, font, brush, xLeft, 2);
                    }
                    else if (iValue == 1)
                    {
                        Font font = new System.Drawing.Font("楷體"13, (FontStyle.Bold));
                        Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height);
                        LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Blue, Color.DarkRed, 1.3ftrue);
                        g.DrawString(Code, font, brush, xLeft, 2);
                    }
                    else if (iValue == 2)
                    {
                        Font font = new System.Drawing.Font("宋體"13, (System.Drawing.FontStyle.Bold));
                        Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height);
                        LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Green, Color.Blue, 1.2ftrue);
                        g.DrawString(Code, font, brush, xLeft, 2);
                    }
                    else if (iValue == 3)
                    {
                        Font font = new System.Drawing.Font("黑體"13, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Bold));
                        Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height);
                        LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Blue, Color.Green, 1.8ftrue);
                        g.DrawString(Code, font, brush, xLeft, 2);
                    }
                }
                //////畫圖片的前景噪音點(diǎn) 
                //for (int i = 0; i < 8; i++)
                
//{
                
//    int x = random.Next(image.Width);
                
//    int y = random.Next(image.Height);
                
//    image.SetPixel(x, y, Color.FromArgb(random.Next()));
                
//}
                
//畫圖片的邊框線 
                g.DrawRectangle(new Pen(Color.Silver), 00, image.Width - 1, image.Height - 1);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                Response.ClearContent();

                Response.BinaryWrite(ms.ToArray());
            }
            finally
            {
                g.Dispose();
                image.Dispose();
            }
        }
    }
}
復(fù)制代碼

 

  四、效果圖展示
  
驗(yàn)證碼數(shù)字

  驗(yàn)證碼數(shù)字+字母

  驗(yàn)證碼漢字


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

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多