当前位置:网站首页>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
边栏推荐
- Problems and solutions of database migration
- 2. Average length of words
- Techniques et principes de détection
- Pytorch notes - complete code for linear regression & manual or automatic calculation of gradient code comparison
- POI and easyexcel exercises
- Explain of MySQL optimization
- 7.Domino piling
- Detection technology and principle
- Troubleshooting of data deleted and reappeared problems
- ThreadLocal. Threadlocalmap analysis
猜你喜欢
A general U-shaped transformer for image restoration
Mysql database foundation
The bottom implementation principle of thread - static agent mode
图像恢复论文——[RED-Net, NIPS16]Image Restoration Using Very Deep Convolutional Encoder-Decoder Networks wi
On traversal of binary tree
Illustrate the significance of hashcode
[leetcode 67] sum of two binary numbers
Algèbre linéaire chapitre 1 - déterminants
How to use comparative learning to do unsupervised - [cvpr22] training & [eccv20] image translation
線性代數第一章-行列式
随机推荐
Use of multithreaded executors
LockSupport. Park and unpark, wait and notify
Algèbre linéaire chapitre 1 - déterminants
Algèbre linéaire chapitre 2 - matrice et son fonctionnement
Reading of denoising papers - [cvpr2022] blind2blind: self supervised image denoising with visible blind spots
Substring Inversion (Easy Version)
Chapter 3 of linear algebra - Elementary Transformation of matrix and system of linear equations
How to use comparative learning to do unsupervised - [cvpr22] training & [eccv20] image translation
PHP processing JSON_ Decode() parses JSON stringify
自動控制(韓敏版)
Formation à la programmation
Collections multiple parameter sorting
JDBC operation transaction
Exception handling: grab and throw model
Filebrowser realizes private network disk
Example of ticket selling with reentrant lock
In depth source code analysis servlet first program
电机与拖动(戚金清版)学习整理
12. Monkeys climb mountains
ValueError: loaded state dict contains a parameter group that doesn‘t match the size of optimizer‘s