验证码

来源:互联网 发布:淘宝lol半价点券来源 编辑:程序博客网 时间:2024/05/22 11:42

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace Ticket.Web
{
    public partial class VerifyCode : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Cache.SetNoStore();
            HttpContext.Current.Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
            HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Public);

            string checkCode = AppCode.Sys.CreateRandomCode(4);
            AppCode.Sys.CreateImage(checkCode);
            //Session[Globals.VerifyKeyName] = Sys.Encrypt(checkCode, ConfigurationManager.AppSettings["EncryptKey"]);
            //Session["VerifyCode"] = Common.Cryptography.AesEncode(verifyCode, AppCode.BaseConfig.EncryptKey);
            //HttpContext.Current.Response.Cache.SetNoStore();
            //HttpContext.Current.Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
            //HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Public);

            //System.Drawing.Color bg = System.Drawing.Color.FromArgb(240, 243, 248);
            //string verifyCode = Ticket.Plugin.Services.VerifyImage.CreateVerifyCode(4, false);
            //Plugin.Services.VerifyImageProvider.GetInstance("JpegImage").GenerateImage(verifyCode, 80, 30, bg, 23);
            Session["VerifyCode"] = Common.Cryptography.AesEncode(checkCode, AppCode.BaseConfig.EncryptKey);
        }
    }
}

 

 

 

 

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using System.IO;
using System.Text;
using System.Drawing.Imaging;

namespace Ticket.Web.AppCode
{
    public class Sys
    {
        /// <summary>
        /// 生成验证码图片
        /// </summary>
        /// <param name="checkCode"></param>
        public static void CreateImage(string checkCode)
        {
            int width = (int)(checkCode.Length * 17.5);
            Bitmap image = new Bitmap(width, 25);

            Graphics graphics = Graphics.FromImage(image);
            Font font = new Font("Arial", 14, FontStyle.Bold);
            Brush brush = new SolidBrush(Color.FromArgb(0, 0, 0));

            graphics.Clear(Color.FromArgb(0xdb, 0xdb, 0xdb));


            graphics.DrawString(checkCode, font, brush, (float)3f, (float)3f);

            //线条颜色
            Pen pen = new Pen(Color.SlateGray, 0f);

            Random random = new Random();
            for (int i = 0; i < 2; i++)
            {
                int num3 = random.Next(image.Height);
                graphics.DrawEllipse(pen, 2, num3, image.Width, num3);
            }
            MemoryStream stream = new MemoryStream();
            image.Save(stream, ImageFormat.Jpeg);

            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.ContentType = "image/jpeg";
            HttpContext.Current.Response.BinaryWrite(stream.ToArray());


            stream.Close();
            graphics.Dispose();
            image.Dispose();
        }
        /// <summary>
        /// 生成随机码
        /// </summary>
        /// <param name="codeCount">代码位数</param>
        /// <returns>字符串</returns>
        public static string CreateRandomCode(int codeCount)
        {
            //,a,b,c,d,e,f,g,h,i,j,k,k,m,n,n,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,J,J,K,L,M,N,P,P,Q,R,S,T,U,W,X,Y,Z
            string[] strArray = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,k,m,n,n,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,J,J,K,L,M,N,P,P,Q,R,S,T,U,W,X,Y,Z".Split(new char[] { ',' });
            string str2 = "";
            int num = -1;
            Random random = new Random();
            for (int i = 0; i < codeCount; i++)
            {
                if (num != -1)
                {
                    random = new Random((i * num) * ((int)DateTime.Now.Ticks));
                }
                int index = random.Next(61);
                if (num == index)
                {
                    return CreateRandomCode(codeCount);
                }
                num = index;
                str2 = str2 + strArray[index];
            }
            return str2;
        }
    }
}