1
PROBLEM: I am unable to validate the user authenticated by the database. What happens is that when I run my application, putting valid user and password, it returns me false inside my loginOk located in the Homecontroller.
Code details
MODAL: In this class I have a Validarlogin method where it performs a check in the database to perform the query.
public class LoginModel
{
public string Email { get; set; }
public string Senha { get; set; }
public bool ValidarLogin()
{
// Comando para fazer a consulta
string consultaSql = $"Select VendedorId From Estudo.Vendedores Where Email='{Email}' and Senha='{Senha}'";
DAL Acessar = new DAL();
DataTable carregaTabela = new DataTable(consultaSql); // Comando para executar a consulta.
if (carregaTabela.Rows.Count == 1)
{
return true;
}
else
{
return false;
}
}
}
CONNECTION CLASS:
public class DAL
{
// Variaveis de conexão
private static string NomeServidor = @"DESKTOP-DC0D3FT\THIAGO";
private static string TipoAutenticacao = "SSPI";
private static string NomeBanco = "SistemaDeVenda";
private static string StringConexao = $"Data source={NomeServidor}; Integrated Security={TipoAutenticacao}; Initial Catalog={NomeBanco}" ;
private static SqlConnection Conexao;
//@"data source=DESKTOP-DC0D3FT\THIAGO; Integrated Security=SSPI;Initial Catalog=SistemaDeVenda"
public DAL()
{
Conexao = new SqlConnection(StringConexao);
Conexao.Open();
}
public DataTable RetDataTable(string sql)
{
DataTable data = new DataTable();
SqlCommand Executar = new SqlCommand(sql, Conexao);
SqlDataAdapter da = new SqlDataAdapter(Executar);
da.Fill(data);
return data;
}
public void ExecutarComandoSQL(string sql)
{
SqlCommand Executar = new SqlCommand(sql, Conexao );
Executar.ExecuteNonQuery();
}
}
Home Controller
[HttpPost]
public IActionResult Login(LoginModel login)
{
bool loginOk = login.ValidarLogin();
return View();
}
If you perform the query (taking the result of the variable
consultaSql
) SQL returns the correct value?– João Martins
Are you sure you want to do this in Viewmodel?
– Leandro Angelo
Ps.: The problem is not because you are returning to View Login without transporting Viewmodel?
– Leandro Angelo