首页 新闻 会员 周边 捐助

为什么我的验证码在项目运行时没有出来?有点急,谢谢大家了,帮忙看一看

0
[待解决问题]

在项目首次运行的时候它不出来。。但是在账户或者密码输入错误的时候,下次登录页面刷新的时候他会出来。这是什么原因呢?希望大家看看。

以下是一些代码。。。希望大家给我找一找问题的所在,谢谢了。。

 

 

这是我的验证码代码。

 public class Validate
    {
        public Validate()
        {
 
        }
        /// <summary>  
        /// 验证码的最大长度  
        /// </summary>  
        public int MaxLength
        {
            get { return 10; }
        }
        /// <summary>  
        /// 验证码的最小长度  
        /// </summary>  
        public int MinLength
        {
            get { return 1; }
        }

        /// <summary>
        /// 生成验证码
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public string CreateValidateCode(int length)
        {
            int[] randMembers = new int[length];
            int[] validateNums = new int[length];
            string validateNumberStr = "";
            //生成起始序列值  
            int seekSeek = unchecked((int)DateTime.Now.Ticks);
            Random seekRand = new Random(seekSeek);
            int beginSeek = (int)seekRand.Next(0, Int32.MaxValue - length * 10000);
            int[] seeks = new int[length];
            for (int i = 0; i < length; i++)
            {
                beginSeek += 10000;
                seeks[i] = beginSeek;
            }
            //生成随机数字  
            for (int i = 0; i < length; i++)
            {
                Random rand = new Random(seeks[i]);
                int pownum = 1 * (int)Math.Pow(10, length);
                randMembers[i] = rand.Next(pownum, Int32.MaxValue);
            }
            //抽取随机数字  
            for (int i = 0; i < length; i++)
            {
                string numStr = randMembers[i].ToString();
                int numLength = numStr.Length;
                Random rand = new Random();
                int numPosition = rand.Next(0, numLength - 1);
                validateNums[i] = Int32.Parse(numStr.Substring(numPosition, 1));
            }
            //生成验证码  
            for (int i = 0; i < length; i++)
            {
                validateNumberStr += validateNums[i].ToString();
            }
            return validateNumberStr;  
        }

        /// <summary>
        /// 创建验证码的图片
        /// </summary>
        /// <param name="validateCode"></param>
        /// <returns></returns>
        public byte[] CreateValidateGraphic(string validateCode)
        {
            Bitmap image = new Bitmap((int)Math.Ceiling(validateCode.Length * 21.5), 40);
            Graphics g = Graphics.FromImage(image);
            try
            {
                //生成随机生成器  
                Random random = new Random();
                //清空图片背景色  
                g.Clear(Color.White);
                //画图片的干扰线  
                for (int i = 0; i < 25; i++)
                {
                    int x1 = random.Next(image.Width);
                    int x2 = random.Next(image.Width);
                    int y1 = random.Next(image.Height);
                    int y2 = random.Next(image.Height);
                    g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
                }
                Font font = new Font("Arial", 22, (FontStyle.Bold | FontStyle.Italic));
                LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.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(image.Width);
                    int y = random.Next(image.Height);
                    image.SetPixel(x, y, Color.FromArgb(random.Next()));
                }
                //画图片的边框线  
                g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
                //保存图片数据  
                MemoryStream stream = new MemoryStream();
                image.Save(stream, ImageFormat.Jpeg);
                //输出图片流  
                return stream.ToArray();
            }
            finally
            {
                g.Dispose();
                image.Dispose();
            }  
        }

        /// <summary>
        /// 得到验证码的长度
        /// </summary>
        /// <param name="validateNumLength"></param>
        /// <returns></returns>
        public static int GetImageWidth(int validateNumLength)
        {
            return (int)(validateNumLength * 24.0);
        }

        /// <summary>
        /// 得到验证码的高度
        /// </summary>
        /// <returns></returns>
        public static double GetImageHeight()
        {
            return 40;
        }

    }

 

 

这是我的控制器代码,随便说一下,这个是一个.Net mvc项目。。

/// <summary>
        /// 随机生成验证码图片
        /// </summary>
        /// <returns></returns>
        public ActionResult GetValidateCode()
        {
            Validate vCode = new Validate();
            //生成随机数字
            string code = vCode.CreateValidateCode(5);
            Session["ValidateCode"] = code;
            //生成图片
            byte[] bytes = vCode.CreateValidateGraphic(code);
            return File(bytes, @"image/jpeg");
        }

 

 

