注册 登录
编程论坛 ASP.NET技术论坛

[求助]为什么直接跳过查询

垃圾的沉默 发布于 2007-09-20 10:58, 1204 次点击
<%@ Page Language="C#" Debug="True" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server">
void Page_Load(Object sender,EventArgs e)
{
}
//----------------------------------------------------
public void Admin_Login(Object sender,EventArgs E)
{
if (TextBox1.Text != Session["ValidateCode"].ToString())
{
CopyRight.Text = "验证码错误!";
}
//获取要加密的字段,并转化为Byte[]数组
byte[] data=System.Text.Encoding.Unicode
.GetBytes(source.Text.Trim().ToCharArray());
//建立加密服务
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
//加密Byte[]数组
byte[] result= md5.ComputeHash(data);
//将加密后的数组转化为字段
string sResult=System.Text.Encoding.Unicode.GetString(result);
//作为密码方式加密
string EnPswdStr=System.Web.Security.FormsAuthentication.
HashPasswordForStoringInConfigFile(source.Text.ToString(),"MD5");
OleDbConnection cnn;
OleDbCommand cmd;
OleDbDataReader datar;
string sql,strcnn;
strcnn="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("App_Data/DataBase.mdb");
cnn=new OleDbConnection(strcnn);
cnn.Open();
sql="select * from userG where userID='"+ AdminID.Text +"' and userPWD='"+ EnPswdStr +"'";
cmd=new OleDbCommand(sql,cnn);
datar=cmd.ExecuteReader();
if(datar.Read())
{
Session["userID"]=datar["userID"].ToString();
Session["userN"]=datar["userN"].ToString();
Session["sc"]=datar["sc"];
}
if(Session["userID"]!="")
{
Session["sc"]=1;
Response.Redirect("index.htm");
}
else
{
Response.Redirect("index.aspx");
}
if(Session["userID"]=="")
{
Response.Redirect("admin_Log.aspx");
}
}
</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<body>
<table cellpadding="0" cellspacing="0" class="twidth" width="576">
<tr>
<td align="center">
<div class="mframe">
<table cellpadding="0" cellspacing="0">
<tr>
<td class="tl"></td>
<td class="tm"><span class="tt">后台管理登陆</span> </td>
<td class="tr"></td>
</tr>
</table>
<table cellpadding="0" cellspacing="0">
<tr>
<td class="ml"></td>
<td class="mm">
<form id="LoginForm" runat="server">
<table cellpadding="0" cellspacing="0" style="height: 300px" width="60%">
<tr>
<td><img alt="后台管理登陆" src="../Images/Admin_Default/admin_key.gif" /></td>
<td align="center">
<table cellpadding="3" cellspacing="3" width="100%">
<tr>
<td align="right">用户名:</td>
<td>
<asp:TextBox ID="AdminID" runat="server" Columns="20"></asp:TextBox>

</td>
</tr>
<tr>
<td align="right">密&nbsp;&nbsp;码:</td>
<td>
<asp:TextBox ID="source" runat="server" Columns="20" TextMode="Password"></asp:TextBox>

</td>
</tr>
<tr>
<td align="right">验证码:</td>
<td>
<asp:TextBox ID="TextBox1" runat="server" Columns="4" MaxLength="4"></asp:TextBox>
<img src="user/ValidateCode.aspx" />

</td>
</tr>
<tr>
<td align="right"></td>
<td><asp:Button ID="LoginBtn" runat="server" OnClick="Admin_Login" Text="登录" /></td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</td>
<td class="mr"></td>
</tr>
</table>
<table cellpadding="0" cellspacing="0">
<tr>
<td class="bl"></td>
<td class="bm">&nbsp;</td>
<td class="br"></td>
</tr>
</table>
</div>
<table id="footer">
<tr>
<td align="center"><asp:Literal ID="CopyRight" runat="server"></asp:Literal></td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
13 回复
#2
jxnuwy042007-09-20 11:05
没怎么看明白你的问题.
#3
垃圾的沉默2007-09-20 11:11
登陆的时候只是直接执行了Response.Redirect("index.htm");这一句
#4
bygg2007-09-20 11:50

Response.Redirect("index.htm");
执行这句了,就说明 Session["userID"] 中有值,怎么说商执行查询呢?
#5
垃圾的沉默2007-09-20 12:18

但是我无论输入什么字符和密码都能转到 Response.Redirect("index.htm")
这一句..查看session["userN"]的值是空的..

#6
jxnuwy042007-09-20 12:21

