首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

.netC#该怎么解决

2013-12-26 
.netC#验证码生成,登录分开测试都对,为什么和一起就出错,即使验证码输对了,还是提示错误,请高手明示,新手

.netC#
验证码生成,登录分开测试都对,为什么和一起就出错,即使验证码输对了,还是提示错误,请高手明示,新手学习中。这两个是代码图
.netC#该怎么解决.netC#该怎么解决
这个是运行图
.netC#该怎么解决
这个是错误显示
.netC#该怎么解决
[解决办法]
你调试一下,看看text3和yanzm是否一个值
[解决办法]

引用:
你调试一下,看看text3和yanzm是否一个值


界面上面显示的和登陆获取的验证码不一致,
曾经遇到过,每次获得的都是上一次的验证码,你打个断点看看就知道
[解决办法]
个人感觉 你的生成 验证码的代码 不对 、我把 我 写的一个 生成验证码的代码 给你 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;

namespace CheckCode
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:ServerCode runat=server></{0}:ServerCode>")]
    public class ServerCode : WebControl
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? "[" + this.ID + "]" : s);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            output.Write(Text);
        }
        public void CreateCode() 
        {
            string chkCode = string.Empty;
            Color[] color = { Color.Black,Color.Red,
                                Color.Blue,Color.Green,
                            Color.Orange,Color.Brown,Color.DarkBlue};
            string[] font = { "Times New Roman","MS Mincho","Book Antiqua",
                            "Gungsuh","PMingLiU","Implact"};
            char[] character = {'2','3','4','5','6','7','8','9',
                               'A','B','C','D','E','F','G','H','J','K','L','M',
                               'N','P','Q','R','S','T','W','X','Y'};
            Random ran = new Random();


            for (int i = 0; i < 4; i++) 
            {
                chkCode += character[ran.Next(character.Length)];
            }
            HttpResponse response = this.Page.Response;
            HttpCookie anyCookie = new HttpCookie("validateCookie");
            anyCookie.Values.Add("ChkCode", chkCode);
            response.Cookies["validateCookie"].Values["ChkCode"] = chkCode;

            Bitmap bmp = new Bitmap(100,30);
            Graphics g = Graphics.FromImage(bmp);
            g.Clear(Color.White);

            for (int i = 0; i < 5; i++) 
            {
                int x1 = ran.Next(100);
                int x2 = ran.Next(30);
                int y1 = ran.Next(100);
                int y2 = ran.Next(30);
                Color clr = color[ran.Next(color.Length)];
                g.DrawLine(new Pen(clr),x1,y1,x2,y2);
            }

            for (int i = 0; i < 100; i++) 
            {
                int x = ran.Next(bmp.Width);
                int y = ran.Next(bmp.Height);
                Color clr = color[ran.Next(color.Length)];
                bmp.SetPixel(x,y,clr);
            }

            for (int i = 0; i < chkCode.Length; i++) 
            {
                string fnt = font[ran.Next(font.Length)];
                Font ft = new Font(fnt,16);
                Color clr = color[ran.Next(color.Length)];
                g.DrawString(chkCode[i].ToString(),ft,new SolidBrush(clr),
                    (float)i*20+20,(float)6);
            }
            MemoryStream ms = new MemoryStream();
            try
            {
                bmp.Save(ms, ImageFormat.Png);
                response.ClearContent();
                response.ContentType = "image/Png";
                response.BinaryWrite(ms.ToArray());
                response.Flush();
                response.End();
            }
            catch (Exception)
            {

                throw;
            }


            finally 
            {
                bmp.Dispose();
                g.Dispose();
            }
        }
        public bool CheckSN(string sn) 
        {
            return (sn.ToUpper()==
                this.Page.Request.Cookies["validateCookie"].Values["ChkCode"]
                .ToString().ToUpper());
        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            string str = this.Page.Request.QueryString["_ImageTag"];
            if (str == "1") 
            {
                this.CreateCode();
            }
        }
        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
            if (!this.DesignMode)
            {
                writer.Write("<img border="1" src="{0}" Style='width:100px height:30px'>",
                    this.Page.Request.Path + "?_ImageTag=1");
            }
            else 
            {
                writer.Write("验证码控件");
            }
        }
    }
}

热点排行