一个需要图文验证的登录程序,请大家帮忙调试下!

lsxq2007 2012-03-27 10:41:50
我做了一个登录界面,需要输入验证码,但是每次单击登录按钮,就只是刷新我的验证码,到底怎么回事啊,请给位大侠帮忙调试下了!

login.aspx页面代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="login.aspx.cs" Inherits="users_login" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
<style type="text/css">
.style1
{
width: 100%;
background-color: #CCFFFF;
}
.style2
{
width: 500px;
}
.style3
{
width: 169px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>

<table align="center" cellspacing="1" class="style1">
<tr>
<td>
<table align="center" cellspacing="1" class="style2">
<tr>
<td class="style3" align="right">
用户名:</td>
<td>
<asp:TextBox ID="txt_name" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3" align="right">
密码:</td>
<td>
<asp:TextBox ID="txt_pwd" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3" align="right">
验证码:</td>
<td>
<asp:TextBox ID="txt_yzm" runat="server"></asp:TextBox>
  
<asp:Image ID="Image1" runat="server" Height="31px" Width ="72px"
ImageUrl="~/users/ValidNums.aspx"/>
</td>
</tr>
<tr>
<td class="style3">
 </td>
<td>
 </td>
</tr>
<tr>
<td class="style3">
 </td>
<td>
<asp:Button ID="btn_login" runat="server" onclick="btn_login_Click" Text="登录" />
          
<asp:Button ID="btn_cancel" runat="server" Text="取消" />
</td>
</tr>
</table>
</td>
</tr>
</table>

</div>
</form>
</body>
</html>


login.aspx.cs代码如下
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.Data.SqlClient;
using System.Drawing;

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

}
protected void btn_login_Click(object sender, EventArgs e)
{
try
{
if (txt_name.Text == "" || txt_pwd.Text == "")
{
this.Page.RegisterStartupScript("ss", "<script>alter('用户名和密码信息不能为空!')</script>");
return ;//返回操作
}
else
{
string num = this.txt_yzm.Text.Trim();//获取验证码
if (Session["ValidNums"].ToString() == num.ToUpper())
{
//连接数据库
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["ftmydata"].ConnectionString);
sqlcon.Open(); //打开数据库
string strselect = "select * from ft_user where username='" + txt_name.Text.Trim() + "'";
SqlDataAdapter adsas = new SqlDataAdapter(strselect, sqlcon);
DataSet addss = new DataSet();
adsas.Fill(addss); //加载数据信息
if (addss.Tables[0].Rows.Count > 0)
{
string strselect1 = "select * from ft_user where username='" + txt_name.Text.Trim() + "' and userpwd='" + txt_pwd.Text.Trim() + "'";
SqlCommand sqlcom1 = new SqlCommand(strselect1, sqlcon);
SqlDataReader dr1 = sqlcom1.ExecuteReader();
if (dr1.Read()) //执行读取数据信息的操作
{
//登录成功后,跳转到的网站页面
Response.Redirect("default.aspx");
}
//如果密码错误,则弹出提示信息对话框
else
{
this.Page.RegisterStartupScript("ss", "<script>alter('密码错误!')</script>");
return;
}
dr1.Close(); //关闭操作
}
//如果用户名称不存在,则弹出提示对话框
else
{
this.Page.RegisterStartupScript("ss", "<script>alter('用户名不存在!')</script>");
return;
}
sqlcon.Close(); //关闭数据库
}
//如果验证码输入错误,则弹出提示对话框
else
{
this.Page.RegisterStartupScript("ss", "<script>alter('验证码输入错误!')</script>");
return;
}
}
}
//如果有操作上的错误,执行CATCH语句中的程序代码
catch (Exception ex)
{
this.Page.RegisterStartupScript("ss", "<script>alter('验证码输入错误,请刷新页面!')</script>");
}
}
}
...全文
118 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
mngzilin 2012-03-27
  • 打赏
  • 举报
回复
你自己断点跟踪一下,看到了哪步出的问题,这种问题最好自己先找出问题点,要不然我们看代码也是有困难的,毕竟人眼比不过调试器
lsxq2007 2012-03-27
  • 打赏
  • 举报
回复
图片验证码页面是 ValidNums.aspx.cs 代码如下
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;

public partial class ValidNums : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string validateNum = CreateRandomNum(4); //成生4位随机字符串
CreateImage(validateNum); //将生成的随机字符串绘成图片
Session["ValidNums"] = validateNum; //保存验证码
}
}
//生成随机字符串
private string CreateRandomNum(int NumCount)
{
string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z";
string[] allCharArray = allChar.Split(',');//拆分成数组
string randomNum = "";
int temp = -1;//记录上次随机数的数值,尽量避免产生几个相同的随机数

Random rand = new Random();
for (int i = 0; i < NumCount; i++)
{
if (temp != -1)
{
rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
}
int t = rand.Next(35);
if (temp == t)
{
return CreateRandomNum(NumCount);
}
temp = t;
randomNum += allCharArray[t];
}
return randomNum;
}
//生成图片
private void CreateImage(string validateNum)
{
if (validateNum == null || validateNum.Trim() == String.Empty)
return;
//生成Bitmap图像
System.Drawing.Bitmap image = new System.Drawing.Bitmap(validateNum.Length * 12 + 10, 22);
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 System.Drawing.Font("Arial", 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.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(validateNum, 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()));
}

//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), 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();
}
}
}


请大侠么帮我看看,,我一调试login.aspx页面,只要点“登录”按钮,就是不停的刷新验证码,,中间密码错误或者其他什么原因,都不显示的?到底怎么回事,请帮我看看吧


注明:我在web.config中设置的数据库连接

62,267

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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