码迷,mamicode.com
首页 > 其他好文 > 详细

创建验证码

时间:2016-04-20 13:28:15      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

    /// <summary>
        ///     创建验证码
        /// </summary>
        /// <param name="contro"></param>
        public static void GetVerify(Controller contro)
        {
            string key = "ADMIN_VERIFY_" + contro.Session["AdminSession"];
            int x = ran.Next(1, 10), y = ran.Next(1, 10), result = 0;

            //获取算式和答案
            StringBuilder sb = new StringBuilder();
            sb.Append(x);
            switch (ran.Next(1, 3))
            {
                case 1:
                    sb.Append(" + ");
                    result = x + y;
                    break;
                default:
                    sb.Append(" × ");
                    result = x * y;
                    break;
            }
            sb.Append(y).Append(" = ? ");

            //将算式输出为图片
            using (Bitmap b = new Bitmap(100, 28))
            using (Graphics g = Graphics.FromImage(b))
            using (Font font = new Font("Microsoft YaHei", 15))
            {
                g.DrawString(sb.ToString(), font, Brushes.White, 0, 0);
                b.Save(contro.Response.OutputStream, ImageFormat.Jpeg);
            }

            //将答案写入redis,2分钟
            WebHelper.WriteRedisKey(key, result.ToString(), DateTime.Now.AddMinutes(2));

  

创建验证码

标签:

原文地址:http://www.cnblogs.com/Darkon/p/5412119.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!