当前位置:网站首页>ASP. Net generate verification code
ASP. Net generate verification code
2022-06-30 18:08:00 【Bailu-】
One . explain
This article is Xiaobai learning Morni Chang Teacher's video teaching <<Asp.Net WEB Server programming technology >> Study notes made in , Some knowledge points follow the tutorial , You can also go to the teacher's homepage to learn , Thank you. .
Two . Generate captcha image (checkcode.aspx.cs)
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MyMusci
{
public partial class checkcode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string chkCode = string.Empty;
int ix, iy;
ix = 80;
iy = 24;
// Color list , Used for verification code 、 Noise line 、 noise
Color[] color = {
Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };
// Font list , Used for verification code
string[] font = {
"Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "MingLiU", "Arial" };
// The character set of the captcha , Removed some confusing characters
char[] character = {
'2', '3', '4', '5', '6', '8', '9','0','1',' One ',' Small ',' white ' };
Random rnd = new Random();
// Generate captcha string
for (int i = 0; i < 4; i++)
{
chkCode += character[rnd.Next(character.Length)];
}
Bitmap bmp = new Bitmap(ix, iy);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);
// Draw a noise line
for (int i = 0; i < 10; i++)
{
int x1 = rnd.Next(ix);
int y1 = rnd.Next(iy);
int x2 = rnd.Next(ix);
int y2 = rnd.Next(iy);
Color clr = color[rnd.Next(color.Length)];
g.DrawLine(new Pen(clr), x1, y1, x2, y2);
}
// Draw the verification code string
for (int i = 0; i < chkCode.Length; i++)
{
string fnt = font[rnd.Next(font.Length)];
Font ft = new Font(fnt, 14, FontStyle.Bold);
Color clr = color[rnd.Next(color.Length)];
g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 16 + 2, (float)2);
}
// Painting noise
for (int i = 0; i < 50; i++)
{
int x = rnd.Next(bmp.Width);
int y = rnd.Next(bmp.Height);
Color clr = color[rnd.Next(color.Length)];
bmp.SetPixel(x, y, clr);
}
// Write the verification code to SESSION, Why can't the second page get the value ???
// General handler adds interface file ;
Session["Code"] = chkCode.ToUpper();
// Clear the page output cache , Set this page to have no cache
Response.Buffer = true;
Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds(0);
Response.Expires = 0;
Response.CacheControl = "no-cache";
Response.AppendHeader("Pragma", "No-Cache");
// Write the verification code image to the memory stream , And put it in "image/Png" Format output
// My page returns pictures ;
MemoryStream ms = new MemoryStream();
try
{
bmp.Save(ms, ImageFormat.Png);
Response.ClearContent();
Response.ContentType = "image/Png";
Response.BinaryWrite(ms.ToArray());
}
finally
{
// Explicitly release resources
bmp.Dispose();
g.Dispose();
}
}
}
}
3、 ... and . Add verification code to the login page (login_new.aspx)
stay table Add the following code to
<tr>
<td>
Verification Code </td>
<td>
<asp:TextBox ID="code" runat="server"></asp:TextBox>
</td>
<td><img id="imgcode" src="checkcode.aspx" /></td>
</tr>
<tr>
<td>
<asp:Button ID="btn_sub" runat="server" class="btn btn-primary" OnClick="btn_sub_Click" Text=" Sign in " />
</td>
<td>
<%-- The reset button is replaced here with register (LinkButton) Control
PostBackUrl Indicates the jump position of the control after clicking
CausesValidation="False" Indicates that you can jump to the position without validating the control
--%>
<asp:LinkButton ID="LinkButton1" runat="server" class="btn btn-secondary" PostBackUrl="~/RegUser.aspx" CausesValidation="False"> register </asp:LinkButton>
</td>
<td> </td>
</tr>
Join in jquery.js, And add the following script :
<script> // Picture click event $("#imgcode").click(function () {
$("#imgcode").attr("src", "checkcode.aspx?t=" + Math.random()); }) </script>
Every time we click on an image , Will refresh the contents of the picture , That is, the content of the verification code :
边栏推荐
- Synchronized summary
- Deep understanding of JVM (VI) -- garbage collection (III)
- MIT science and Technology Review released the list of innovators under the age of 35 in 2022, including alphafold authors, etc
- News management system based on SSM
- 【架构】1366- 如何画出一张优秀的架构图
- Optimization of interface display for general kernel upgrade of mobo video management system v3.5.0
- 4 years of working experience, and you can't tell the five communication modes between multithreads. Can you believe it?
- Redis (VII) - sentry
- K-line diagram must be read for quick start
- Six pictures show you why TCP has three handshakes?
猜你喜欢

Analysis on the construction scheme and necessity of constructing expressway video monitoring platform
![[零基础学IoT Pwn] 环境搭建](/img/3b/a0689a1570fcc40bb9a5a4e9cdc63c.png)
[零基础学IoT Pwn] 环境搭建

Servlet operation principle_ API details_ Advanced path of request response construction (servlet_2)

同济、阿里的CVPR 2022最佳学生论文奖研究了什么?这是一作的解读

Share 5 commonly used feature selection methods, and you must see them when you get started with machine learning!!!

AnimeSR:可学习的降质算子与新的真实世界动漫VSR数据集
![[Netease Yunxin] playback demo build: unable to convert parameter 1 from](/img/6e/41e1eafd4c863c9e5f3a545b69a257.png)
[Netease Yunxin] playback demo build: unable to convert parameter 1 from "asyncmodalrunner *" to "std:: nullptr\u T"**

Importing alicloud ECS locally to solve deployment problems

2022上半年盘点:20+主流数据库重大更新及技术要点汇总

. Net ORM framework hisql practice - Chapter 1 - integrating hisql
随机推荐
IEEE TBD SCI impact factor increased to 4.271, ranking Q1!
流批一体在京东的探索与实践
Vue3 reactive database
Post MSF infiltration summary
6 张图带你搞懂 TCP 为什么是三次握手?
Word中添加代码块(转载)
TCP session hijacking based on hunt1.5
2022上半年盘点:20+主流数据库重大更新及技术要点汇总
Share 5 commonly used feature selection methods, and you must see them when you get started with machine learning!!!
Lenovo's "dual platform" operation and maintenance solution helps to comprehensively improve the intelligent management ability of the intelligent medical industry
Small tools (3) integration knife4j3.0.3 interface document
. Net ORM framework hisql practice - Chapter 1 - integrating hisql
Importing alicloud ECS locally to solve deployment problems
Spin lock exploration
TFTP下载kernel,nfs挂载文件系统
编译生成busybox文件系统
[PROJECT] Xiaomao school (IX)
Rainbow Brackets 插件的快捷键
Zero foundation can also be an apple blockbuster! This free tool can help you render, make special effects and show silky slides
How to write a technical proposal