这是界面的Html代码

<div class="loginWrap">
  <div class="login">
    <div class="Logo"></div>
    <div class="rInput">
      <h1><img src="../../Content/images/t1.gif" width="194" height="39" /></h1>
     <form method="post" action="/Login/Index">
      <table>
        <tbody>
          <tr>
            <th>用户名:</th>
                <td class="inputBg" id="InputName">
                    <%:Html.TextBox("Name") %>
                    <%--<input type="text" name="txtUserName" id="UserName" class="inputBg" />--%>
                </td>
                <%:Html.ValidationMessage("Name","") %>
          </tr>
          <tr>
            <th>密 码:</th>
                <td class="inputBg" id="InputPwd">
                    <%:Html.TextBox("Password") %>
                    <%--<input type="text" name="txtPwd" id="Pwd" class="inputBg" />--%>
                </td>
                <%:Html.ValidationMessage("Password","") %>
          </tr>
          <tr class="vCodeWrap">
            <th>验证码:</th>
            <td><div class="vCodeWrap">
                <ul>
                  <li>
                    <input type="text" name="textfield2" id="textfield2" class="inputW"/>
                  </li>
                  <li>
                    <div class="captch_img">
                        <img id="captcha_img" src="GetValidateCode" mce_src="GetValidateCode" />
                        <a id="captcha_a" href="javascript:void(0)" mce_href="javascript:void(0)">点击图片换一张</a>
                    </div>
                  </li>
                </ul>
              </div></td>
          </tr>
          <tr class="rBtn">
            <th>&nbsp;</th>
            <td><input type="submit"  onmouseover="this.className='LoginActive'" onmouseout="this.className='LoginNormal'" class="LoginNormal" /></td>
          </tr>
          <tr>
            <th>&nbsp;</th>
            <td class="rAccount"><input type="checkbox" name="checkbox"  class="checkbox" id="ckUsersName" />
              <label for="checkbox">记住账户</label>
              <input type="checkbox" name="checkbox"  class="checkbox" id="ckPassWord" style="margin-left:20px;"/>
              <label for="checkbox2">记住密码</label></td>
          </tr>
        </tbody>
      </table>
   </form>
    </div>
  </div>
</div>

日久生情的主页 日久生情 | 菜鸟二级 | 园豆:206
提问于:2014-08-26 21:37
< >
分享
所有回答(6)
0

跟下代码就知道问题出哪了。

Firen | 园豆:5385 (大侠五级) | 2014-08-27 09:17
0

<img id="captcha_img" src="GetValidateCode" mce_src="GetValidateCode" />  这个地方 页面加载的时候 请求一次验证码 是不是有问题

wolfy | 园豆:2636 (老鸟四级) | 2014-08-27 13:00

是的

支持(0) 反对(0) 日久生情 | 园豆:206 (菜鸟二级) | 2014-08-31 17:26

它说找不到GetValidateCode。。。我不知道怎么回事

支持(0) 反对(0) 日久生情 | 园豆:206 (菜鸟二级) | 2014-08-31 17:34
0

在做的时候注意点 最好给src的url后加一个随机数. 因为 像IE 会缓存同url的内容

望着天的蜗牛 | 园豆:354 (菜鸟二级) | 2014-08-27 17:07
0

src=GetValidateCode???这个路径对吗?可以在URL上加时间戳、可以在页面完,再用JS去加载验证码。

Alex_QY1987 | 园豆:1888 (小虾三级) | 2014-08-27 17:28

谢谢了

支持(0) 反对(0) 日久生情 | 园豆:206 (菜鸟二级) | 2014-08-31 17:28
0

检查下加载登陆页时候,验证码图片生成路径对不对

伏草惟存 | 园豆:1420 (小虾三级) | 2014-08-27 18:27

好的,知道了

支持(0) 反对(0) 日久生情 | 园豆:206 (菜鸟二级) | 2014-08-31 17:28
0

直接跟踪一下断点,查看首次登陆页面加载的时候,生成验证码的代码执行过程,一目了然……

白志伟 | 园豆:250 (菜鸟二级) | 2014-08-28 11:20
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册