C# 如果在c中登录成功,如何获取其他列值#

C# 如果在c中登录成功,如何获取其他列值#,c#,asp.net,sql,C#,Asp.net,Sql,我有一个名为Users的表,它有4列 用户ID 用户名 密码 角色 如果登录成功,我想知道用户ID和角色值, 对于登录验证,我编写了以下函数 private bool ValidationFunction(string username, string pwd) { bool boolReturnValue = false; string s = "correct connection string"; SqlConnection

我有一个名为Users的表,它有4列

  • 用户ID
  • 用户名
  • 密码
  • 角色
如果登录成功,我想知道用户ID和角色值,

对于登录验证,我编写了以下函数

 private bool ValidationFunction(string username, string pwd)
    {
        bool boolReturnValue = false;

        string s = "correct connection string";
        SqlConnection con = new SqlConnection(s);
        con.Open();
        string sqlUserName;
        sqlUserName = "SELECT UserName,Password FROM Users WHERE UserName ='" + username + "' AND Password ='" + pwd + "'";
        SqlCommand cmd = new SqlCommand(sqlUserName, con);

        string CurrentName;
        CurrentName = (string)cmd.ExecuteScalar();

        if (CurrentName != null)
        {
            boolReturnValue = true;
        }
        else
        {
            Session["UserName"] = "";
            boolReturnValue = false;
        }
        return boolReturnValue;
    }
ExecuteScalar()
函数只返回第一列的
顶部记录值
。因此,您需要使用
ExecuteReader()

另一件重要的事情是,最好使用参数化查询将这些用户键入的值传递到数据库中。您可以通过这种方式进行sql注入攻击

试试这个:

using (SqlConnection cnn = new SqlConnection("yourConnectionString"))
{
    string sql= "select userId,role from users " +
                "where username=@uName and password=@pWord";

    using (SqlCommand cmd = new SqlCommand(sql,cnn))
    {
         cmd.Parameters.AddWithValue("@uName", username);
         cmd.Parameters.AddWithValue("@pWord", pwd);

         cnn.Open();
         SqlDataReader reader = cmd.ExecuteReader();

         while (reader.Read())
         {
            //get the reader values here.
         }
    }
}
ExecuteScalar()
函数只返回第一列的
顶部记录值
。因此,您需要使用
ExecuteReader()

另一件重要的事情是,最好使用参数化查询将这些用户键入的值传递到数据库中。您可以通过这种方式进行sql注入攻击

试试这个:

using (SqlConnection cnn = new SqlConnection("yourConnectionString"))
{
    string sql= "select userId,role from users " +
                "where username=@uName and password=@pWord";

    using (SqlCommand cmd = new SqlCommand(sql,cnn))
    {
         cmd.Parameters.AddWithValue("@uName", username);
         cmd.Parameters.AddWithValue("@pWord", pwd);

         cnn.Open();
         SqlDataReader reader = cmd.ExecuteReader();

         while (reader.Read())
         {
            //get the reader values here.
         }
    }
}

如果用户表中有
UserID
Role
,则可以使用下面的代码。它还具有使用参数防止SQL注入攻击的额外好处

private class User
{
    public int UserID {get;set;}
    public string Role {get;set;}
    public string UserName {get;set;}

}
private bool ValidationFunction(string username, string pwd, out User)
    {
        bool boolReturnValue = false;

        string s = "correct connection string";
        SqlConnection con = new SqlConnection(s);
        con.Open();
        string sqlUserName;
        sqlUserName = "SELECT UserName,Password,UserID,Role FROM Users WHERE UserName =@usr AND Password=@pwd";
        SqlCommand cmd = new SqlCommand(sqlUserName, con);
        cmd.Parameters.Add(new SqlParameter("usr", username));
        cmd.Parameters.Add(new SqlParameter("pwd", pwd));

        SqlDataReader reader = command.ExecuteReader();

        if (reader.Read())
        {
            boolReturnValue = true;
            User = new User(){UserName = username, UserID=reader.GetInt32(2), Role=reader.GetString(3)};
        }
        else
        {
            Session["UserName"] = "";
            boolReturnValue = false;
        }
        return boolReturnValue;
    }

如果用户表中有
UserID
Role
,则可以使用下面的代码。它还具有使用参数防止SQL注入攻击的额外好处

private class User
{
    public int UserID {get;set;}
    public string Role {get;set;}
    public string UserName {get;set;}

}
private bool ValidationFunction(string username, string pwd, out User)
    {
        bool boolReturnValue = false;

        string s = "correct connection string";
        SqlConnection con = new SqlConnection(s);
        con.Open();
        string sqlUserName;
        sqlUserName = "SELECT UserName,Password,UserID,Role FROM Users WHERE UserName =@usr AND Password=@pwd";
        SqlCommand cmd = new SqlCommand(sqlUserName, con);
        cmd.Parameters.Add(new SqlParameter("usr", username));
        cmd.Parameters.Add(new SqlParameter("pwd", pwd));

        SqlDataReader reader = command.ExecuteReader();

        if (reader.Read())
        {
            boolReturnValue = true;
            User = new User(){UserName = username, UserID=reader.GetInt32(2), Role=reader.GetString(3)};
        }
        else
        {
            Session["UserName"] = "";
            boolReturnValue = false;
        }
        return boolReturnValue;
    }
使用查询

SqlDataReaer reader= Select *from Users where password="yourPassword"
然后你可以得到你想要的任何东西,即
阅读器[“用户名”]
等使用查询

SqlDataReaer reader= Select *from Users where password="yourPassword"

然后你可以得到你想要的任何东西,例如,
reader[“userName”]
etc

而不是executeScalar,你需要ExecuteReader,它的结果将包含一行你需要读取的内容(),这将包含你选择的列。的确,MSDN上的任何样本都可以做到这一点。顺便说一句,永远不会。。。。曾经像在SQL查询中那样连接字符串。我可以将“blah”或1=1;--”作为我的用户名传递,并且每次都将成功进行身份验证。您需要的不是executeScalar,而是ExecuteReader,其结果将包含一行,您需要读取(),该行将包含您选择的列。的确,MSDN上的任何样本都可以做到这一点。顺便说一句,永远不会。。。。曾经像在SQL查询中那样连接字符串。我可以将“blah”或1=1;--”作为我的用户名,并且每次都可以成功地进行身份验证。例如,作为用户名,“blah”或1=1;--“可以做到这一点。在尝试登录时,将这些密码散列存储并比较散列值也是一个好主意。@Garrisonely:当然,这是设计问题。确保用户名唯一也是另一个问题。Microsoft Membership System是一个很好的系统,它支持角色管理和哈希密码+.net支持。例如,作为用户名,“blah”或1=1;-”就可以解决这个问题。在尝试登录时,将这些密码哈希存储并比较哈希值也是一个好主意。@Garrisonely:当然,这些都是设计问题。确保用户名唯一也是另一个问题。Microsoft会员系统是一个很好的系统,支持角色管理和哈希密码+.net支持。