I'm starting to work with webForms / aspx, I'm trying to implement a simple log but there is an error in what I'm trying to do.
I have this code in the backend that if the user is incorrect it returns a response value to AJAX to show an alert, if the user is correct, it adds a session variable and the redirection is done from AJAX
[WebMethod]
public static string buscarUser(string name, string pass)
{
SqlDataReader rd;
string resp = "";
using (SqlConnection cnn = new SqlConnection("Data Source=ALPHA-
2MYT322;Initial Catalog=Ejemplo1Login;Integrated Security=True"))
{
SqlCommand cmd = cnn.CreateCommand();
cmd.CommandText = "EXEC login @nn,@pp";
cmd.Parameters.Add("@nn", SqlDbType.VarChar, 50).Value = name;
cmd.Parameters.Add("@pp", SqlDbType.VarChar, 200).Value = pass;
cnn.Open();
rd = cmd.ExecuteReader();
if (rd.HasRows) //Si el dataReader contiene filas es que existe un usuario
{
while (rd.Read()) //Lee
{
HttpContext.Current.Session["nombre"]=
rd["nameUser"].ToString();
resp = "SI";
}
}
else
{
resp = "no";
}
}
return resp;
}
On the page to which I point I put this in the Load
protected void Page_Load(object sender, EventArgs e)
{
if (Session["nombre"] == null)
{
Response.Redirect("Login.aspx");
}
}
But every time I have a successful login (so the user exists), it redirects me to the page but it shows me this:
What should I add or remove in order to log in correctly?