protected override void OnAuthentication(AuthenticationContext filterContext) { var cookies = filterContext.HttpContext.Request.Cookies; if (cookies[Cookies.B_ADMIN_LOGIN_TOKEN] == null) { filterContext.Result = RedirectToLoginPage(Request.Url.ToString()); return; } // expired session var dangNhap = dangNhapRepository.CheckLogin(cookies[Cookies.B_ADMIN_LOGIN_TOKEN].Value, new TimeSpan(31, 0, 0, 0)); if (dangNhap != null) { LoggedInUser = nguoiDungRepository.GetByID(dangNhap.NguoidungID); // update last_login dangNhap.Thoigiandangnhap = DateTime.Now; dangNhap.Solandangnhapsai = 0; adminUow.SubmitChanges(); } if (LoggedInUser == null) { filterContext.Result = RedirectToLoginPage(Request.Url.ToString()); return; } // cache RequestScope.LoggedInUser = LoggedInUser; base.OnAuthentication(filterContext); }
/// <summary> /// create admin account if no account exists /// </summary> public ActionResult Seed() { int count = nguoiDungRepository.GetAll().Count(); if (count == 0) { var password = AuthHelpers.CreatePassword("123456"); adminUow.BeginTransaction(); try { var nguoiDung = new HDNHD.Models.DataContexts.Nguoidung() { Taikhoan = "admin", Isadmin = true, Matkhau = password }; nguoiDungRepository.Insert(nguoiDung); adminUow.SubmitChanges(); var dangNhap = new HDNHD.Models.DataContexts.Dangnhap() { NguoidungID = nguoiDung.NguoidungID, Solandangnhapsai = 0 }; dangNhapRepository.Insert(dangNhap); adminUow.SubmitChanges(); adminUow.Commit(); return(Content("Created user: '******' successfully!")); } catch (Exception e) { adminUow.RollBack(); return(Content("Fail to create seeding user: '******'.")); } } return(Content("Users exist. Seeding aborted!")); }