c# - 如何使用 C# 通过 html 表单将数据库值与输入进行比较

标签 c# html sql-server

这是模板的 HTML 表单:

<form class="form-login" action="index.html">
            <h2 class="form-login-heading">sign in now</h2>
            <div class="login-wrap">
                <input type="text" class="form-control" placeholder="User ID" autofocus="autofocus">
                <br>
                <input type="password" class="form-control" placeholder="Password">
                <label class="checkbox">
                    <span class="pull-right">
                        <a data-toggle="modal" href="login.html#myModal"> Forgot Password?</a>

                    </span>
                </label>
                <button class="btn btn-theme btn-block" href="index.html" type="submit"><i class="fa fa-lock"></i> SIGN IN</button>

这是我对它的修改:

<form id="form1" runat="server" class="form-login" method="post" action="HomeDoc.aspx">
        <div>
            <h2 class="form-login-heading">sign in now</h2>
                    <div class="login-wrap">
                        <input type="text" class="form-control" placeholder="User ID" id="userid" runat="server" autofocus="autofocus"/>
                        <br/>
                        <input type="password" class="form-control" placeholder="Password" id="password" runat="server" />
                        <label class="checkbox">
                            <span class="pull-right">
                                <a data-toggle="modal" href="StaffLogin.aspx#myModal"> Forgot Password?</a>
                            </span>
                        </label>
                        <button class="btn btn-theme btn-block" runat="server" type="submit"><i class="fa fa-lock"></i> SIGN IN</button>

输出:到目前为止,每次我单击提交按钮时都会加载我打算将用户重定向到的页面,无论用户 ID/密码如何。

问题:我想做的是使用 C# 将此处的 2 个输入的值与我的 SQLServer 数据库中的值进行比较。

另外,我知道用于设置连接和将值与 db for web 表单进行比较的 c# 代码。那么,要对 html 表单输入的代码进行哪些具体更改?

请帮忙。谢谢。

编辑:很抱歉没有提供后端代码。这里(忽略任何琐碎的语法错误):

public partial class StaffLogin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Login_Click(object sender, EventArgs e)
{
    String getTextValuesUserID = Page.Request.Form["userid"].ToString();
    String getTextValuesPassword = Page.Request.Form["password"].ToString();

    //setting up connection with database 
    SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\v11.0;AttachDbFilename=C:\\Users\\Pavel\\Documents\\Visual Studio 2013\\WebSites\\IMS\\App_Data\\DatabaseIMS.mdf;Integrated Security=True");
    con.Open();
    SqlCommand cmd = new SqlCommand("select * from Doctor where userid=@userid and password=@password", con);
    cmd.Parameters.AddWithValue("@userid", getTextValuesUserID);
    cmd.Parameters.AddWithValue("@password", getTextValuesPassword);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill(dt);
    if (dt.Rows.Count > 0)
    {
        //  Response.Redirect("UserLoggedIn.aspx");
        Response.Redirect("HomeDoc.aspx");
    }
    else
    {
        //javascript for invalid username and password Alert box
        ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and password')</script>");
    }
}

最佳答案

您的代码中有多个问题,我只会指出其中的几个。在将此网站上线之前,请对正确的 C# 编程做一些研究,因为这完全是错误的...

1.) 如果您使用具有 runat 属性的输入字段,您可以使用它们的 ID 在代码隐藏中访问它们的值!比在 Request 集合中搜索要好得多

所以在你的情况下,而不是

string getTextValuesPassword = Page.Request.Form["password"].ToString();

你可以说

string myPassword = password.Text;

2.) 你应该学会关闭SqlConnection和处理外部资源

3.) 每次你存储用户密码时,你不应该以明文形式存储它!!!尽快了解正确的散列。

4.) 你永远不应该在 .cs 文件中存储这样的连接字符串。它可以更改,或者您可能必须在多个地方使用它。至少存储在 web.config 中

5.) .....

为了解决您的特定问题,您确实是在将值与数据库值进行比较,但是,您实际上并没有登录用户。您至少需要对基本的 Forms 身份验证进行一些研究,或者如果您需要更高级的方案,则可以使用 ASP.NET Identity。

关于c# - 如何使用 C# 通过 html 表单将数据库值与输入进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28829835/

相关文章:

C# GUI 线程错误

c# - 调用Invoke挂起程序

c# - WPF RichTextBox - ScrollToEnd() 方法不会导致发生任何滚动

javascript - JQuery 在位置插入表格行

sql-server - 如何使用Insert Into在Sql Server中减去负小数

c# - 分数 API - 更新请求返回 HTTP 403 错误,(#200) 用户无法访问此应用程序

php - 更新产品数量(php和Mysql)

php搜索数据库行

sql-server - 删除文档中的重复记录(Elasticsearch)

mysql - 如何转动?如何将多行转换为多列的一行?