简体   繁体   中英

User login module using c# code with password encryption not working

Iam retrieving the user from sql users table which password was encrypted using HASHBYTES SHA1.

Dataset retrive only null value.

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];
        }
    }
}

You need to change this line:

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

With this line:

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

nvarchar (max) must match the data type on the table change it based on the type you have on the password field

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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