Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

What is the purpose of the bolded code and how does it accomplish its purpose? i

ID: 3572060 • Letter: W

Question

What is the purpose of the bolded code and how does it accomplish its purpose?

if (!Page.IsPostBack) { ViewState["TimeOfFailedLogin"] = ""; }

lblCantLogIn.Visible = false; }

protected void cmdLogin_Click(object sender, EventArgs e) {

if (ViewState["TimeOfFailedLogin"].ToString() != "")

{ DateTime TimeOfFailedLogin = DateTime.Parse(ViewState["TimeOfFailedLogin"].ToString());

TimeSpan difference = DateTime.Now - TimeOfFailedLogin;

if (difference.Seconds < 7)

{ System.Threading.Thread.Sleep((7 - difference.Seconds) * 1000); } }

if (txtEmail.Text != "" && txtPassword.Text != "")

{ using (AttendanceModel.AttendanceEntities myEntities = new AttendanceModel.AttendanceEntities())

{ // See if we can find this user's email & password in the AppUser table.

var user = from u in myEntities.AppUsers where u.EMail == txtEmail.Text && u.Password == txtPassword.Text select u;

// Did we find the user?

if (user.Count() == 1)

{ System.Web.Security.FormsAuthentication.RedirectFromLoginPage(txtEmail.Text, chkKeepMeLoggedIn.Checked); }

else // We didn't find the user in the AppUser table...bad user name or password.

{ lblCantLogIn.Visible = true;

ViewState["TimeOfFailedLogin"] = DateTime.Now.ToString();

}

}

}

else lblCantLogIn.Visible = true;

}

}

Explanation / Answer

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Login : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { ViewState["TimeOfFailedLogin"] = ""; } lblCantLogIn.Visible = false; } protected void cmdLogin_Click(object sender, EventArgs e) { ValidateUser(); } /// /// Determine if the user should be authenticated /// private void ValidateUser() { if (ViewState["TimeOfFailedLogin"].ToString() != "") { DateTime TimeOfFailedLogin = DateTime.Parse(ViewState["TimeOfFailedLogin"].ToString()); TimeSpan difference = DateTime.Now - TimeOfFailedLogin; if (difference.Seconds < 7) { System.Threading.Thread.Sleep((7 - difference.Seconds) * 1000); } } if (txtEmail.Text != "" && txtPassword.Text != "") { using (AttendanceModel.AttendanceEntities myEntities = new AttendanceModel.AttendanceEntities()) { // See if we can find this user's email & password in the AppUser table. var user = from u in myEntities.AppUsers where u.EMail == txtEmail.Text && u.Password == txtPassword.Text select u; // Did we find the user? if (user.Count() == 1) { System.Web.Security.FormsAuthentication.RedirectFromLoginPage(txtEmail.Text, chkKeepMeLoggedIn.Checked); } else // We didn't find the user in the AppUser table...bad user name or password. { lblCantLogIn.Visible = true; // Failed login - capture the time ViewState["TimeOfFailedLogin"] = DateTime.Now.ToString(); } } } else { lblCantLogIn.Visible = true; } } }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote