乱数認証コードの生成

3491 ワード

/// 
    ///       
    /// 
    public class Captcha
    {
        public static string randomCode = "";//        
        /// 
        ///      
        /// 
        ///      
        /// 
        public static string CreateRandomCode(int codeLen)
        {
            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,V,W,X,Y,Z
            string[] allCharAry = allChar.Split(',');
            randomCode = "";
            int temp = -1;
            Random rand = new Random();
            for (int i = 0; i < codeLen; i++)
            {
                if (temp != -1)
                {
                    rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
                }
                int t = rand.Next(9);
                if (temp == t)
                {
                    return CreateRandomCode(codeLen);
                }
                temp = t;
                randomCode += allCharAry[t];
            }
            return randomCode;
        }
    }

乱数は1つのクラスに単独で静的フィールドを使用し、静的フィールドは同じインスタンスで一意です.
/// 
        ///          
        /// 
        /// 
        private byte[] CreateValidGraphic()
        {
            string validateCode = Captcha.CreateRandomCode(codeLen);
            Bitmap img = new Bitmap((int)Math.Ceiling(validateCode.Length * 16.0), 27);
            Graphics g = Graphics.FromImage(img);
            try
            {
                Random random = new Random();//     
                g.Clear(Color.White);//       
                for (int i = 0; i < 25; i++)//       
                {
                    int x1 = random.Next(img.Width);
                    int x2 = random.Next(img.Width);
                    int y1 = random.Next(img.Height);
                    int y2 = random.Next(img.Height);
                    g.DrawLine(new Pen(Color.Silver), x1, x2, y1, y2);
                }
                Font font = new Font("Arial", 13, (FontStyle.Bold | FontStyle.Italic));
                LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Blue, Color.DarkRed, 1.2f, true);
                g.DrawString(validateCode, font, brush, 3, 2);
                for (int i = 0; i < 100; i++)//         
                {
                    int x = random.Next(img.Width);
                    int y = random.Next(img.Height);
                    img.SetPixel(x, y, Color.FromArgb(random.Next()));
                }
                g.DrawRectangle(new Pen(Color.Silver), 0, 0, img.Width - 1, img.Height - 1);//       
                MemoryStream stream = new MemoryStream();
                img.Save(stream, ImageFormat.Jpeg);
                return stream.ToArray();//    
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                g.Dispose();
                img.Dispose();
            }
        }

画像検証コードを生成し、自分のニーズに合わせて調整する
/// 
        ///        
        /// 
        /// 
        [HttpGet]
        public IActionResult GetCaptcha()
        {
            var bytes = CreateValidGraphic();
            return File(bytes,@"image/Gif");
        }

フロントエンドインタフェースに提供