public ActionResult Login(String returnUrl) { ViewBag.Title = "Login"; DAL.m_announcement dal_m_announcement = new DAL.m_announcement(); List<Models.m_announcement> m_announcement_list = dal_m_announcement.GetListModelByPage(true); try { //Remember me login. DAL.z_user dal_user = new DAL.z_user(); if (Request.Cookies["YourAppLogin"] != null) { string userAccount = Request.Cookies["YourAppLogin"].Values["loginUserAccount"]; if (userAccount != null) { Common.Encrypt Encrypt = new Common.Encrypt(); Models.z_user model_z_user = dal_user.GetLoginModel(Encrypt.DecryptString(userAccount.Trim())); if (model_z_user != null) { DAL.z_menu dal_z_menu = new DAL.z_menu(); List<Models.z_menu> z_menu_list = dal_z_menu.GetMenuModelListByUserID(model_z_user.ID); Session.Add("loginUserMenuList", z_menu_list); Session.Add("loginUserModel", model_z_user); if (!String.IsNullOrEmpty(returnUrl)) { return Redirect(returnUrl); } else { return RedirectToAction("Index", "Home"); } } } } ViewBag.errorMessage = "Please Login..."; return View(m_announcement_list); } catch { ViewBag.errorMessage = "Please Login..."; return View(m_announcement_list); } }
public override void OnActionExecuting(ActionExecutingContext filterContext) { Models.z_user model_z_user = (Models.z_user)filterContext.HttpContext.Session["loginUserModel"]; List<Models.z_menu> z_menu_list = (List<Models.z_menu>)filterContext.HttpContext.Session["loginUserMenuList"]; if (model_z_user != null && z_menu_list != null) { //检查是否有权限 String controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName; Boolean hasPermission = false; foreach (Models.z_menu item in z_menu_list) { if (item.VALUE.IndexOf("/" + controllerName) >= 0) { hasPermission = true; } } if (hasPermission) { filterContext.Controller.ViewBag.model_z_user = model_z_user; filterContext.Controller.ViewBag.z_menu_list = z_menu_list; } else { //没有权限 filterContext.Result = new ViewResult { ViewName = "NoPermission", ViewData = filterContext.Controller.ViewData }; } //检查是否有权限 } else //Session lost { //Remember me login. if (filterContext.HttpContext.Request.Cookies["YourAppLogin"] != null) { DAL.z_user dal_user = new DAL.z_user(); string userAccount = filterContext.HttpContext.Request.Cookies["YourAppLogin"].Values["loginUserAccount"]; if (userAccount != null) { Common.Encrypt Encrypt = new Common.Encrypt(); model_z_user = dal_user.GetLoginModel(Encrypt.DecryptString(userAccount.Trim())); if (model_z_user != null) { DAL.z_menu dal_z_menu = new DAL.z_menu(); z_menu_list = dal_z_menu.GetMenuModelListByUserID(model_z_user.ID); filterContext.HttpContext.Session.Remove("loginUserMenuList"); filterContext.HttpContext.Session.Remove("loginUserModel"); filterContext.HttpContext.Session.Add("loginUserMenuList", z_menu_list); filterContext.HttpContext.Session.Add("loginUserModel", model_z_user); //检查是否有权限 String controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName; Boolean hasPermission = false; foreach (Models.z_menu item in z_menu_list) { if (item.VALUE.IndexOf("/" + controllerName) >= 0) { hasPermission = true; } } if (hasPermission) { filterContext.Controller.ViewBag.model_z_user = model_z_user; filterContext.Controller.ViewBag.z_menu_list = z_menu_list; } else { //没有权限 filterContext.Result = new ViewResult { ViewName = "NoPermission", ViewData = filterContext.Controller.ViewData }; } //检查是否有权限 } } } else //没有session也没有cookies { filterContext.Result = new ViewResult { ViewName = "SessionLost", ViewData = filterContext.Controller.ViewData }; } } base.OnActionExecuting(filterContext); }
public ActionResult Login(FormCollection collection) { DAL.m_announcement dal_m_announcement = new DAL.m_announcement(); List<Models.m_announcement> m_announcement_list = dal_m_announcement.GetListModelByPage(true); try { var name = collection["name"].Trim(); var password = collection["password"].Trim(); var rememberMe = collection["rememberMe"]; var valicode = collection["valicode"].Trim(); var returnUrl = collection["returnUrl"]; //验证验证码 if (Session["ValidateCode"].ToString() != valicode) { ViewBag.errorMessage = "Incorrect verification code"; return View(m_announcement_list); } //此处验证用户名、密码 DAL.z_user dal_user = new DAL.z_user(); Models.z_user model_z_user = dal_user.GetLoginModel(name.Trim(), Common.Common.MD5(password.Trim())); if (model_z_user == null) { ViewBag.errorMessage = "User name or password is incorrect"; return View(m_announcement_list); } //验证成功 if (rememberMe == "on") { HttpCookie cookie = new HttpCookie("YourAppLogin"); Common.Encrypt Encrypt = new Common.Encrypt(); cookie.Values.Add("loginUserAccount", Encrypt.EncryptString(name.Trim())); cookie.Expires = DateTime.Now.AddDays(5); Response.Cookies.Add(cookie); } DAL.z_menu dal_z_menu = new DAL.z_menu(); List<Models.z_menu> z_menu_list = dal_z_menu.GetMenuModelListByUserID(model_z_user.ID); Session.Add("loginUserMenuList", z_menu_list); Session.Add("loginUserModel", model_z_user); if (!String.IsNullOrEmpty(returnUrl)) { return Redirect(returnUrl); } else { return RedirectToAction("Index", "Home"); } } catch { ViewBag.errorMessage = "Incorrect verification code"; return View(m_announcement_list); } }