当前位置:网站首页>Generation of verification code
Generation of verification code
2022-04-23 06:25:00 【Wood acridine】
When a lot of software logs in , Verification code will be used , As shown in the figure below .

Next, let's share the method of generating verification code . I hope that's helpful
Define a method in the controller
First declare a string , It can be directly assigned to a fixed string , You can also generate a random string
for example :string strRandom = "abc";
string strRandom = ValidCodeUtils.GetRandomCode(5); Randomly generate one 5 Bit string
notes :ValidCodeUtils.GetRandomCode Is to create the methods defined in the class , Details of the method are as follows
Create a class and encapsulate the methods you need to use in it for easy use .
Define a method to get a random string
Operations to be implemented :
Generate random numbers mixed with numbers and passwords
Determines the string based on the current random number
The code is as follows :
public static string GetRandomCode(int intLength)
{
/* Generate random numbers mixed with numbers and passwords */
string strReturn = string.Empty;
Random random = new Random();// random number . Represents a pseudo-random number generator
for (int i = 0; i < intLength; i++)
{
char cRerult;
int intRandom = random.Next();// Generate a nonnegative random integer
/* Determines the string based on the current random number */
//intRandom % 3 What you get is intRandom/3 Remainder obtained
if (intRandom % 3 == 0)
{
// Generate numbers
// Number of digits to generate numbers
cRerult = (char)(0x30 + (intRandom % 10));
}
else if (intRandom % 3 == 1)
{
// The number of digits produces capital letters : Uppercase characters 65-97 A 65
//68 D 25 Z
cRerult = (char)(0x41 + (intRandom % 0x1a));
}
else
{
// Remainder is 2
// Produce lowercase letters 98-116
cRerult = (char)(0x61 + (intRandom % 0x1a));
}
strReturn += cRerult.ToString();
}
return strReturn;
}
Then save the verification code into session conversation
Session["validCode"] = strRandom;
Then generate pictures according to the verification code
byte[] imgByte = ValidCodeUtils.CreateImage(strRandom);
notes :ValidCodeUtils.CreateImage Is the method defined in the created class , Details are as follows .
Define a method to create a verification code based on a string
Operations to be implemented :
The new pictures
Draw text on the picture
Draw interference lines on the picture
Draw the foreground interference point of the picture
Draw a border on the outermost side
Save the map to the memory stream
The code is as follows :
public static byte[] CreateImage(string strRandom)
{
// The new pictures
Bitmap newBitmap = new Bitmap(strRandom.Length * 20, 38);//Bitmap Is used to process image objects defined by pixel data . Initialize with the specified size Bitmap class
Graphics g = Graphics.FromImage(newBitmap);//Graphics: Encapsulates a GDI+ Drawing picture . From the specified image Create a new Graphics
g.Clear(Color.White);// Clears the entire drawing surface and fills it with the specified background color
// Draw text on the picture
SolidBrush solidBrush = new SolidBrush(Color.Red);//SolidBrush Define a monochrome brush , Used to fill the shape of the drawing
g.DrawString(strRandom, new Font("Aril", 17), solidBrush, 12, 1);// In the specified position and with the specified Brush and Font Object to draw the specified text string
// Draw interference lines on the picture
Random random = new Random();
for (int i = 0; i < 10; i++)
{
// Generate a line , And draw it on the canvas . The starting point (x,y) Summary point
int x1 = random.Next(newBitmap.Width);
int y1 = random.Next(newBitmap.Height);
int x2 = random.Next(newBitmap.Width);
int y2 = random.Next(newBitmap.Height);
g.DrawLine(new Pen(Color.DarkGray), x1, y1, x2, y2);// Draw a line connecting two points specified by a coordinate pair
}
// Draw the foreground interference point of the picture
for (int i = 0; i < 100; i++)
{
int x = random.Next(newBitmap.Width);
int y = random.Next(newBitmap.Height);
newBitmap.SetPixel(x, y, Color.FromArgb(random.Next()));
}
// Draw a border on the outermost side
g.DrawRectangle(new Pen(Color.Blue), 0, 0, newBitmap.Width, newBitmap.Height);// Draw by coordinate pair 、 Rectangle with specified width and height
g.DrawRectangle(new Pen(Color.Blue), -1, -1, newBitmap.Width, newBitmap.Height);
// Save the map to the memory stream
MemoryStream ms = new MemoryStream();// Create a stream that supports its storage area as memory
newBitmap.Save(ms, ImageFormat.Jpeg);// Save this picture to the specified stream in the specified format
return ms.ToArray();// Write the contents of the stream to byte Array return
}
Finally, return the picture information
return File(imgByte, @"image/jpeg");
版权声明
本文为[Wood acridine]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210616405063.html
边栏推荐
- Chapter 3 of linear algebra - Elementary Transformation of matrix and system of linear equations
- [leetcode 67] sum of two binary numbers
- [leetcode 954] double pair array
- Detection technology and principle
- Understanding and use of tp50, tp90 and tp99
- JDBC connection database
- container
- Gaussian processes of sklearn
- 深入理解去噪论文——FFDNet和CBDNet中noise level与噪声方差之间的关系探索
- Addition, deletion, modification and query of MySQL table
猜你喜欢

A sharp tool to improve work efficiency

Algèbre linéaire chapitre 1 - déterminants

PyTorch笔记——实现线性回归完整代码&手动或自动计算梯度代码对比
![[leetcode 59] spiral matrix II](/img/6e/58e600272797563129d2b325a054b5.png)
[leetcode 59] spiral matrix II

container

Guaba and Computational Geometry

List segmentation best practices

Integration and induction of knowledge points of automatic control principle (Han min version)

SQL injection

MySQL table constraints and table design
随机推荐
[leetcode 67] sum of two binary numbers
Use of multithreaded executors
RPC must know and know
检测技术与原理
Chapter 3 of linear algebra - Elementary Transformation of matrix and system of linear equations
[leetcode 383] ransom letter
Fundamentals of digital image processing (Gonzalez) II: gray transformation and spatial filtering
Example of reentrant lock thread waiting to wake up
St table template
[leetcode 150] evaluation of inverse Polish expression
PyTorch笔记——通过搭建ResNet熟悉网络搭建方式(完整代码)
Pytorch notes - get familiar with the network construction method by building RESNET (complete code)
Contrôle automatique (version Han min)
[leetcode169] most elements
3. Continuous integer
On traversal of binary tree
Import of data
6.Reversal
In depth understanding of the relationship between dncblevel and noise denoising in the paper
Usage scenario of copyonwritearraylist