I'm doing a Login
, in which the user's password stored in the Database is of type varbinary(50)
I must recover this value, to later compare it with the key string
entered by the user.
In the Database I have the following:
password =
0x61646D696E
instring
isadmin
using theEncoding.ASCII
.
I have done the following (assume the connection cnn
is open):
string comando = string.Empty;
DataSet dataset = new DataSet();
byte[] claveByte = Encoding.ASCII.GetBytes(txt_clave.Text.Trim());
string claveString = "0x" + BitConverter.ToString(claveByte).Replace("-", string.Empty);
comando = @"select nombre_usuario, convert(varchar(50), password, 1) as clave from USUARIO
where nombre_usuario = '" + txt_usuario.Text.Trim() + "'" +
" and password =" + claveString;
using (SqlDataAdapter sentencia = new SqlDataAdapter(comando, cnn))
{
sentencia.Fill(dataset);
sentencia.Dispose();
}
byte[] claveObtenida = Encoding.ASCII.GetBytes(dataset.Tables[0].Rows[0]["clave"].ToString());
usuario = dataset.Tables[0].Rows[0]["nombre_usuario"].ToString();
clave = Encoding.ASCII.GetString(claveObtenida);
if (txt_usuario.Text == usuario && txt_clave.Text == clave)
{
//.........
//........
}
txt_usuario.Text = "admin";
andtxt_clave.Text = "admin";
, when converting the key obtained from the database tostring
I get the following:
Is there a better way to do it without involving the use of so many type conversions and being able to correctly buy the user's password?
Entordo: Visual Studio 2010 (WindowsForms) C #, .NET NetFrameWork 4.