public ActionResult Create() { string username = Request.Form["userName"]; string password = Request.Form["password"]; if (username == "") { ViewBag.ErrorMessage = "Please enter a username"; return(View("New")); } else if (username.Length > 30) { ViewBag.ErrorMessage = "Username must be between 1 and 30 characters."; return(View("New")); } else if (ERUser.Exists(username)) { ViewBag.ErrorMessage = $"The username {username} is already taken."; return(View("New")); } else { ERUser newUser = ERUser.Create(username, password); Response.Cookies.Append("username", username); return(Redirect("/")); } }
public ActionResult Login() { string username = Request.Form["userName"]; string password = Request.Form["password"]; if (username == "" || password == "") { ViewBag.ErrorMessage = "Please enter a username"; return(View("LoginPage")); } else if (!ERUser.Exists(username)) { ViewBag.ErrorMessage = "Please enter a valid username."; return(View("LoginPage")); } else { ERUser user = ERUser.Get(username); if (user.ValidatePassword(password)) { Response.Cookies.Append("username", username); return(Redirect("/")); } else { ViewBag.ErrorMessage = "Incorrect username or password."; return(View("LoginPage")); } } }
public static void SetLoginData(Microsoft.AspNetCore.Http.HttpRequest request, dynamic viewbag) { viewbag.showLoginInfo = true; string username = request.Cookies["username"]; if (ERUser.Exists(username)) { viewbag.user = ERUser.Get(username); viewbag.isLoggedIn = true; } else { viewbag.user = null; viewbag.isLoggedIn = false; } }