[Selfless sharing: ASP. net core Project Practice (Chapter 1)] implementation of the graphic verification code, asp. netcore
Directory Index
[Selfless sharing: ASP. net core project practice] Directory Indexing
Introduction
I haven't updated my blog for a long time. First, I 've been busy recently. Second, I 've been exploring Core, but I have already completed a framework, we are also preparing to try it out in the production environment, but many things are constantly updated and constantly changed out of the superficial and technical aspects we understand. Therefore, if we haven't done well yet, write a blog and correct it repeatedly, which may mislead some new friends.
If you have friends who are studying Core, you can join us.
Verification Code
I believe many of my friends are like me, and the graphic Verification Code has become a stumbling block to Core.
System. Drawing. Primitives this is an official Drawing library, but there are no Bitmap, Graphics, and many other things, so this graphic Verification Code basically passes.
CoreCompat. system. drawing is a third-party and uses the mono System. drawing implementation, as long as the System is used before installation. the Drawing Code does not need to be modified at all. It also supports tracing functions such as CAPTCHA. To support linux or osx, you can install runtime. linux. CoreCompat. System. Drawing and runtime. osx.10.10-x64.CoreCompat. System. Drawing. (Address: https://github.com/CoreCompat/CoreCompat), basically everyone is using this, this in Windows is no problem, in Linux has not been successful, I don't know whether it is a problem of self-compiling or something.
Zkweb. system. drawing is also a third-party, which is obtained from the mono System. Drawing modification. The process is detailed and implemented. So I would like to share this with you.
The differences between this class library and CoreCompat are as follows:
- Without a strong name, CoreCompat uses a forged signature to make the Assembly name the same, but causes Asp. Net and Owin to check that the signed old project fails to start.
- If the CoreCompat project is downloaded and compiled directly, more than 100 errors may occur, most of which are errors that cannot be found by types. I do not know how the author compiled them.
This project copies all the required files from mono 4.6.1.13 and modifies them. You can download and compile them directly through
- Dotnet test can be used to run unit tests. The current pass rate is about 80%.
- I have actually tested it on linux and provided commands for installing libgdiplus in various releases. Currently, the test does not reference System. drawing. primitive, because System. drawing. primitive in.. Net Framework references the original System. drawing, which may cause type conflicts during compilation (only warnings are reported during actual testing)
- Ubuntu Server 16.04 LTS 64bit
- Fedora 24 64bit
- CentOS 7.2 64bit
Zkweb. system. drawing
First, add a reference using Nuget: Install-Package ZKWeb. System. Drawing.
Simple Graphic verification code generation:
1 public class VierificationCodeServices 2 {3 /// <summary> 4 // This method is used to generate a random number of 5 /// </summary> 6 // <param name = "VcodeNum"> the parameter is the number of digits of a random number </param> 7 // <returns> returns a random number string </returns> 8 private string RndNum (int VcodeNum) 9 {10 // the character set displayed for the verification code is 11 string Vchar = ", a, B, c, d, e, f, g, h, I, j, k, l, m, n, p "+ 12", q, r, s, t, u, v, w, x, y, z, a, B, C, D, E, F, G, H, I, J, K, L, M, N, P, P, Q "+ 13", R, s, T, U, V, W, X, Y, Z "; 14 String [] VcArray = Vchar. split (new Char [] {','}); // Split it into an array of 15 string code = ""; // a random number of 16 int temp =-1; // record the last Random value and avoid producing several identical Random numbers 17 18 Random rand = new Random (); 19 // use a simple algorithm to ensure different 20 for (int I = 1; I <VcodeNum + 1; I ++) 21 {22 if (temp! =-1) 23 {24 rand = new Random (I * temp * unchecked (int) DateTime. now. ticks); // initialize random Class 25} 26 int t = rand. next (61); // obtain the random number 27 if (temp! =-1 & temp = t) 28 {29 return RndNum (VcodeNum); // If the obtained random number is repeated, the 30} 31 temp = t is recursively called; // record the generated Random Number 32 code + = VcArray [t]; // The number of digits of the random number plus 33} 34 return code; 35} 36 37 // <summary> 38 // This method writes the generated random number to the image file 39 // </summary> 40 // <param name = "code"> code is a random number </param> 41 // <param name = "numbers"> Number of generated digits (four digits by default) </param> 42 public MemoryStream Create (out string code, int numbers = 4) 43 {44 code = RndNum (numbers); 45 Bitmap Img = null; 46 Graphics g = null; 47 MemoryStream MS = null; 48 Random random = new Random (); 49 // verification code Color set 50 Color [] c = {Color. black, Color. red, Color. darkBlue, Color. green, Color. orange, Color. brown, Color. darkCyan, Color. purple}; 51 52 // Verification Code font set 53 string [] fonts = {"Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial ", ""}; 54 55 56 // defines the image size. The generated image instance 57 Img = new Bitmap (int) code. length * 18, 32); 58 59g = Graphics. fromImage (Img); // generates a new Graphics object 60 61 GB from the Img object. clear (Color. white); // set the background to White 62 63 // draw the background Point 64 at a random position for (int I = 0; I <100; I ++) 65 {66 int x = random. next (Img. width); 67 int y = random. next (Img. height); 68g. drawRectangle (new Pen (Color. lightGray, 0), x, y, 1, 1); 69} 70 // The verification code is drawn in g 71 for (int I = 0; I <code. length; I ++) 72 {73 int cindex = random. next (7); // random color index value 74 int findex = random. next (5); // random Font index value 75 Font f = new Font (fonts [findex], 15, FontStyle. bold); // font 76 Brush B = new SolidBrush (c [cindex]); // color 77 int ii = 4; 78 if (I + 1) % 2 = 0) // The Verification Code is not in the same height 79 {80 ii = 2; 81} 82g. drawString (code. substring (I, 1), f, B, 3 + (I * 12), ii); // draw a validation character 83} 84 MS = new MemoryStream (); // generate the memory stream object 85 Img. save (MS, ImageFormat. jpeg); // Save the image as a Png image file to the stream. dispose (); 89 Img. dispose (); 90 return MS; 91} 92}
Test Run
Create an IActionResult in the Controller to output the verification code:
1 /// <summary> 2 // graphic Verification Code 3 /// </summary> 4 /// <returns> </returns> 5 public IActionResult ValidateCode ([FromServices] vierificationCodeServices _ vierificationCodeServices) 6 {7 string code = ""; 8 System. IO. memoryStream MS = _ vierificationCodeServices. create (out code); 9 HttpContext. session. setString ("LoginValidateCode", code); 10 Response. body. dispose (); 11 return File (ms. toArray (), @ "image/png"); 12}
Foreground output: Windows
Run the following command in Windows:
Linux (CentOS7)
Follow these steps to execute the following command:
Yum install autoconf automake libtool
Yum install freetype-devel fontconfig libXft-devel
Yum install libjpeg-turbo-devel libpng-devel giflib-devel libtiff-devel libexif-devel
Yum install glib2-devel cairo-devel
Git clone https://github.com/mono/libgdiplus
Cd libgdiplus
./Autogen. sh
Make
Make install
Cd/usr/lib64/
Ln-s/usr/local/lib/libgdiplus. so gdiplus. dll
Upload the released project to the Linux server and go to the project directory:
Cd OcelotWeb
Dotnet Ocelot. Web. dll
Let's take a look at our website:
Not Displayed. Let's look at the running log:
FontFamily Not Found: Incorrect font. Let's take a look at our code:
These fonts are not available in Linux, and there are many solutions. Here I use the simplest ones. I don't want to modify my code any more, so I will directly copy these fonts in windows:
Upload to the/usr/share/fonts/chinese/TrueType directory on the server (the chinese/TrueType directories are created by yourself)
Enter this directory:
Cd/usr/share/fonts/chinese/TrueType
Mkfontscale
Mkfontdir
Fc-cache-fv
Open the previous page again:
Yes!
I hope to study Asp.net Core with you.
At the beginning, there was a limited level of contact, and many things were self-understanding and reading the information of the great gods on the Internet. If there is something wrong or incomprehensible, I hope you can correct it!
Although Asp.net Core is very popular now, many materials on the Internet are copied in the previous article, so I have not solved many problems for the time being. I hope you can help me together!
Original article reprinted please respect labor results http://yuangang.cnblogs.com