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

[求助]请高人们看看是什么问题

freedomGD 发布于 2010-07-28 01:25, 1671 次点击
protected void Button1_Click(object sender, EventArgs e)
    {
        
        if (this.name.Text != "")//判断用户名是否未空
        {
         if (this.pwd.Text != "")//判断密码是否未空
         {
               if (this.yanzhen1.Text != "")//判断验证码是否未空
           {
            if (this.yanzhen1.Text == this.Label1.Text)//判断验证码是否相等
               {

                   try
                   {
                       OleDbConnection m_conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("./app_data/library.mdb"));
                       m_conn.Open();

                       OleDbCommand cmd = new OleDbCommand("select * from Administrator where 用户名=" + this.name.Text + "and 密码=" + this.pwd.Text, m_conn);    //建立sql查询语句

  int state = Convert.ToInt32(cmd.ExecuteScalar());           
                           if (state == 0 || state > 1)                              
                           {
                               this.Label2.Text = "用户不存在,请检测用户名和密码是否正确!";
                           }
                                  else
                                 {            this.Label2.Text = "登入成功!";              }
                          
                                
             m_conn.Close();}
              
                           catch (Exception a)
                               {    Response.Redirect("Default2.aspx");        }   
                           }
                  

            
                  else
                   {          this.Label2.Text = "验证码不正确,请重新输入!";         }   
                  }
            
             else
              {    this.Label2.Text = "验证码没有填写!";           }     
               }

        else
         {         this.Label2.Text = "密码没有填写!";                 }  
        }

程序是运行成功的,问题是只要验证码一样,用户名和密码和ACCESS表内的有无相同的数据,也可以到另一个网页,我是新手,请高人们看看是什么问题
23 回复
#2
冰镇柠檬汁儿2010-07-28 09:48
如果我没记错的话,ExecuteScalar方法应该是执行SQL语句,并返回记录结果中第一行第一列的值,你确定你的这个值一定是数字吗?况且看你的判断if (state == 0 || state > 1),你确定这个返回值只有1一个吗?难道你有这样一个字段,它的值都是1,然后这个字段是表的第一列?并且我还想问一下,你的这个程序是用记事本写的吗?
#3
Alohal2010-07-28 11:10
ExecuteScalar方法是返回首行首列,而你用的select *,这显然不对嘛,应该select count(*)才是返回查找记录数,而且你那个state怎么还是>1,莫非查找出来的记录不唯一,一个名字加一密码不是完全能确定唯一记录么。你再改改试试。
#4
bygg2010-07-28 11:38
程序代码:
protected void Button1_Click(object sender, EventArgs e)
        {
            #region 条件判断

            if (string.IsNullOrEmpty(this.name.Text))//判断用户名是否未空
            {
                this.Label2.Text = "用户名有填写!";
                return;
            }
            if (string.IsNullOrEmpty(this.pwd.Text))//判断密码是否未空
            {
                this.Label2.Text = "密码没有填写!";
                return;
            }
            if (string.IsNullOrEmpty(this.yanzhen1.Text))//判断验证码是否未空
            {
                this.Label2.Text = "验证码没有填写!";
                return;
            }
            if (!this.yanzhen1.Text.Equals(this.Label1.Text))//判断验证码是否相等
            {
                this.Label2.Text = "验证码不正确,请重新输入!";
                return;
            }

            #endregion

             OleDbConnection m_conn = null;
             try
             {
                 m_conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("./app_data/library.mdb"));
                 m_conn.Open();

                 OleDbCommand cmd = new OleDbCommand("select COUNT(*) from Administrator where 用户名='" + this.name.Text + "' and 密码='" + this.pwd.Text + "'", m_conn);    //建立sql查询语句

                 int state = Convert.ToInt32(cmd.ExecuteScalar());
                 if (state != 1)
                 {
                     this.Label2.Text = "用户不存在,请检测用户名和密码是否正确!";
                 }
                 else
                 {
                     this.Label2.Text = "登入成功!";
                 }
             }

             catch (Exception a)
             {
                 Response.Redirect("Default2.aspx");
             }
             finally
             {
                 if (m_conn != null)
                 {
                     m_conn.Close();
                 }
             }
        }


[ 本帖最后由 bygg 于 2010-7-28 11:43 编辑 ]
#5
冰镇柠檬汁儿2010-07-28 11:40
Alohal你要注意到一个问题,他的页面跳转实际上是因为代码出现异常才会跳转的,而异常的问题是由于他的sql语句有问题
OleDbCommand cmd = new OleDbCommand("select * from Administrator where 用户名=" + this.name.Text + "and 密码=" + this.pwd.Text, m_conn);    //建立sql查询语句
int state = Convert.ToInt32(cmd.ExecuteScalar());
显然是他在加查询条件的时候没有加单引号引起的

