SoFunction
Updated on 2025-04-05

MVC implements verification code output function in Controller controller

The mvc project uses the verification code. In order to enable the previous WebForm code to use the above code and after slightly changes, the code can be used as follows:

using System;
using ;
using ;
using ;
using ;
using ;
using ;
namespace 
{
  public class CheckCodeController : Controller
  {
    //
    // GET: /CheckCode/
    public ActionResult Index()
    {
      (GenerateCheckCode());
      return View();
    }
    private string GenerateCheckCode()
    {
      int number;
      char code;
      string checkCode = ;
       random = new Random();
      for (int i = 0; i < 5; i++)
      {
        number = ();
        if (number % 2 == 0)
          code = (char)('0' + (char)(number % 10));
        else
          code = (char)('A' + (char)(number % 26));
        if (code == '0' || code == 'o' || code == 'L' || code == 'I')
        {
          i = i - 1;
        }
        else
        {
          checkCode += ();
        }
      }
      // (new HttpCookie("CheckCode", checkCode));
      ["checkcode"] = checkCode;
      return checkCode;
    }
    private void CreateCheckCodeImage(string checkCode)
    {
      if (checkCode == null || () == )
        return;
       image = new ((int)(( * 12.5)), 22);
      Graphics g = (image);
      try
      {
        //Generate random generator        Random random = new Random();
        //Clear the background color of the picture        ();
        //Draw the background noise line of the picture        for (int i = 0; i < 25; i++)
        {
          int x1 = ();
          int x2 = ();
          int y1 = ();
          int y2 = ();
          (new Pen(), x1, y1, x2, y2);
        }
        Font font = new ("Arial", 12, ( | ));
        . brush = new .(new Rectangle(0, 0, , ), , , 1.2f, true);
        (checkCode, font, brush, 2, 2);
        //Foreground noise points for drawing pictures        for (int i = 0; i < 100; i++)
        {
          int x = ();
          int y = ();
          (x, y, (()));
        }
        //Draw the border lines of the picture        (new Pen(), 0, 0,  - 1,  - 1);
         ms = new ();
        (ms, );
        ();
         = "image/Gif";
        (());
      }
      finally
      {
        ();
        ();
      }
    }
  }
}

Finally, don’t forget the session acquisition settings. You need to add the following code to the file:

/// <summary>
/// MVC in order to obtain session parameters/// </summary>
public override void Init()
{
  PostAuthenticateRequest += (s, e) => ();
  ();
}
void MvcApplication_PostAuthenticateRequest(object sender, EventArgs e)
{
  ();
}

html page code:

html code

<img name="img1"  style="position:absolute;top:5px;right:36px!important;z-index:1000;" alt="Click the image to refresh the verification code" src="CheckCode/Index" <br>onclick="JavaSccript:reloadImage('CheckCode/Index');" /><br><script type="text/javascript">
function reloadImage(url) {
("img1").src = url + '?abc=' + ();
}
  </script>

Summarize

The above is what the editor introduced to you. MVC implements verification code output function in the Controller controller. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!
If you think this article is helpful to you, please reprint it. Please indicate the source, thank you!