各位老师快帮忙!

lakerskobe_wuxin 2008-07-26 10:09:30
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;

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

private string GenerateCheckCode()
{
int number;
char code;
string checkCode = String.Empty;

System.Random random = new Random();

for (int i = 0; i < 5; i++)
{
number = random.Next();

if (number % 2 == 0)
code = (char)('0' + (char)(number % 10));
else
code = (char)('A' + (char)(number % 26));

checkCode += code.ToString();
}

Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
Session["CheckCode"] = checkCode;
return checkCode;
}

private void CreateCheckCodeImage(string checkCode)
{
if (checkCode == null || checkCode.Trim() == String.Empty)
{
return;
}

System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 13.5)), 22);//13.5必须是double型,控制图像宽度,22控制高度
Graphics g = Graphics.FromImage(image);

try
{
//生成随机生成器
Random random = new Random();

////清空图片背景色
g.Clear(ColorTranslator.FromHtml("#3F63AD"));

////画图片的背景噪音线
//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(ColorTranslator.FromHtml("#3F63AD")), x1, y1, x2, y2);
//}

Font font = new System.Drawing.Font("Microsoft Sans Serif", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.White, Color.White, 1.2f, true);
g.DrawString(checkCode, font, brush, 2, 2);

//画图片的前景噪音点
//for (int i = 0; i < 200; 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(ColorTranslator.FromHtml("#3F63AD")), 0, 0, image.Width - 1, image.Height - 1);

System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}
}

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

public partial class login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
string strSql = "selectLoginUser";
SqlParameter[] paras ={
new SqlParameter("@User",SqlDbType.Char,30),
new SqlParameter("@Psw",SqlDbType.Char,15)};
if (Session["CheckCode"] != null)
{
if (Session["CheckCode"].ToString()==this.tbCode.Text.Trim())
{
if (this.tbUser.Text == "" || this.tbPsw.Text == "")
{
Response.Write("<script language=javascript>alert('请输入完整信息!')</script>");
}
else
{
string aaa = SqlHelper.ExecuteScalar(SqlHelper.connectionString, CommandType.StoredProcedure, strSql, paras).ToString();
int count = int.Parse(aaa);
if (count < 0)
{
Response.Write("<script language=javascript>alert('对不起,您的管理名或是密码有误,请认真填写!')</script>");
}
if (count > 0)
{
string sqlcc = "selectsoluUser";
string sss = SqlHelper.ExecuteScalar(SqlHelper.connectionString, CommandType.StoredProcedure, sqlcc, paras).ToString();
int Audit = int.Parse(sss);
if (Audit == 0)
{
Response.Redirect("waitLogin.aspx");
}
if (Audit == 1)
{
Session["Level"] = "work";
Response.Redirect("~/Default.aspx");
}
if (Audit == 2)
{
Session["Level"] = "Finance";
Response.Redirect("~/Default.aspx");
}
}
}
}
else
{
Response.Write("<script language=javascript>alert('验证码错误!')</script>");
}
}
}
}


我在if (Session["CheckCode"].ToString()==this.tbCode.Text.Trim())这句里的不到session的值 总是直接的跳转到验证码错误 谢谢各位了!
...全文
104 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
lakerskobe_wuxin 2008-07-26
  • 打赏
  • 举报
回复
OK了 嘿嘿写了
GhostAdai 2008-07-26
  • 打赏
  • 举报
回复
哇噻,上来就是一大段代码,能不能把几个关键代码段贴出来啊,看着眼晕!自己下断点慢慢跟踪,找出关键点再上来问。
lakerskobe_wuxin 2008-07-26
  • 打赏
  • 举报
回复
好的 我试试看
wxbps 2008-07-26
  • 打赏
  • 举报
回复
你新建一个页面接收下这个Session在Label里面显示出来,看看能不能收到.我出问题都是TRY CATCH一段一段分的,你都写了那么多了,出问题最烦了!
biglazy 2008-07-26
  • 打赏
  • 举报
回复


验证码输错了吧? 呵呵 开个玩笑~

建议再仔细检查下程序!


lakerskobe_wuxin 2008-07-26
  • 打赏
  • 举报
回复
唉 就是因为我跟了 所以才郁闷 session跳到(Session["CheckCode"].ToString()==this.tbCode.Text.Trim())直接就跳到下面 而且我也看了断点的值 根本就没有那个显示的字短 郁闷 真的没有错吗 怎么总是跳到验证码错误那?
cheng_feng001 2008-07-26
  • 打赏
  • 举报
回复
跟一下看看Session["CheckCode"]的值应该能发现问题
biglazy 2008-07-26
  • 打赏
  • 举报
回复
二楼 那上面一句不是刚刚判断过吗

if (Session["CheckCode"] != null)
。。。。

biglazy 2008-07-26
  • 打赏
  • 举报
回复

应该没有问题啊
没看出哪出错了!

xudaneng 2008-07-26
  • 打赏
  • 举报
回复
断点调试一下Session["CheckCode"]是否为空,如果为空说明Session["CheckCode"]没有赋值或者服务端禁用了Session

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