protected void Page_Load(object sender, EventArgs e)
{
this.Image1.ImageUrl = "ValidateCode2.aspx";//获取验证码
}
protected void btnLogin_Click(object sender, EventArgs e)//登陆按钮
{
if (txtAdminName.Text.Trim() == "" || txtAdminPwd.Text.Trim() == "")
{
Response.Write(CC.MessageBox("登录名和密码不能为空!", "Login.aspx"));
}
else
{
//判断用户输入的验证码是否正确
//此处出错
if (Session["CheckCode"].ToString().Equals(txtAdminCode.Text.ToString().ToUpper()))
{
string UserName = txtAdminName.Text;
string UserPwd = txtAdminPwd.Text;
string selsql = "select * from tbUser where (Name='" + UserName + "')and(passWord='" + UserPwd + "')";
SqlDataReader sdr = operateData.getRow(selsql); if (operateData.getCount(selsql) > 0)
{
//该用户为合法用户,跳转到后台首页(AdminIndex.aspx)中
Session["admin"] = txtAdminName.Text.Trim();//用户名
sdr.Read();
Response.Redirect("../NewsManage/AdminIndex.aspx");
}
else
{
//该用户不是合法用户,调用CommonClass类中的MassageBox方法,弹出提示框
Response.Write(CC.MessageBox("您输入的用户名或密码错误,请重新输入!", "Login.aspx")); }
}
else
{
Response.Write(CC.MessageBox("验证码输入有误,请重新输入!", "Login.aspx")); }
}
}
{
this.Image1.ImageUrl = "ValidateCode2.aspx";//获取验证码
}
protected void btnLogin_Click(object sender, EventArgs e)//登陆按钮
{
if (txtAdminName.Text.Trim() == "" || txtAdminPwd.Text.Trim() == "")
{
Response.Write(CC.MessageBox("登录名和密码不能为空!", "Login.aspx"));
}
else
{
//判断用户输入的验证码是否正确
//此处出错
if (Session["CheckCode"].ToString().Equals(txtAdminCode.Text.ToString().ToUpper()))
{
string UserName = txtAdminName.Text;
string UserPwd = txtAdminPwd.Text;
string selsql = "select * from tbUser where (Name='" + UserName + "')and(passWord='" + UserPwd + "')";
SqlDataReader sdr = operateData.getRow(selsql); if (operateData.getCount(selsql) > 0)
{
//该用户为合法用户,跳转到后台首页(AdminIndex.aspx)中
Session["admin"] = txtAdminName.Text.Trim();//用户名
sdr.Read();
Response.Redirect("../NewsManage/AdminIndex.aspx");
}
else
{
//该用户不是合法用户,调用CommonClass类中的MassageBox方法,弹出提示框
Response.Write(CC.MessageBox("您输入的用户名或密码错误,请重新输入!", "Login.aspx")); }
}
else
{
Response.Write(CC.MessageBox("验证码输入有误,请重新输入!", "Login.aspx")); }
}
}
解决方案 »
- textarea格式问题
- WEB工作流设计器???
- 在线等——一个网站能否同时部署4.0和2.0的项目
- 关于浏览器兼容问题?
- 两个关于用户控件的问题
- 工程引用EXCEL.DLL,并在程序里导出EXCEL ,部署的服务器是不是一定要装OFFICE???
- 要在Access中存储 byte [] 数组怎么实现?或者经过转换?
- 下面这段是什么意思:[Browsable(false),DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- 请问服务器控件如何运行客户端的脚本?
- 网站传值
- 应用程序域创建实例对象,纠结。求救
- 怎样才能在datalist下面的image动态显示图片
你不判断Session["CheckCode"]是否为空就ToString...出错正常了
第一次打开.都没存过checkcode.能不空?
为什么进入登陆页面时Session["CheckCode"]为空呢?
ValidateCode2.aspx的代码
protected void Page_Load(object sender, EventArgs e)
{
//string checkCode = GetRandom();
//Session["CheckCode"] = checkCode;
DrawRandom();
}
private string GetRandom()
{
int number;
char charCode;
string strCode = string.Empty;
System.Random random = new System.Random(); for (int i = 1; i <= 5; i++)
{
number = random.Next();
if (number % 2 == 0)
{
charCode = (char)('0' + (char)(number % 10));
}
else
{
charCode = (char)('A' + (char)(number % 26));
}
strCode += charCode.ToString();
}
// System.Web.HttpContext.Current.Response.Cookies
HttpContext.Current.Response.Cookies.Add(new HttpCookie("codebycookies", strCode));
return strCode;
} public void DrawRandom()
{ string strCode = GetRandom();
Session["CheckCode"] = strCode;
Bitmap image = new Bitmap(100, 30);
Graphics huatu = Graphics.FromImage(image);
try
{
//生成随机生成器
Random random = new Random();
//清空图片背景色
huatu.Clear(Color.White); //画图片的背景噪音线
int x1, x2, y1, y2;
for (int i = 0; i < 10; i++)
{
x1 = random.Next(image.Width);
x2 = random.Next(image.Width);
y1 = random.Next(image.Height);
y2 = random.Next(image.Height);
huatu.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
} Font font = new Font("Arial", 20, (FontStyle.Bold | System.Drawing.FontStyle.Italic));
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
huatu.DrawString(strCode, font, brush, 2, 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()));
}
//画图片的边框线
huatu.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width, image.Height);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
}
finally
{
huatu.Dispose();
image.Dispose();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//代码放这里 }
}
所以通常的解决方案是用脚本改变图片的src来加载验证码,加载时在链接后面加上随机的query。不过这还得看你这个图片响应时的http头,如果http头里面约束了不使用private缓存。ie8也不会缓存图片。
private void SetPageNoCache()
{
Response.Buffer = true;
Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
Response.Expires = 0;
Response.CacheControl = "no-cache";
Response.AppendHeader("Pragma", "No-Cache");
}
在PAGELOAD里调用一下!
判断session是否为空,在PAGELODA中用
if(!ispostback){}判断
如果session不存在,就会出现 你现在的这种异常!System.NullReferenceException: 未将对象引用设置到对象的实例
是写成这样吗?this.Image1.ImageUrl = "ValidateCode2.aspx?id=" + DateTime.Now.ToFileTime().ToString(); 可是这样还是缓存图片啊,还是需要刷新才能找到session
http头里面约束不使用private缓存是什么意思?
我生成验证码的时候就已经把验证码保存在session内了
是不是写成这样?
this.Image1.ImageUrl = "ValidateCode2.aspx?id=" + DateTime.Now.ToFileTime().ToString();
但还是不行
http://www.suchso.com/projecteactual/aspnet-chrome-cookie-session-id.html