public ActionResult LoginControl(AdminInformationsTable com) { if (ModelState.IsValid) { _core.Log(com.AdminName, com.AdminPassword); if (_core.status == true) { const int userId = 1; const string role = "Admin"; string userData = userId.ToString(CultureInfo.InvariantCulture) + "," + com.AdminName.Trim() + "," + role; FormsAuthenticationTicket ticket = new FormsAuthenticationTicket( 1, com.AdminName, DateTime.Now, DateTime.Now.AddMinutes(120), false, userData, FormsAuthentication.FormsCookiePath); string encryptedTicket = FormsAuthentication.Encrypt(ticket); HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket); cookie.HttpOnly = true; Response.Cookies.Add(cookie); return(RedirectToAction("Index", "AdmArticles")); } else { return(RedirectToAction("Index", "Login/")); } } return(View()); }
public void Log(string userName, string userPassword) { try { AdminInformationsTable adminTable = (from p in _loginContext.AdminInformations select p).First(); if (userName == adminTable.AdminName && userPassword == adminTable.AdminPassword) { status = true; } else { status = false; } } catch (Exception) { _adminMainTable.AdminName = "admin"; _adminMainTable.AdminPassword = "******"; using (MyWebContext db = new MyWebContext()) { db.AdminInformations.Add(_adminMainTable); db.SaveChanges(); } _adminMainTable = (from p in _loginContext.AdminInformations select p).First(); if (userName == _adminMainTable.AdminName && userPassword == _adminMainTable.AdminPassword) { status = true; } else { status = false; } } }
public LoginCore() { _adminMainTable = new AdminInformationsTable(); _loginContext = new MyWebContext(); }