当前位置:网站首页>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 :
边栏推荐
- Exch:exchange server 2013 is about to end support
- K-line diagram must be read for quick start
- Deep understanding of JVM (II) - memory structure (II)
- MSF后渗透总结
- 5g business is officially commercial. What are the opportunities for radio and television?
- Compile and generate busybox file system
- Solution: STM32 failed to parse data using cjson
- Tencent cloud installs MySQL database
- [machine learning] K-means clustering analysis
- Redis (II) -- persistence
猜你喜欢

Word中添加代码块(转载)

构建基本buildroot文件系统

Babbitt | yuanuniverse daily must read: minors ask for a refund after a reward. The virtual anchor says he is a big wrongdoer. How do you think of this regulatory loophole

IEEE TBD SCI影响因子提升至4.271,位列Q1区!

Six photos vous montrent pourquoi TCP serre la main trois fois?

5G商用三年,未来创新何去何从?

Write the simplest small program in C language Hello World

Apache 解析漏洞(CVE-2017-15715)_漏洞复现

New skill: accelerate node through code cache JS startup

New research of HKUST & MsrA: about image to image conversion, finishing is all you need
随机推荐
Post MSF infiltration summary
每日面试1题-蓝队基础面试题-应急响应(1)应急响应基本思路流程+Windows入侵排查思路
Redis (I) - data type
Send the injured baby for emergency medical treatment. Didi's driver ran five red lights in a row
后渗透之文件系统+上传下载文件
6 张图带你搞懂 TCP 为什么是三次握手?
New skill: accelerate node through code cache JS startup
[binary tree] preorder traversal to construct binary search tree
Exch:exchange server 2013 is about to end support
[sword finger offer] 53 - I. find the number I in the sorted array
leetcode:787. The cheapest transfer flight in station K [k-step shortest path + DFS memory + defaultdict (dict)]
IEEE TBD SCI影响因子提升至4.271,位列Q1区!
TCP session hijacking based on hunt1.5
[bjdctf2020]the mystery of ip|[ciscn2019 southeast China division]web11|ssti injection
MIT science and Technology Review released the list of innovators under the age of 35 in 2022, including alphafold authors, etc
【义修换届大礼包】
Booking UI effect implemented by svg
流批一体在京东的探索与实践
【二叉树】前序遍历构造二叉搜索树
Distributed machine learning: model average Ma and elastic average easgd (pyspark)