就是啊,楼上的说的对.如果没执行查询的话,Session["UserID"]也就没有实例化,就会提示"未将对象引用至实例"的错误!

#7
jxnuwy042007-09-20 12:27

哦,我明白了.Session["UserID"]是object类型,要判断它是否有值应该是这样判断的 即:if(Session["UserID"]!=System.DBNull.Value){ Response.Redirect("index.htm");},你换成这个试试.

#8
bygg2007-09-20 12:37
if(Session["userID"]!="")
改成
if(Session["userID"]!="" && Session["userID"] != null)

你为什么不把这些代码写在后置代码里呢??
这样写不安全,而且不太好调试
#9
垃圾的沉默2007-09-20 14:50

我只是试试的还没放在后置代码里面...
前面的问题解决了..但是我第一次登陆后..第二次无论输入什么都可以登陆,...为什么??
我加判断空的密码会提示..但总是不执行(谁能告诉我在哪里加吗?)
public void Admin_Login(Object sender, EventArgs E)
{
if (TextBox1.Text != Session["ValidateCode"].ToString())
{
Response.Write("<script>alert('验证码错误,请重新登陆!!')</sc" + "ri" + "pt/>");
}
else
{
//获取要加密的字段,并转化为Byte[]数组
byte[] data = System.Text.Encoding.Unicode
.GetBytes(source.Text.Trim().ToCharArray());
//建立加密服务
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
//加密Byte[]数组
byte[] result = md5.ComputeHash(data);
//将加密后的数组转化为字段
string sResult = System.Text.Encoding.Unicode.GetString(result);
//作为密码方式加密

string EnPswdStr = System.Web.Security.FormsAuthentication.
HashPasswordForStoringInConfigFile(source.Text.ToString(), "MD5");
OleDbConnection cnn;
OleDbCommand cmd;
OleDbDataReader datar;
string sql, strcnn;
strcnn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("App_Data/DataBase.mdb");
cnn = new OleDbConnection(strcnn);
cnn.Open();
sql = "select * from userG where userID='" + AdminID.Text + "' and userPWD='" + EnPswdStr + "'";
cmd = new OleDbCommand(sql, cnn);
datar = cmd.ExecuteReader();
if (datar.Read())
{
Session["userID"] = datar["userID"].ToString();
Session["userN"] = datar["userN"].ToString();
Session["sc"] = datar["sc"];
}
if (Session["userID"] != "" && Session["userID"] != null)
{

Response.Redirect("index.htm");
}
else
{
Response.Write("<script>alert('密码或用户名错误,请重新登陆!!')</sc" + "ri" + "pt/>");
Response.Redirect("admin_Login_A.aspx");
}

}
}

#10
jxnuwy042007-09-20 14:54
没看到你判断密码为空时的提示代码啊,在哪写的?
#11
垃圾的沉默2007-09-20 16:02

我写在这里但是不知道为什么就是不执行.
public void Admin_Login(Object sender, EventArgs E)
{
if (TextBox1.Text != Session["ValidateCode"].ToString())
{
Response.Write("<script>alert('验证码错误,请重新登陆!!')</sc" + "ri" + "pt/>");
}
else
{
if (source.Text == "")
{
Response.Write("<script>alert('密码不能为空!!')</sc" + "ri" + "pt/>");
}
//获取要加密的字段,并转化为Byte[]数组
byte[] data = System.Text.Encoding.Unicode
.GetBytes(source.Text.Trim().ToCharArray());
//建立加密服务
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
//加密Byte[]数组
byte[] result = md5.ComputeHash(data);
//将加密后的数组转化为字段
string sResult = System.Text.Encoding.Unicode.GetString(result);
//作为密码方式加密

string EnPswdStr = System.Web.Security.FormsAuthentication.
HashPasswordForStoringInConfigFile(source.Text.ToString(), "MD5");
OleDbConnection cnn;
OleDbCommand cmd;
OleDbDataReader datar;
string sql, strcnn;
strcnn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("App_Data/DataBase.mdb");
cnn = new OleDbConnection(strcnn);
cnn.Open();
sql = "select * from userG where userID='" + AdminID.Text + "' and userPWD='" + EnPswdStr + "'";
cmd = new OleDbCommand(sql, cnn);
datar = cmd.ExecuteReader();
if (datar.Read())
{
Session["userID"] = datar["userID"].ToString();
Session["userN"] = datar["userN"].ToString();
Session["sc"] = datar["sc"];
}
if (Session["userID"] != "" && Session["userID"] != null)
{

Response.Redirect("index.htm");
}
else
{
Response.Write("<script>alert('密码或用户名错误,请重新登陆!!')</sc" + "ri" + "pt/>");
Response.Redirect("admin_Login_A.aspx");
}

}
}

