C#登录页面验证码刷新

验证码生成页面  png.aspx

using System;

using System.Collections.Generic;

using System.Drawing;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;



namespace Maticsoft.Web.Admin

{

    public partial class png : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            string checkCode = CreateRandomCode(4);

            Session["CheckCode"] = checkCode;

            CreateImage(checkCode);

        }

        private string CreateRandomCode(int codeCount)

        {

            // 函数功能:产生数字和字符混合的随机字符串        

            string allChar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

            char[] allCharArray = allChar.ToCharArray();

            string randomCode = "";

            Random rand = new Random();

            for (int i = 0; i < codeCount; i++)

            {

                int r = rand.Next(61);

                randomCode += allCharArray.GetValue(r);

            }

            return randomCode;

        }

        private void CreateImage(string checkCode)

        {

            // 生成图象验证码函数   

            int iwidth = (int)(checkCode.Length * 11.5);

            System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 20);

            Graphics g = Graphics.FromImage(image);

            Font f = new System.Drawing.Font("Arial", 10, System.Drawing.FontStyle.Bold);

            Brush b = new System.Drawing.SolidBrush(Color.Azure); //字母白色        

            //g.FillRectangle(new System.Drawing.SolidBrush(Color.Blue),0,0,image.Width, image.Height);       

            g.Clear(Color.Brown);//背景灰色     

            g.DrawString(checkCode, f, b, 3, 3);

            Pen blackPen = new Pen(Color.Black, 0);

            Random rand = new Random();

            System.IO.MemoryStream ms = new System.IO.MemoryStream();

            image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);



            Response.ClearContent();

            Response.ContentType = "image/Jpeg";

            Response.BinaryWrite(ms.ToArray());

            g.Dispose();

            image.Dispose();

        }

    }

}

 

登录页面放入图片

<asp:Image ID="Image1" runat="server" ImageUrl="png.aspx" onclick="refresh(this, 'png.aspx');"/>

js脚本

        function refresh()
        {
            var url = "png.aspx?id=";
            var r = Math.random() * 1000;
            url = url + r;
            document.all("Image1").src = url;
            document.all("LoI_txtVC").value = "";
            document.all("LoI_txtVC").focus();
            return false;
        }</script>

 

登录判断验证码  TextBox3为验证码输入框

if (base.Session["CheckCode"] == null)

            {

                Page.ClientScript.RegisterStartupScript(Page.ClientScript.GetType(), "myscripts", "<script>alert(\"验证码已过期!\");</script>");

                return;

            }

            if (TextBox3.Text != Session["CheckCode"].ToString())

            {

                Page.ClientScript.RegisterStartupScript(Page.ClientScript.GetType(), "myscripts", "<script>alert(\"验证码不正确!\");</script>");

                return;

            }

 

你可能感兴趣的:(验证码)