After the logon window is complete, several steps are required to complete the logon. Now let's complete the Controller that outputs the verification code image. To do this, you need to use the verifycode class, which is found online. Because it has been a long time and the source has been forgotten, I am sorry to the author.
First, create a helper directory under the project, which will be used to place helper classes, including some custom practical methods. Copy the verifycode. CS file to the directory, open the file, and add the namespace "simplecms. helper" to it. This is done to facilitate the Controller to access this class.
Add a controller named verifycodecontroller to the Controller directory (controllers). The controller code is as follows:
Usingsystem;
Usingsystem. Collections. Generic;
Usingsystem. LINQ;
Usingsystem. Web;
Usingsystem. Web. MVC;
Namespace simplecms. Controllers
{
Public class verifycodecontroller: Controller
{
//
// Get:/verifycode/
Public actionresult index ()
{
Return view ();
}
}
}
Because the output is an image in the file format and is not a view, you need to change the returned actionresult type to filecontentresult.
Add a reference to the verifycode class:
Usingsimplecms. helper;
In the verifycode class, the createverifycode method can generate a 6-bit verification code, and the createimages method can be called to return the byte array of the image, through which the image file can be generated. The Code is as follows:
Public filecontentresultindex ()
{
Verifycode v = new verifycode ();
String code = V. createverifycode (); // obtain the random code
Session ["vcode"] = code;
V. Padding = 10;
Byte [] bytes = V. createimage (CODE );
Return file (bytes, @ "image/JPEG ");
}
The keywords in the session can be defined according to your favorite name, not necessarily vcode. The padding attribute serves the same purpose as the CSS-defined patch on the page. Finally, a file consisting of byte arrays returned by createimage is returned. The file type is JPEG.
The Controller code has been completed. Now let's test whether the image can be properly displayed. You can directly press ctra + F5, or select debug in the main menu to start executing (not debugging), open the website in the browser, and modify the address to access the verifycode controller, if the result shown in Figure 8 is displayed (the verification code in the figure is different), the Controller is complete.