Add verification code when. Net log in

Keywords: ASP.NET Session

1, ASPX login interface verification code

1. Login verification code picture and input verification code box

<asp:TextBox ID="txtValiCode" runat="server" Width="50px"></asp:TextBox>
<asp:Image ID="ValiCode" ImageUrl="CreateValiImg.aspx" runat="server" style="position:relative;top:4px;" />

Interface

 

 2,js

 $(function () {
            $("#loginBtn").click(function () {
                var Pwd = $("#PwdTbx").val();
                var md5pwd = $.md5(Pwd);
                $("#PwdTbx").val(md5pwd);

            });
            $("#txtValiCode").val("");

            $("#ValiCode").click(function () {
                location.reload();
            });
            
        });

3. Create ASPX page of production verification code CreateValiImg.aspx

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

Background code

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CreateValiImg : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string validateNum = CreateRandomNum(4);
        CreateImage(validateNum);
        Session["ValidateNum"] = validateNum;
    }

    //Random number of production
    private string CreateRandomNum(int NumCount)
    {
        string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,O,P,Q,R,S,T,U,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,m,n,o,p,q,s,t,u,w,x,y,z";
        string[] allCharArray = allChar.Split(',');//Split into arrays
        string randomNum = "";
        int temp = -1;                             //Record the value of the last random number and try to avoid generating several identical random numbers
        Random rand = new Random();
        for (int i = 0; i < NumCount; i++)
        {
            if (temp != -1)
            {
                rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
            }
            int t = rand.Next(35);
            if (temp == t)
            {
                return CreateRandomNum(NumCount);
            }
            temp = t;
            randomNum += allCharArray[t];


        }
        return randomNum;
    }
    //Production picture
    private void CreateImage(string validateNum)
    {
        if (validateNum == null || validateNum.Trim() == string.Empty)
            return;
        //generate BitMap image
        System.Drawing.Bitmap image = new System.Drawing.Bitmap(validateNum.Length * 12 + 12, 22);
        Graphics g = Graphics.FromImage(image);
        try
        {
            //Generate random generator
            Random random = new Random();
            //Clear picture background
            g.Clear(Color.White);
            //Draw the background noise line of the picture
            for (int i = 0; i < 25; 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, x2, y1, y2);
            }
            Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
            System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
            g.DrawString(validateNum, font, brush, 2, 2);
            //Picture foreground noise
            for (int i = 0; i < 100; i++)
            {
                int x = random.Next(image.Width);
                int y = random.Next(image.Height);
                image.SetPixel(x, y, Color.FromArgb(random.Next()));

            }
            //Draw the border line of the picture
            g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            //Save image to specified stream
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            Response.ClearContent();
            Response.ContentType = "image/Gif";
            Response.BinaryWrite(ms.ToArray());
        }
        finally
        {
            g.Dispose();
            image.Dispose();
        }
    }
}

4. Verify when running login and submitting account password verification code

 if (ValiCode != Session["ValidateNum"].ToString()){ ... }

2, Verification code used in MVC

1. Add the input verification code box and picture on the landing page

 Verification code < input id = "txtvalicode" type = "text" / >
       <img ID="ValiCode" src="/Login/CreatevaliImg"   style="position:relative;top:4px;" />

2. Login interface js, click the verification code picture to refresh the verification code

  $(function () {     
        $("#txtValiCode").val("");
        $("#ValiCode").click(function () {
            location.reload();
        });
    });

3. Add the CreatevaliImg method production verification code in the controller for the img url call in the interface

 public void CreatevaliImg()
        {
            string validateNum = CreateRandomNum(4);
            CreateImage(validateNum);
            Session["ValidateNum"] = validateNum;
        }

        //Random number of production
        private string CreateRandomNum(int NumCount)
        {
            string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,O,P,Q,R,S,T,U,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,m,n,o,p,q,s,t,u,w,x,y,z";
            string[] allCharArray = allChar.Split(',');//Split into arrays
            string randomNum = "";
            int temp = -1;                             //Record the value of the last random number and try to avoid generating several identical random numbers
            Random rand = new Random();
            for (int i = 0; i < NumCount; i++)
            {
                if (temp != -1)
                {
                    rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
                }
                int t = rand.Next(35);
                if (temp == t)
                {
                    return CreateRandomNum(NumCount);
                }
                temp = t;
                randomNum += allCharArray[t];


            }
            return randomNum;
        }
        //Production picture
        private void CreateImage(string validateNum)
        {
            if (validateNum == null || validateNum.Trim() == string.Empty)
                return;
            //generate BitMap image
            System.Drawing.Bitmap image = new System.Drawing.Bitmap(validateNum.Length * 12 + 12, 22);
            Graphics g = Graphics.FromImage(image);
            try
            {
                //Generate random generator
                Random random = new Random();
                //Clear picture background
                g.Clear(Color.White);
                //Draw the background noise line of the picture
                for (int i = 0; i < 25; 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, x2, y1, y2);
                }
                Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
                System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
                g.DrawString(validateNum, font, brush, 2, 2);
                //Picture foreground noise
                for (int i = 0; i < 100; i++)
                {
                    int x = random.Next(image.Width);
                    int y = random.Next(image.Height);
                    image.SetPixel(x, y, Color.FromArgb(random.Next()));

                }
                //Draw the border line of the picture
                g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                //Save image to specified stream
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                Response.ClearContent();
                Response.ContentType = "image/Gif";
                Response.BinaryWrite(ms.ToArray());
            }
            finally
            {
                g.Dispose();
                image.Dispose();
            }
        }

4. Operation effect

5. When logging in, verify the input in the next page and the background generated Session["ValidateNum"]

Posted by NoFear on Mon, 23 Dec 2019 07:10:54 -0800