Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用c通过html表单将数据库值与输入进行比较_C#_Html_Sql Server - Fatal编程技术网

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

C# 如何使用c通过html表单将数据库值与输入进行比较,c#,html,sql-server,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" placeholde

以下是模板的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/密码如何,每次单击submit按钮都会加载我打算重定向用户的页面

问题:我想做的是使用c将这里2个输入的值与SQLServer数据库中的值进行比较

此外,我还知道用于设置连接并将值与web窗体的db进行比较的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编程的研究,因为这是完全错误的

一,。如果您使用带有runat属性的输入字段,您可以使用它们的ID在代码隐藏中访问它们的值!这比在请求集合中搜索它们要好得多

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

string getTextValuesPassword = Page.Request.Form["password"].ToString();
你可以说

string myPassword = password.Text;
二,。您应该学会关闭SqlConnection并处理外部资源

三,。每次存储用户密码时,都不应将其存储为纯文本!!!尽快了解正确的散列

四,。永远不要在.cs文件中存储这样的连接字符串。它可以更改,或者您可能必须在多个位置使用它。至少将其存储在web.config中

5


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

验证之前是否需要阻止页面重定向?这是你的问题吗?或者你需要在你的c中输入html吗?你基本上是要求社区为你编写你的后端登录代码,而不提供你自己编写的任何代码来尝试执行检查。您的问题不太可能在这种形式下得到回答,因此我建议您尝试一些东西并展示您的努力。@Tanner只是提供了我的后端代码。这不是对您问题的回答,但会注意到以纯文本形式存储您的密码不是一种好的做法。注意2-如果您不熟悉保护网站的最佳做法,你自己滚是很难的。建议您考虑使用内置的ASP.NET身份验证功能。