【问题标题】:How to compare database values with inputs through html form using c#如何使用c#通过html表单将数据库值与输入进行比较
【发布时间】:2015-03-04 19:54:14
【问题描述】:

这是模板的 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 中的值进行比较。

另外,我知道用于设置连接并将值与 db 进行比较以用于 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# 中获取 html 输入?
  • 您基本上是在要求社区为您编写后端登录代码,而没有提供您为尝试自己执行检查而编写的任何代码。您的问题不太可能以这种形式得到回答,所以我建议您尝试一下并展示您的努力。
  • @Tanner 刚刚提交了我的后端代码。
  • 不是您问题的答案,但请注意,像这样以纯文本形式存储您的密码并不是一个好习惯。
  • 注意 2 - 如果您不熟悉保护网站的最佳实践,那么自己动手做起来会很困难。建议您考虑使用内置的 ASP.net 身份验证功能。

标签: c# html sql-server


【解决方案1】:

您的代码中存在多个问题,我将仅指出其中的几个。在将此网站上线之前,请先对正确的 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。

【讨论】:

    猜你喜欢
    • 2021-06-20
    • 2018-03-26
    • 1970-01-01
    • 1970-01-01
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    相关资源
    最近更新 更多