Пример #1
0
 public ActionResult SaveAuth(TreeInfo info)
 {
     try
     {
         //删除之前的角色菜单关系
         var list = RoleForMenuManager.GetInstance().SelectList(m => m.roleid == info.roleid);
         if (list.Count > 0)
         {
             RoleForMenuManager.GetInstance().RemoveList(list);
             RoleForMenuManager.GetInstance().Save();
         }
         foreach (var item in info.list)
         {
             if (item.id > 0)            //导航菜单排除在外
             {
                 var model = new bjf_roleformenu()
                 {
                     roleid     = info.roleid,
                     menuid     = item.id,
                     createid   = Convert.ToInt32(SessionHelper.GetSession("userid")),
                     updateid   = Convert.ToInt32(SessionHelper.GetSession("userid")),
                     createtime = DateTime.Now,
                     updatetime = DateTime.Now
                 };
                 RoleForMenuManager.GetInstance().Add(model);
             }
         }
         RoleForMenuManager.GetInstance().Save();
         return(Json(new { state = 0, msg = "保存成功" }));
     }
     catch (Exception e)
     {
         LogHelper.log.Error(e.Message);
         return(Json(new { state = 1, msg = e.Message }));
     }
 }
Пример #2
0
        public ActionResult Login(LoginQuery query)
        {
            try
            {
                if (Session["ValidateCode"] == null)
                {
                    return(Json(new { state = 1, msg = "验证码已过期,请重新输入" }));
                }
                if (Session["ValidateCode"].ToString().ToLower() != query.ValidCode.ToLower())  //验证码忽略大小写
                {
                    return(Json(new { state = 1, msg = "验证码错误" }));
                }

                //判断用户信息
                var item = UserManager.GetInstance().GetItemByLoginName(query.LoginName);
                if (item == null)
                {
                    return(Json(new { state = 1, msg = "此用户不存在." }));
                }
                if (query.PassWord != EncryAndDecryptHelper.Decryption(item.password))
                {
                    return(Json(new { state = 1, msg = "用户密码不对." }));
                }
                var roleList = UserForRoleManager.GetInstance().GetListRole(item.id);
                if (roleList.Count == 0)
                {
                    return(Json(new { state = 1, msg = "当前用户暂未分配角色,不能使用系统." }));
                }

                //用户全部角色集合
                string arrRoleid = "";
                roleList.ForEach(m => arrRoleid += m.roleid + ",");

                //用户可查看菜单集合
                var    menulist  = RoleForMenuManager.GetInstance().GetListMenuId(roleList);
                string arrMenuid = "";
                menulist.ForEach(m => arrMenuid += m.ToString() + ",");

                //记住用户名和密码
                if (query.Rember != null)
                {
                    //放进cookie中
                    Response.Cookies.Add(new HttpCookie("cookieLoginname", query.LoginName));
                    Response.Cookies.Add(new HttpCookie("cookiePassword", query.PassWord));
                    Response.Cookies["cookieLoginname"].Expires = DateTime.Now.AddDays(3);
                    Response.Cookies["cookiePassword"].Expires  = DateTime.Now.AddDays(3);
                }
                //验证通过将用户信息记录进session
                SessionHelper.SetSession("userinfo", item);
                SessionHelper.SetSession("userid", item.id);
                SessionHelper.SetSession("username", item.username);
                SessionHelper.SetSession("rolelist", arrRoleid);                //角色集合
                SessionHelper.SetSession("menulist", menulist);                 //菜单集合
                SessionHelper.SetSessionTimeout(20);

                //写登录日志
                LoginLogManager.GetInstance().SaveItem();
                return(Json(new { state = 0, msg = "登录成功." }));
            }
            catch (Exception e)
            {
                LogHelper.log.Error(e.Message);
                return(Json(new { state = 1, msg = e.Message }));
            }
        }