Esempio n. 1
0
        public static void WriteLog(int user_id, string info, string ip, string target, int type, string device, LythenContext db)
        {
            Sys_Logs log = new Sys_Logs
            {
                log_content = info,
                log_device  = device,
                log_ip      = ip,
                log_target  = target,
                log_time    = DateTime.Now,
                log_type    = type,
                log_user_id = user_id
            };

            db.Sys_Logs.Add(log);
            try
            {
                db.SaveChanges();
            }
            catch (DbEntityValidationException ex)
            {
                StringBuilder errors = new StringBuilder();
                IEnumerable <DbEntityValidationResult> validationResult = ex.EntityValidationErrors;
                foreach (DbEntityValidationResult result in validationResult)
                {
                    ICollection <DbValidationError> validationError = result.ValidationErrors;
                    foreach (DbValidationError err in validationError)
                    {
                        errors.Append(err.PropertyName + ":" + err.ErrorMessage + "\r\n");
                    }
                }
                ErrorUnit.WriteErrorLog(errors.ToString(), "WriteLog");
            }
            catch (Exception e) { ErrorUnit.WriteErrorLog(e.ToString(), "WriteLog"); }
        }
Esempio n. 2
0
        public ActionResult Index(LoginModel model)
        {
            if (Session["token"] == null || Session["token"].ToString() != model.token)
            {
                ViewBag.msg = "登陆异常,请刷新页面后重新登陆。";
                return(View(model));
            }
            //List<SelectOption> options = DropDownList.SysRolesSelect();
            //ViewBag.ddlRoles = DropDownList.SetDropDownList(options);
            if (Session["checkCode"] == null)
            {
                ViewBag.msg = "验证码已过期,请点击验证码刷新后重新输入密码码。";
                return(View(model));
            }
            if (model.checkCode.ToUpper() != Session["checkCode"].ToString())
            {
                ViewBag.msg = "验证码不正确。";
                return(View(model));
            }
            //验证帐号密码
            var user = (from p in db.User_Infos
                        join uvr in db.User_vs_Roles
                        on p.user_id equals uvr.uvr_user_id
                        where p.user_name == model.userName
                        select p
                        ).FirstOrDefault();

            if (user == null)
            {
                ViewBag.msg = "姓名或密码输入不正确,请重新输入。";
                return(View(model));
            }
            string password = AESEncrypt.Encrypt(PasswordUnit.getPassword(PageValidate.InputText(model.password, 40).ToUpper(), user.user_salt));

            if (password != user.user_password)
            {
                ViewBag.msg = "用户密码不正确,请重新输入。";
                return(View(model));
            }
            if (user.user_state == 0)
            {
                ViewBag.msg = "您的帐号被锁定,暂时无法登陆。";
                return(View(model));
            }
            if (user.user_state != 1)
            {
                ViewBag.msg = "您的帐号异常,暂时无法登陆。";
                return(View(model));
            }
            //验证权限
            var role = (from uvr in db.User_vs_Roles
                        join r in db.Sys_Roles
                        on uvr.uvr_role_id equals r.role_id
                        where uvr.uvr_user_id == user.user_id
                        select new LoginRole
            {
                roleId = r.role_id,
                roleName = r.role_name
            }).FirstOrDefault();

            if (role == null || role.roleId == 0 || role.roleId > 5)
            {
                ViewBag.msg = "没有权限登陆所选角色。";
                return(View(model));
            }
            //功能权限
            var controlroles = (from r in db.Sys_Roles
                                join rvc in db.Role_vs_Controllers
                                on r.role_id equals rvc.rvc_role_id
                                where r.role_id == role.roleId
                                select rvc.rvc_controller
                                ).ToArray();

            Session["LoginRole"]    = role;
            Session["ControlRoles"] = controlroles;
            Session["UserInfo"]     = user;
            Session["realname"]     = user.real_name;
            DataCache.SetCache("user-roles-" + user.user_id, role);
            HttpCookie cookie;

            if (model.isRemember)
            {
                cookie         = new HttpCookie("name", Server.UrlEncode(model.userName));
                cookie.Expires = DateTime.Now.AddHours(1);
                Response.AppendCookie(cookie);
            }
            else if (Request.Cookies["username"] != null)
            {
                Response.Cookies.Remove("username");
            }

            //cookie = new HttpCookie("role", role.roleId.ToString());
            //cookie.Expires = DateTime.Now.AddYears(1);
            //Response.AppendCookie(cookie);

            FormsAuthentication.SetAuthCookie(user.user_id.ToString(), true);

            string ip       = IpHelper.GetIP();
            string loginDev = string.Format("{0}-{1}-{2}-{3}-{4}"
                                            , Request.Browser.Id
                                            , Request.Browser.MobileDeviceManufacturer
                                            , Request.Browser.MobileDeviceModel
                                            , Request.Browser.Platform
                                            , Request.Browser.Type
                                            );
            Sys_Logs log = new Sys_Logs
            {
                log_content = "登陆",
                log_time    = DateTime.Now,
                log_user_id = user.user_id,
                log_ip      = ip,
                log_device  = loginDev
            };

            user.user_login_times++;
            db.Sys_Logs.Add(log);
            db.Entry(user).State = EntityState.Modified;
            db.SaveChanges();
            Session.Remove("token");
            return(RedirectToRoute(new { controller = "Home", action = "Index" }));
        }
Esempio n. 3
0
 public static Sys_LogsView EntityMap(this Sys_Logs model)
 {
     return(Mapper.Map <Sys_Logs, Sys_LogsView>(model));
 }