无效的用户名。无法从数据库中读取用户名(ASP.net)

时间:2017-11-07 14:24:41

标签: c# asp.net

我正在尝试创建一个连接到我的数据库的登录页面。但它显示用户无效。我可以知道这是什么问题吗?谢谢! 这是完整的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class Login : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ToString());
        conn.Open();
        string checkuser = " select count(*) from [Table] where UserName = ' " + TextBoxUserName.Text + "' and Password='"+ TextBoxPassword.Text + "' ";
        SqlCommand com = new SqlCommand(checkuser, conn);
        int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
        if (temp == 1)
        {
            Session["user"] = TextBoxUserName.Text;
            Response.Write("Login success");
        }
        else
        {
            Response.Write("Login fail");
        }

    } 

1 个答案:

答案 0 :(得分:1)

首先回答你的问题:你有一个空间,你不应该有空间。 where UserName = ' " 系统也会检查这个空间,这是错误的。你只需要TextBox内容,我是对的。

第二件事是这段代码的安全性。您的代码非常容易受到sql注入,并且密码永远不应该是纯文本,哈希是更好的答案。