I am trying to authenticate Admin Login in my AdminController, Here is what I have done so far:
[HttpPost]
public ActionResult AdminLogin(Admin admin)
{
Admin newAdmin = admin;
if (ModelState.IsValid)
{
Admin validAdmin = _iadmin.AuthenticateAdmin(admin);
if(newAdmin.AdminName == validAdmin.AdminName)
{
ViewBag.message = "Valid";
}
}
return View();
}
public Admin AuthenticateAdmin(Admin admin)
{
int count = 0;
using (SqlConnection conn = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(query, conn))
{
conn.Open();
command.Parameters.AddWithValue("AdminName", admin.AdminName);
command.Parameters.AddWithValue("AdminPassword", admin.AdminPassword);
SqlDataReader reader;
reader = command.ExecuteReader();
while (reader.Read())
{
count++;
}
reader.Close();
}
}
if (count==0)
{
admin.AdminName = "";
admin.AdminPassword = "";
}
return admin;
}
The problem is: when AuthenticateAdmin(admin)
is executed, validAdmin
and newAdmin
have the save values. From my understanding, newAdmin
should have the initial parameters passed by the admin and when AuthenticateAdmin
runs, validAdmin
should have new values set in the authenticate method.
Why is this an automatic pass by reference?
I have looked at someone asking the same question before but didn't help me much.