C# asp.net中使用web服务的网站登录表单

C# asp.net中使用web服务的网站登录表单,c#,asp.net,C#,Asp.net,我正在尝试从网站登录到web服务。我有一个带有表用户(id、user、pass、int admin)的access数据库(如果是1,如果不是0)。 在web服务中,我有以下webmethod: [WebMethod] public DataSet login(string u, string p) { OleDbConnection CNN = null; OleDbCommand CMD = null; string sql = "select * from user

我正在尝试从网站登录到web服务。我有一个带有表用户(id、user、pass、int admin)的access数据库(如果是1,如果不是0)。 在web服务中,我有以下webmethod:

[WebMethod]
public DataSet login(string u, string p)
{
    OleDbConnection CNN = null;
    OleDbCommand CMD = null;

    string sql = "select * from users where username ='" + u + "' and pass='" + p + "' ";

    CNN = new OleDbConnection(conn);
    CMD = new OleDbCommand(sql, CNN);
    CMD.Connection.Open();

    OleDbDataAdapter adapter = new OleDbDataAdapter(CMD);
    DataSet ds = new DataSet();
    adapter.Fill(ds, "logged");

    CNN.Close();
    return ds;
}
在我的网站上,我有以下代码:

protected void Button1_Click(object sender, EventArgs e)
{
    db.Service Login = new db.Service();

    Login.login(lUser.Text, lPass.Text);
}
所以我的问题是,我如何才能看到登录的用户是admin还是no

我想从数据集ds中读取它——因为它充满了我需要的所有信息,但如何做到这一点呢

谢谢,
dnisko

首先,请避免使用sql字符串将用户类型的值直接传递到数据库。您可能会受到sql注入攻击,并且很容易出错

//Parametrize your following query.
string sql = "select * from users where username ='" + u + "' and pass='" + p + "' "; 

回答您的问题:

您的
login()
方法返回一个
DataSet
对象,因此您需要将
login()
方法的返回值分配给
DataSet

db.Service Login = new db.Service();

DataSet ds = Login.login(lUser.Text, lPass.Text);

bool isAdmin = false;

//Check if there is a record for the username and password
if(ds.Tables[0].Rows.Count == 1)
{
    //now check if user is an admin or not
    isAdmin = Convert.ToBoolean(ds.Tables[0].Rows[0]["admin"]);

    if(isAdmin)
    {
        //User is an admin
    }

}else{

   //User does not exist in the database
}

首先,请避免直接使用sql字符串将用户类型的值传递给数据库。您容易受到sql注入攻击,并且容易出错

//Parametrize your following query.
string sql = "select * from users where username ='" + u + "' and pass='" + p + "' "; 

回答您的问题:

您的
login()
方法返回一个
DataSet
对象,因此您需要将
login()
方法的返回值分配给
DataSet

db.Service Login = new db.Service();

DataSet ds = Login.login(lUser.Text, lPass.Text);

bool isAdmin = false;

//Check if there is a record for the username and password
if(ds.Tables[0].Rows.Count == 1)
{
    //now check if user is an admin or not
    isAdmin = Convert.ToBoolean(ds.Tables[0].Rows[0]["admin"]);

    if(isAdmin)
    {
        //User is an admin
    }

}else{

   //User does not exist in the database
}

我在这里遇到一个错误—(ds.Tables[0]。Rows[“admin”])-“无法将字符串转换为int”。它说行必须是int…有什么建议吗?啊,我丢失了行索引并进行了更新。它应该是
ds.Tables[0]。Rows[0][“admin”
好的,这就解决了所有问题。感谢您的解决方案,现在,我想,我可以进行会话了(使用用户id和用户名)使用此解决方案…再次感谢,干杯。我在这里遇到一个错误-(ds.Tables[0]。行[“admin”])-“无法将字符串转换为int”。它说行必须是int。有什么建议吗?啊,我缺少行索引并进行了更新。它应该是
ds.Tables[0]。行[0][“admin”]
好的,这解决了所有问题。感谢您提供的解决方案,现在,我想,我可以使用此解决方案进行会话(使用用户id和用户名)…再次感谢,干杯。