通过ashx生成验证码  存入session中 
在另外一个ashx中验证验证码是否正确  获取session为null
如果是通过aspx生成验证码 获取session就没有问题

解决方案 »

  1.   

    本帖最后由 net_lover 于 2012-05-22 12:57:12 编辑
      

  2.   

    public class Handler : IHttpHandler, IRequiresSessionState
      

  3.   

    接口都实现了 还是不行using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    using System.Drawing;
    using System.Text;
    using System.Drawing.Imaging;
    using System.Web.SessionState;namespace XKT.WebUI.ashx
    {
        /// <summary>
        /// $codebehindclassname$ 的摘要说明
        /// </summary>
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        public class validateimage : IHttpHandler, IRequiresSessionState,
        {        /**//// <summary>
            /// 生成验证图片核心代码
            /// </summary>
            /// <param name="hc"></param>
            public void ProcessRequest(HttpContext context)
            {
                //设置输出流图片格式
                context.Response.ContentType = "image/gif";            string chkCode = string.Empty;
                //颜色列表,用于验证码、噪线、噪点 
                Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };
                //字体列表,用于验证码 
                string[] font = { "Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact" };
                //验证码的字符集,去掉了一些容易混淆的字符 
                char[] character = { '2', '3', '4', '5', '6', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' };
                Random rnd = new Random();
                //生成验证码字符串 
                for (int i = 0; i < 4; i++)
                {
                    chkCode += character[rnd.Next(character.Length)];
                }
                Bitmap bmp = new Bitmap(100, 40);
                Graphics g = Graphics.FromImage(bmp);
                g.Clear(Color.White);
                //画噪线 
                for (int i = 0; i < 10; i++)
                {
                    int x1 = rnd.Next(bmp.Width);
                    int y1 = rnd.Next(bmp.Height);
                    int x2 = rnd.Next(bmp.Width);
                    int y2 = rnd.Next(bmp.Height);
                    Color clr = color[rnd.Next(color.Length)];
                    g.DrawLine(new Pen(clr), x1, y1, x2, y2);
                }
                //画验证码字符串 
                for (int i = 0; i < chkCode.Length; i++)
                {
                    string fnt = font[rnd.Next(font.Length)];
                    Font ft = new Font(fnt, 24);
                    Color clr = Color.Black;
                    g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 20 + 8, (float)4);
                }
                //画噪点 
                for (int i = 0; i < 100; i++)
                {
                    int x = rnd.Next(bmp.Width);
                    int y = rnd.Next(bmp.Height);
                    Color clr = color[rnd.Next(color.Length)];
                    bmp.SetPixel(x, y, clr);
                }
                //画图片的边框线
                g.DrawRectangle(new Pen(Color.Silver), 0, 0, bmp.Width - 1, bmp.Height - 1);            try
                {
                    bmp.Save(context.Response.OutputStream, ImageFormat.Gif);
                    context.Session["xktinfo"] = chkCode.ToString(); //先保存在Session中,验证与用户输入是否一致
                    context.Response.End();
                }
                finally
                {
                    //显式释放资源 
                    bmp.Dispose();
                    g.Dispose();
                }
                
           
            }
            
            /**//// <summary>
            /// 表示此类实例是否可以被多个请求共用(重用可以提高性能)
            /// </summary>
            public bool IsReusable
            {
                get
                {
                    return true;
                }
            }
        }
    }
    public class login : IHttpHandler, IRequiresSessionState
        {        public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/plain";
                string username = context.Request["uname"];
                string password = context.Request["upass"];
                string checkcode = context.Request["code"];            if (checkcode!=context.Session["xktinfo"].ToString().ToLower())
                {
                    context.Response.Write("checkcodeerror");
                }
                else
                {
                    if (username == "admin")
                    {
                        context.Response.Write("ok");
                    }
                    else
                    {
                        context.Response.Write("no");
                    }
                }
                
                
            }        public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
      

  4.   

    不是接口的问题
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    using System.Drawing;
    using System.Text;
    using System.Drawing.Imaging;
    using System.Web.SessionState;namespace XKT.WebUI.ashx
    {
        /// <summary>
        /// $codebehindclassname$ 的摘要说明
        /// </summary>
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        public class validateimage : IHttpHandler, IRequiresSessionState,
        {        /**//// <summary>
            /// 生成验证图片核心代码
            /// </summary>
            /// <param name="hc"></param>
            public void ProcessRequest(HttpContext context)
            {
                //设置输出流图片格式
                context.Response.ContentType = "image/gif";            string chkCode = string.Empty;
                //颜色列表,用于验证码、噪线、噪点 
                Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };
                //字体列表,用于验证码 
                string[] font = { "Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact" };
                //验证码的字符集,去掉了一些容易混淆的字符 
                char[] character = { '2', '3', '4', '5', '6', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' };
                Random rnd = new Random();
                //生成验证码字符串 
                for (int i = 0; i < 4; i++)
                {
                    chkCode += character[rnd.Next(character.Length)];
                }
                Bitmap bmp = new Bitmap(100, 40);
                Graphics g = Graphics.FromImage(bmp);
                g.Clear(Color.White);
                //画噪线 
                for (int i = 0; i < 10; i++)
                {
                    int x1 = rnd.Next(bmp.Width);
                    int y1 = rnd.Next(bmp.Height);
                    int x2 = rnd.Next(bmp.Width);
                    int y2 = rnd.Next(bmp.Height);
                    Color clr = color[rnd.Next(color.Length)];
                    g.DrawLine(new Pen(clr), x1, y1, x2, y2);
                }
                //画验证码字符串 
                for (int i = 0; i < chkCode.Length; i++)
                {
                    string fnt = font[rnd.Next(font.Length)];
                    Font ft = new Font(fnt, 24);
                    Color clr = Color.Black;
                    g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 20 + 8, (float)4);
                }
                //画噪点 
                for (int i = 0; i < 100; i++)
                {
                    int x = rnd.Next(bmp.Width);
                    int y = rnd.Next(bmp.Height);
                    Color clr = color[rnd.Next(color.Length)];
                    bmp.SetPixel(x, y, clr);
                }
                //画图片的边框线
                g.DrawRectangle(new Pen(Color.Silver), 0, 0, bmp.Width - 1, bmp.Height - 1);            try
                {
                    bmp.Save(context.Response.OutputStream, ImageFormat.Gif);
                    context.Session["xktinfo"] = chkCode.ToString(); //先保存在Session中,验证与用户输入是否一致
                    context.Response.End();
                }
                finally
                {
                    //显式释放资源 
                    bmp.Dispose();
                    g.Dispose();
                }
                
           
            }
            
            /**//// <summary>
            /// 表示此类实例是否可以被多个请求共用(重用可以提高性能)
            /// </summary>
            public bool IsReusable
            {
                get
                {
                    return true;
                }
            }
        }
    }
      

  5.   

    问题应该在这里  
    try
                {
                    bmp.Save(context.Response.OutputStream, ImageFormat.Gif);
                    context.Session["xktinfo"] = chkCode.ToString(); //先保存在Session中,验证与用户输入是否一致
                    context.Response.End();
                }
                finally
                {
                    //显式释放资源 
                    bmp.Dispose();
                    g.Dispose();
                }我把这个换成MemoryStream ms = new MemoryStream();
                try
                {
                    bmp.Save(ms, ImageFormat.Gif);
                    context.Session["xktinfo"] = chkCode.ToString(); //先保存在Session中,验证与用户输入是否一致
                    context.Response.ClearContent();
                    context.Response.ContentType = "image/Png";
                    context.Response.BinaryWrite(ms.ToArray());
                }
                finally
                {
                    //显式释放资源 
                    bmp.Dispose();
                    g.Dispose();
                }就可以了 没弄清楚究竟怎么回事
      

  6.   

    建议关注一下SESSIONID,看看,几次操作的SESSION,是不是同一个ID
      

  7.   

    StreamWriter sw = new StreamWriter(filename,false,Encoding.GetEncoding("gb2312"));
      

  8.   

    本帖最后由 net_lover 于 2012-05-22 13:26:38 编辑