简体   繁体   English

使用 asp.net 和 SQL Server 的登录屏幕

[英]Login screen using asp.net and SQL Server

I am trying to create a login page but but my Login button does not work.我正在尝试创建一个登录页面,但我的登录按钮不起作用。 I am selecting username and password from my sql server database.我正在从我的 sql server 数据库中选择用户名和密码。

Unfortunately, I get an error不幸的是,我收到一个错误

System.Data.SqlClient.SqlException: Incorrect syntax near '' System.Data.SqlClient.SqlException: '' 附近的语法不正确

on line 27:在第 27 行:

int temp = Convert.ToInt32(com.ExecuteScalar().ToString());

Code below:代码如下:

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connect"].ConnectionString);
con.Open();

string checkuser = "select * from tb_Login where Username='" + txtUsername.Text + "' and Password='" + txtPassword.Text + "' ";

SqlCommand com = new SqlCommand(checkuser, con);

int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
con.Close();

if (temp == 1)
{
    con.Open();
    string checkPass = "select Password from tb_Login where Username='" + txtUsername.Text + "'";

    SqlCommand passCom = new SqlCommand(checkPass, con);
    string password = passCom.ExecuteScalar().ToString().Replace(" ", "");

    if (password == txtPassword.Text)
    {
        Session["New"] = txtUsername.Text;
        Response.Write("Correct");
    }
    else
    {
        Response.Write("Not Correct");
    }
}
else
{
    Response.Write("Username not correct");
}

This line of code:这行代码:

string checkuser = "select * from tb_Login where Username='" + txtUsername.Text + "' and Password='" + txtPassword.Text + "' ";

Is sending a query to the database and asking: "Give me all the columns from tb_Login whose UserName is the value in the txtUsername box and the Password is in the txtPassword box."正在向数据库发送查询并询问:“给我来自 tb_Login 的所有列,其用户名是 txtUsername 框中的值,密码在 txtPassword 框中。”

Then this line will take the value of the first column of the first row and try to convert it to an integer and if it cannot it will fail:然后这一行将获取第一行第一列的值并尝试将其转换为整数,如果不能,它将失败:

int temp = Convert.ToInt32(com.ExecuteScalar().ToString());

Change your query to select one column only: the column you need.更改您的查询以仅选择一列:您需要的列。

Also make sure you read this question on Stack Overflow so you can see how your code is a security threat to your own application.还要确保您在 Stack Overflow 上阅读了这个问题,以便了解您的代码如何对您自己的应用程序构成安全威胁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM