【问题标题】:Checking database for username and password检查数据库的用户名和密码
【发布时间】:2014-12-03 15:49:57
【问题描述】:

我正在使用 asp.net 创建一个网站。到目前为止,我已经完成了一个注册页面,它将详细信息保存到数据库表中。

如何检查用户名和密码是否在该表中,然后允许他们继续到下一页?

这是我的注册码;

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["userinfo.ConnectionString"].ConnectionString);
conn.Open();

string insertQuery = "INSERT INTO [user] (UserName, FirstName, LastName, Email, Password, Country) VALUES (@uname, @fname, @lname, @email, @password, @country)";

SqlCommand comm = new SqlCommand(insertQuery, conn);

comm.Parameters.AddWithValue("@uname", usernametextbox.Text);
comm.Parameters.AddWithValue("@fname", fnametextbox.Text);
comm.Parameters.AddWithValue("@lname", lnametextbox.Text);
comm.Parameters.AddWithValue("@email", emailtextbox.Text);
comm.Parameters.AddWithValue("@country", DropDownListcountry.Text);
comm.Parameters.AddWithValue("@password", passwordtextbox.Text);

comm.ExecuteNonQuery();

conn.Close();

我猜我需要创建一个 SELECT 查询,可能带有一个 if 语句?

【问题讨论】:

标签: c# sql asp.net sql-server


【解决方案1】:

克里斯,

您对这个问题投了反对票,因为您提出的问题是您可以通过 Google 自己轻松解决的问题。

话虽如此,是的,您是正确的,您需要使用 SELECT 语句。我不会为您提供确切的 .NET 或 SQL 语法,因为那样会剥夺您的学习经验。但是,弄清楚如何进行 SELECT COUNT 查询。您基本上想计算提供的用户名和密码已经存在的行数。您可以使用 ExecuteScalar,而不是使用 ExecuteNonQuery,它返回单个值。

然后在您的 .NET 代码中,您将查看返回的计数值。如果为 0,则继续。如果大于 0,请执行其他操作。

话虽如此,.NET 有一些内置工具可以为您完成所有这些工作。找到它们并使用它们!

今后,在来这里寻求帮助之前,请尝试花更多时间自己做一些研究。

祝你好运!

【讨论】:

  • :-) 好吧,我看到其他人已经为您提供了确切的语法,以及内置 .NET 工具的名称(ASP 成员资格)。太糟糕了。自学对你更有帮助。
【解决方案2】:

如果您在登录页面中使用文本框输入用户名和密码。

      string connectionstring = WebConfigurationManager.ConnectionStrings["userinfo.ConnectionString"].ConnectionString;
                string sqlstring;
                sqlstring = "Select UserName,Password from user  where UserName='" + 
Texboxusername.Text + "' and Password ='" + Textboxpassword.Text + "'";

                SqlConnection con = new SqlConnection(connectionstring);
                SqlCommand command = new SqlCommand(sqlstring, con);

                System.Data.SqlClient.SqlDataReader reader;

                // open a connection with sqldatabase
                con.Open();

                reader = command.ExecuteReader();

                if (reader.Read())//Reader.read() true if found in database
                {

        Response.Redirect("userHome.aspx");
        }
        con.close();

第二种解决方案是使用表单身份验证。将登录从工具箱添加到您的登录设计页面。单击它两次。在它的同一代码中,但 Texboxusername.Text 和 Textboxpassword.Text 使用 Login.Username 和 Login.Password。

 if (reader.Read())
{ e.Authenticated = true;}
else{e.Authenticated=false;}

最后在 Web.config 中添加 <system.web> .. </system.web>;

   <forms loginUrl="login.aspx" defaultUrl="userHome.aspx" timeout="60"/>
    </authentication>
    <authorization>
      <deny users="?"/>
    </authorization>

经过身份验证的用户将自动重定向到 defaultUrl,并且人们只能从 loginUrl 访问,这要归功于 . 如果你有关于 UnobtrusiveValidationMode 的错误。 在&lt;configuration&gt;... &lt;/configuration&gt;;里面添加这个

<add key="ValidationSettings:UnobtrusiveValidationMode" value="none"/>

【讨论】:

  • 嗯,我同意凯西。显然你不想强迫自己学习,但想看到结果。但我喜欢直接给出解决方案,因为我还是有点懒:p
猜你喜欢
  • 2010-12-12
  • 1970-01-01
  • 2012-07-17
  • 2013-11-25
  • 2012-08-29
  • 1970-01-01
  • 1970-01-01
  • 2017-11-29
  • 1970-01-01
相关资源
最近更新 更多