C# 为什么登录控件C对任何用户名和密码进行身份验证
我正在创建一个登录表单,该表单对创建的数据库中的用户名和密码进行身份验证,代码运行时没有错误,但是当写入任何用户名或密码时,即使它不存在于它登录的数据库中,我需要它进行身份验证,并仅使数据库用户名登录C# 为什么登录控件C对任何用户名和密码进行身份验证,c#,asp.net,authentication,login-control,C#,Asp.net,Authentication,Login Control,我正在创建一个登录表单,该表单对创建的数据库中的用户名和密码进行身份验证,代码运行时没有错误,但是当写入任何用户名或密码时,即使它不存在于它登录的数据库中,我需要它进行身份验证,并仅使数据库用户名登录 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace WebApplication1
{
public partial class LoginTest : System.Web.UI.Page
{
private string strcon = WebConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
this.UnobtrusiveValidationMode = System.Web.UI.UnobtrusiveValidationMode.None;
}
private bool UserLogin(string un, string pw)
{
SqlConnection conn = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand("Select id from student where id=@un and Password=@pw", conn);
cmd.Parameters.AddWithValue("@un", un);
cmd.Parameters.AddWithValue("@pw", pw);
conn.Open();
string result = Convert.ToString(cmd.ExecuteScalarAsync());
if (String.IsNullOrEmpty(result)) return false; return true;
}
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
string un = Login1.UserName;
string pw = Login1.Password;
bool result = UserLogin(un, pw);
if (result)
{
e.Authenticated = true;
Session["username"] = un;
}
else e.Authenticated = false;
}
}
}
cmd.ExecuteScalarAsync()
返回一个任务
对象。将其转换为字符串将始终成功;它将创建一个字符串“System.Threading.Tasks.Task”,该字符串肯定不是空的或空的
您需要等待调用ExecuteScalarAsync()
,或者需要调用ExecuteScalarAsync().Result
,它将一直阻塞,直到查询操作完成
此外,请确保您正在制作的任何应用程序(即使您认为它是一个玩具)都使用了正确的单向密码哈希。这里有很多关于Stackoverflow和其他地方关于如何安全存储密码的建议。您是否验证了从“结果”中得到的信息?设置一个断点,并检查无效用户名/密码的“结果”值。此外,还应说明:密码,不应,永远,可以在数据库中以纯文本形式存储。这样做是非常不安全的,可能会让你面临法律问题。Salt和hash存储在数据库中的所有密码。