[此贴子已经被作者于2007-9-20 16:07:42编辑过]

#12
jxnuwy042007-09-20 16:15

如果你是要实现登录功能的话,还是给你这段代码作作参考吧.
#region ///登陆事件
//当用户单击登陆按钮时执行该方法
private void cmdLogin_Click(object sender, System.EventArgs e)
{
if(this.txtLoginName.Text!=""&&this.txtPassword.Text!="")
{
//设立一个用户输入的密码是否正确的标志变量
bool bValidPassword=false;

//保存哈希后的数据库密码
byte[] arHashedSavePass=new byte[20];

//保存反映数据库操作正确与否的返回值
int iReturnValue;

//从数据库中获取用户正确的密码
iReturnValue=DBUtils.Login.GetEmpPassword(txtLoginName.Text,ref arHashedSavePass);

if(iReturnValue==(int)DBResult.Success)
{
//如果数据库操作成功,则验证数据库密码和用户输入的密码

//对输入的密码进行Unicode编码后再对其进行哈希处理,采用SHA1

UnicodeEncoding UE=new UnicodeEncoding();
byte[] arSuppliedPass=UE.GetBytes(txtPassword.Text);

SHA1Managed SHhash=new SHA1Managed();
byte[] arHashSuppliedPass=SHhash.ComputeHash(arSuppliedPass);

//比较数据库密码跟用户输入的密码是否相等
bool bHashesEqual=true;

for(int i=0;i<arHashSuppliedPass.Length;i++)
{
if(arHashSuppliedPass[i]!=arHashedSavePass[i])
{
bHashesEqual=false;
break;
}
}
if(bHashesEqual)
{
//如果相等,则标志变量为真
bValidPassword=true;
}
else
{
//否则显示密码错误信息
lblInfo.Text="用户名或密码错误,请重试!";
}

}
else
{
//如果数据库操作失败,则说明登录名不存在,显示错误信息
lblInfo.Text="用户名或密码错误,请重试!";
}
if(bValidPassword)
{
//如果用户登陆名跟密码都正确,则在会话间保存用户登录信息

//保存用户登录名
Session["LoginName"]=txtLoginName.Text;

string strLoginName=txtLoginName.Text;

//新建一个客户端的Cookie值
HttpCookie objNewCookie=new HttpCookie("BlueHill");

//设定Cookie的有效日期
objNewCookie.Expires=DateTime.Now.AddDays(30);

//添加Cookie子项
objNewCookie.Values.Add("LoginName",strLoginName);

//将Cookie值写入客户端
Response.Cookies.Add(objNewCookie);

//从数据库获取该登录名所对应的员工编号
int iEmpID=0;
DBUtils.Login.GetEmployeeID(strLoginName,ref iEmpID);

//在会话间保存该编号信息
Session["EmployeeID"]=iEmpID;

string sID=iEmpID.ToString();
if(FormsAuthentication.GetRedirectUrl(sID,false).IndexOf("default.aspx")==-1)
{
//如果用户试图访问其他网页,则跳转到该网页
FormsAuthentication.RedirectFromLoginPage(sID,false);
}
else
{
//如果用户访问的是登陆网页,则跳转到"显示员工信息"网页
FormsAuthentication.SetAuthCookie(sID,false);
Response.Redirect("EmployeeInfo/ShowEmpInfo.aspx");
}

}
}
else
{
//用户名跟密码为空
lblInfo.Text="用户名跟密码不能为空,请重试!";
}
}
#endregion

这个数据用的是SQL2000 当中有些是为了存储Cookie而写的,可以不用官管. 还有一些获取正确密码的方法我封装到了类里面,但说明这个问题可以不用那些的.你可以参考参考的!

#13
bygg2007-09-20 17:57
登陆前先把所有的Session清空一下.
#14
垃圾的沉默2007-09-21 00:21
我有点明白了..算法应该是这样::
先把验证密码的时候应该出现的错误尽可能的表示出来.如果正确再执行验证码..如果验证码正确就转到应该转的页面上去..我上面写的只是第一次登陆验证密码..如果是第二次登陆的话就只是验证一下session["userID"]是不是空...那第一次登陆的时候session["userID"]对像存在.那就不会再验证密码.只要验证码对的话就直接转到应该页面上去..
1