关于验证码的生成,本人做了一个简单的Demo!

代码如下:

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。 验证码生成 随笔 第1张
 1  public class RandomCode  2  {  3         /// <summary>
 4         /// 这个是使用字母,数字混合  5         /// </summary>
 6         /// <param name="VcodeNum"></param>
 7         /// <returns></returns>
 8         public static string CreateRandomCode(int codeCount)  9  { 10             string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z"; 11             string[] allCharArray = allChar.Split(','); 12             string randomCode = ""; 13             int temp = -1; 14             Random rand = new Random(); 15             for (int i = 0; i < codeCount; i++) 16  { 17                 if (temp != -1) 18  { 19                     rand = new Random(i * temp * ((int)DateTime.Now.Ticks)); 20  } 21                 int t = rand.Next(61); 22                 if (temp == t) 23  { 24                     return CreateRandomCode(codeCount); 25  } 26                 temp = t; 27                 randomCode += allCharArray[t]; 28  } 29 
30             return randomCode; 31  } 32     }
RandomCode

控制器

1 [OutputCache(Duration =5)] 2         public ActionResult Index() 3  { 4                                          //设置二维码是几位的
5             ViewBag.list = RandomCode.CreateRandomCode(6); 6             return View(); 7         }    

视图

1  <p>您的随机验证码为:</p> @ViewBag.list  

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