用户登录模块使用c#代码,密码加密不起作用

用户登录模块使用c#代码,密码加密不起作用,c#,C#,Iam正在从sql users表中检索使用HASHBYTES SHA1加密密码的用户 数据集仅检索空值 public class UserModelClass { public int Id { get; set; } public string UserName { get; set; } public int Authentication { get; set; } public string FullName { get; set; } publi

Iam正在从sql users表中检索使用HASHBYTES SHA1加密密码的用户

数据集仅检索空值

public  class UserModelClass
{
    public int Id { get; set; }
    public string UserName { get; set; }
    public int Authentication { get; set; }
    public string FullName { get; set; }
    public string Address { get; set; }
    public string Tel { get; set; }
    public int Encrypted { get; set; }
    public string Password { get; set; }
}

public static DataTable CheckUser(UserModelClass us)
{
    string sql = "SELECT TOP 1 1 FROM Users WHERE UserName=@username AND Password=HASHBYTES('SHA1',@Password)";
    using (SqlConnection con = new SqlConnection(DbConnection.GetConnectionString()))
    {
        using (SqlDataAdapter adapter = new SqlDataAdapter(sql, con))
        {
            adapter.SelectCommand.Parameters.AddWithValue("@UserName", us.UserName);
            adapter.SelectCommand.Parameters.AddWithValue("@password",  us.Password);
            DataSet ds = new DataSet();
            adapter.Fill(ds, "Users");
            return ds.Tables[0];
        }
    }
}

您需要更改此行:

string sql = "SELECT TOP 1 1 FROM Users WHERE UserName=@username AND Password=HASHBYTES('SHA1',@Password)";
string sql = "SELECT TOP 1 1 FROM Users WHERE UserName=@username AND Password= convert(nvarchar(max),HASHBYTES('SHA1',@password),1)";
这一行:

string sql = "SELECT TOP 1 1 FROM Users WHERE UserName=@username AND Password=HASHBYTES('SHA1',@Password)";
string sql = "SELECT TOP 1 1 FROM Users WHERE UserName=@username AND Password= convert(nvarchar(max),HASHBYTES('SHA1',@password),1)";

nvarchar(max)必须与表上的数据类型匹配,根据密码字段上的类型更改它。

@Uwe-Keim仍不工作。表返回null值我需要知道列的字段属性(类型和维度)在Sqlserver中Database@AshikDangol我已经更新了我的首字母answer@AshikDangol如果我的答案是正确的,请给我反馈