楼主请注意,你的sql那句应该这样写
OleDbCommand cmd = new OleDbCommand("select count(*) from Administrator where 用 户名='" + this.name.Text + "' and 密码='" + this.pwd.Text +"'", m_conn);
#6
Alohal2010-07-28 11:55
回复 5楼 冰镇柠檬汁儿
恩,就是SQL语句没加单引号,呵呵,我都没注意了。
#7
冰镇柠檬汁儿2010-07-28 11:56
回复 6楼 Alohal
没什么的,bygg也没注意到呢
#8
bygg2010-07-28 12:02
谁说我没注意,哼哼/
#9
冰镇柠檬汁儿2010-07-28 13:15
以下是引用bygg在2010-7-28 12:02:59的发言:

谁说我没注意,哼哼/
改帖子是有时间记录的哦
#10
bygg2010-07-28 13:18
.其实我是注意到的,不过把前面那个改了,后面就忘记了直接发了,后面发现了,却在你发贴之后.
就这样
#11
freedomGD2010-07-29 16:26
先谢谢大家帮忙,异常是已经解决了,但无法判断用户和密码是否和ACCESS表内的一致,用户是否存在

#12
冰镇柠檬汁儿2010-07-29 16:47
如果返回值是1不就是说有一样的吗,返回0就没有呗,有什么不会的呢
#13
bygg2010-07-29 17:26
直接用我的代码跑!
#14
cgme2010-07-30 00:01
三楼的代码容易出现SQL注入
#15
冰镇柠檬汁儿2010-07-30 10:16
以下是引用cgme在2010-7-30 00:01:39的发言:

三楼的代码容易出现SQL注入
重要的是让楼主学习到东西,现在的问题是功能上的实现,而不是细节上的安全处理
#16
freedomGD2010-08-01 12:39
回复 13楼 bygg
也试过了,但都是无法判断用户和密码是否和ACCESS表内的一致,用户是否存在。
 if (state != 1)
            {
                this.Label2.Text = "用户不存在,请检测用户名和密码是否正确!";
            }
            else
            {
                this.Label2.Text = "登入成功!";
            }
        }
这个判断语句无执行
#17
bygg2010-08-01 16:33
无法执行的话, state的值又是什么呢
#18
冰镇柠檬汁儿2010-08-01 21:24
请问楼主看到我在5楼发的帖子了吗?如果没有异常的话
int state = Convert.ToInt32(cmd.ExecuteScalar());           
                           if (state == 0 || state > 1)                              
                           {
                               this.Label2.Text = "用户不存在,请检测用户名和密码是否正确!";
                           }
                                  else
                                 {            this.Label2.Text = "登入成功!";              }
这些代码一定会被执行
#19
NewDeveloper2010-08-02 21:31
以下是引用freedomGD在2010-8-1 12:39:15的发言:

也试过了,但都是无法判断用户和密码是否和ACCESS表内的一致,用户是否存在。
 if (state != 1)
            {
                this.Label2.Text = "用户不存在,请检测用户名和密码是否正确!";
            }
            else
            {
                this.Label2.Text = "登入成功!";
            }
        }
这个判断语句无执行
你点击后 label2里面显示的是什么?还是保持不变?
#20
pgj5252010-08-03 13:59

看了跟帖,好感动!呵呵,我是才注册的,这是我在这第一个打开的帖子,有种很温馨的感觉!O(∩_∩)O~
#21
freedomGD2010-08-15 11:35
以下是引用NewDeveloper在2010-8-2 21:31:10的发言:

你点击后 label2里面显示的是什么?还是保持不变?
label2什么都无显示出来
#22
NewDeveloper2010-08-15 12:53
有没有出现什么异常啊 有的话大致上就是你访问EXCEL出现问题了
#23
freedomGD2010-08-15 14:14
以下是引用NewDeveloper在2010-8-15 12:53:49的发言:

有没有出现什么异常啊 有的话大致上就是你访问EXCEL出现问题了
异常就是label2无显示是否成功登录,试过换成转到另一个页面也不行, 库的连接我测过,可以的,用SQL也行
#24
NewDeveloper2010-08-15 14:30
我是说你测试的网页代码里面数据查询链接语句在执行的时候肯定出错了  你把try catch去掉就知道自己出什么异常了
1