首页 新闻 会员 周边

网站发布后验证码无法显示。

2
悬赏园豆:20 [已解决问题] 解决于 2011-04-10 23:47

各位博友,我在iis里面发布的网站为什么验证码显示不了,其它的都正常,权限也设置过了,就是显示不出来,但是在本地测试是可以的,发布的时候就出问题了。研究了好长时间,没有找到答案,麻烦哪位高手解答一下,小弟在此谢过了。

下面是权限设置,验证码页面是在后台的根目录里面。

<system.web>
      <authorization>
        <deny users="?"/>
      </authorization>
    </system.web>
  <location path="CheckCode.aspx">
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  </location>

<location path="css">
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  </location>
  <location path="js">
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  </location>
  <location path="upload">
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  </location>
  <location path="rightcommon">
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  </location>
  <location path="js-global">
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  </location>
  <location path="loginskin">
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  </location>
  <location path="images">
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  </location>
  <location path="images-global">
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  </location>

下面我把验证码code贴上。

using System;
using System.Collections;
using System.Configuration;
using System.Data;
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.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Drawing.Printing;

public partial class Admin_CheckCode : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CreateCheckCodeImage(GenerateCheckCode());
    }

    private string GenerateCheckCode()
    {
        int number;
        string strCode = string.Empty;

        //随机数种子
        Random random = new Random();

        for (int i = 0; i < 4; i++) //校验码长度为4
        {
            //随机的整数
            number = random.Next();

            //字符从0-9,A-Z中随机产生,对应的ASCII码分别为
            //48-57,65-90
            number = number % 36;
            if (number < 10)
            {
                number += 48;
            }
            else
            {
                number += 55;
            }
            strCode += ((char)number).ToString();
        }

        //在session中保存校验码
        Session["CheckCode"] = strCode;
        return strCode;
    }

    /// <summary>
    /// 根据校验码输出图片
    /// </summary>
    /// <param name="checkCode">产生的随机校验码</param>
    private void CreateCheckCodeImage(string checkCode)
    {
        //若校验码为空,则直接返回
        if (checkCode == null || checkCode.Trim() == String.Empty)
        {
            return;
        }
        //根据校验码的长度确定输出图片的长度
        System.Drawing.Bitmap image = new System.Drawing.Bitmap(55, 20);//(int)Math.Ceiling(Convert.ToDouble(checkCode.Length * 15))
        //创建Graphics对象
        Graphics g = Graphics.FromImage(image);
        try
        {
            //生成随机数种子
            Random random = new Random();
            //清空图片背景色
            g.Clear(Color.White);
            //画图片的背景噪音线 10条
            //---------------------------------------------------
            for (int i = 0; i < 10; i++)
            {
                //噪音线起点坐标(x1,y1),终点坐标(x2,y2)
                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);
            }
            //---------------------------------------------------
            //Brush b = Brushes.Silver;
            //g.FillRectangle(b, 0, 0, image.Width, image.Height);
            //---------------------以上两种任选其一------------------------------
            //输出图片中校验码的字体: 12号Arial,粗斜体
            Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));

            //线性渐变画刷
            LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Purple, 1.2f, true);
            g.DrawString(checkCode, font, brush, 2, 2);

            //画图片的前景噪音点 50个
            for (int i = 0; i < 50; 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.Peru), 0, 0, image.Width - 1, image.Height - 1);

            //创建内存流用于输出图片
            using (MemoryStream ms = new MemoryStream())
            {
                //图片格式指定为png
                image.Save(ms, ImageFormat.Jpeg);
                //清除缓冲区流中的所有输出
                Response.ClearContent();
                //输出流的HTTP MIME类型设置为"image/Png"
                Response.ContentType = "image/Jpeg";
                //输出图片的二进制流
                Response.BinaryWrite(ms.ToArray());
            }
        }
        finally
        {
            //释放Bitmap对象和Graphics对象
            g.Dispose();
            image.Dispose();
        }
    }
}

路过春秋的主页 路过春秋 | 初学一级 | 园豆:10
提问于:2011-04-07 18:12
< >
分享
最佳答案
0

可以在验证码放在一般处理程序然后返回一张图片。然后IMG控件src指向这个页面

收获园豆:20
青牛客 | 小虾三级 |园豆:562 | 2011-04-08 12:02
感谢侠虫的回答,我的问题原因找到了。原因是命名空间没有加,后来加上就可以了。
路过春秋 | 园豆:10 (初学一级) | 2011-04-09 05:30
其他回答(2)
0

不发布时,在本机上面能显示不

加百力 | 园豆:533 (小虾三级) | 2011-04-07 19:48
在本机上面可以显示的,只是发布到IIS服务器上面就显示不出来了。我的问题解决了,原因是没有添加命名空间。谢谢你的回答。
支持(0) 反对(0) 路过春秋 | 园豆:10 (初学一级) | 2011-04-09 05:28
0

我在本地也没问题,但是我发现只要在Web.Config里面加上下面的代码的话,登录页面的验证码就无法显示了:

    <authorization>
      <deny users="*"/>
    </authorization>

只要把这一句注释掉就能显示,不知什么原因。

无 影 | 园豆:59 (初学一级) | 2012-10-31 12:24
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册