登录问题(Web应用程序)

登录问题(Web应用程序)

问题描述:

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.OleDb; //connecting database

public partial class LawyersHome : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        string a = Request.Form["uname"].ToString();
        string b = Request.Form["pwd"].ToString();
        OleDbConnection con = new OleDbConnection();
        con.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=H:\MoratuwaLawyersAssociation\MLADB.mdb";
        con.Open();
        string sql = "SELECT * FROM admin";
        OleDbCommand comm = new OleDbCommand(sql, con);
        OleDbDataReader or;
        or = comm.ExecuteReader();
        while (or.Read())
        {
            if ((a == or["aID"].ToString()) && (b == or["apw"].ToString()))
            {
                con.Close();
                Response.Redirect("LawyersHome.aspx");
            }
            else
            {
                con.Close();
                Response.Redirect("IncorrectLogin.aspx");
            }
        }
        


    }
}



即使我输入了正确的密码,它也会直接进入错误的页面,任何人都可以帮助我解决这个问题.



even though i enter a correct password it re direct into wrong page can anybody help me to overcome this.

用户名和密码中可能有空格.所以用修剪

may be space is there in username and password. so use trim

if ((a == or["aID"].ToString().trim) && (b == or["apw"].ToString().trim))


您的管理表中有多少条记录?
调试应用程序,您将发现造成的灾难.如果第一条记录与您的用户名和密码不匹配,它将重定向到错误页面".
选择唯一的用户名和密码(使用where条件指定),然后检查是否获取了任何记录.如果获取,则重定向到成功页面,否则重定向到错误页面.可以解决您的问题.
How many records you have in your admin table?
Debug your application and you will find the disaster you have made. If the first record does not match with your username and password it will redirect to the "wrong page".
Select the only for the username and password specified using where condition and then check whether any records fetched or not. If fetched then redirect to success page, error page otherwise. That will solve your problem.




您的代码中存在逻辑问题.

首先,您的管理表包含以下记录
sno aID aPW

1个ID1 PW1
2 ID2 PW2
3 ID3 PW3

根据您的代码逻辑,如果我给出ID2和PW2,则WHILE循环将检查数据库并获取第一条记录ID1和PW1,如果IF条件失败,则转到ELSE部分并关闭连接,重定向到ELSE部分中的网页.

在这里修改了您的代码,请参见下文,

Hi,

There is a logic issue in your code.

First say, your admin table contains following records
sno aID aPW

1 ID1 PW1
2 ID2 PW2
3 ID3 PW3

As per your code logic, if i give ID2 and PW2, the WHILE loop will check the database and fetches the first record ID1 and PW1 and it fails the IF condition and goes to ELSE part and closses connection and redirects to the Web page in ELSE part.

Here modified your code, see below,

protected void Page_Load(object sender, EventArgs e)
{
string a = Request.Form["uname"].ToString();
string b = Request.Form["pwd"].ToString();
OleDbConnection con = new OleDbConnection();
con.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=H:\MoratuwaLawyersAssociation\MLADB.mdb";
con.Open();
string sql = "SELECT * FROM admin";
OleDbCommand comm = new OleDbCommand(sql, con);
OleDbDataReader or;
or = comm.ExecuteReader();
bool isFound = false;
while (or.Read())
{
if ((a == or["aID"].ToString()) && (b == or["apw"].ToString()))
{
  isFound = true;
  break;
}
else
{
  isFound = false;
}
}

con.close();

if(isFound)
{
  Response.Redirect("LawyersHome.aspx");
}
else
{
  Response.Redirect("IncorrectLogin.aspx");
}

}



注意:您正在使用Openconnection方法.加载数据需要花费时间.尝试使用封闭连接方法.这样可以加快您的流程.


问候,
Suresh



NOTE: You are using Openconnection method. It takes time to load your data. Try using Closed connection method. That will speed up your process.


Regards,
Suresh