public ActionResult Loginon(LoginDto loginModel)
        {
            if (string.IsNullOrEmpty(loginModel.uname))
            {
                ModelState.AddModelError("err", "用户名不能为空");
            }
            if (string.IsNullOrEmpty(loginModel.pwd))
            {
                ModelState.AddModelError("err", "密码不能为空");
            }
            try
            {
                var user         = _userApp.LoginValidate(loginModel.uname.Trim(), loginModel.pwd.Trim());
                var loginUserDto = new LoginUserDto();
                if (user != null)
                {
                    loginUserDto.Id          = user.id;
                    loginUserDto.AccountName = user.account_name;
                    loginUserDto.RealName    = user.real_name;
                    loginUserDto.IsSuper     = user.is_super;
                    loginUserDto.City        = user.city;
                    loginUserDto.County      = user.county;

                    loginUserDto.UserLevel = user.user_level;
                    //if (user.account_name.ToLower() == "admin")
                    //{
                    //    loginUserDto.IsSuper = true; //user.IsSuper;
                    //}
                    //else
                    //{
                    //    loginUserDto.IsSuper = user.is_super; //user.IsSuper;
                    //}
                    loginUserDto.SysRoleId = user.sys_role_id;
                    //设置cookie
                    // FormsAuthentication.SetAuthCookie(loginUserDto.AccountName, false);
                    string claimstr = loginUserDto.ToJson();
                    CookieHelper.WriteLoginCookie(claimstr);

                    return(Redirect("/Home"));
                }
                ModelState.AddModelError("err", "用户名或密码错误");
            }
            catch (Exception e)
            {
                LogNHelper.Exception(e);
                ModelState.AddModelError("err", "登录异常");
            }

            return(View("Index", loginModel));
        }
Exemplo n.º 2
0
        public ActionResult Loginon(LoginDto loginModel)
        {
            if (string.IsNullOrEmpty(loginModel.uname))
            {
                ModelState.AddModelError("err", "用户名不能为空");
            }
            if (string.IsNullOrEmpty(loginModel.pwd))
            {
                ModelState.AddModelError("err", "密码不能为空");
            }
            try
            {
                login_info loginInfo = new login_info();
                var        userAgent = RequestHelper.UserAgent();
                if (userAgent != null)
                {
                    loginInfo.login_name   = loginModel.uname.Trim();
                    loginInfo.browser      = userAgent.Browser;
                    loginInfo.device_info  = userAgent.Device;
                    loginInfo.osinfo       = userAgent.OS;
                    loginInfo.request_ip   = userAgent.Ip;
                    loginInfo.request_time = DateTime.Now;
                    if (NetHelper.IsIntranetIP(loginInfo.request_ip))
                    {
                        loginInfo.real_address = "本地局域网";
                    }
                    else
                    {
                        var ipInfo = IpTool.Search(loginInfo.request_ip);
                        if (ipInfo != null)
                        {
                            loginInfo.real_address = $"{ipInfo.Province}-{ipInfo.City}";
                        }
                        //string filePath = AppDomain.CurrentDomain.BaseDirectory + @"data\ip2region.db";
                        //DbSearcher dbSearcher = new DbSearcher(filePath);
                        //var dataBlock = dbSearcher.BtreeSearch("120.195.209.125");
                        //loginInfo.real_address = dataBlock.ToString();
                    }
                }
                var user         = _userApp.LoginValidate(loginModel.uname.Trim(), loginModel.pwd.Trim());
                var loginUserDto = new LoginUserDto();
                if (user != null)
                {
                    if (user.user_avatar.IsEmpty())
                    {
                        user.user_avatar = "/ui/images/profile.jpg";
                    }
                    loginUserDto.Id        = user.id;
                    loginUserDto.LoginName = user.login_name;
                    loginUserDto.UserName  = user.user_name;
                    loginUserDto.IsSuper   = user.is_super == 1;
                    loginUserDto.DeptId    = user.dept_id;
                    loginUserDto.Avatar    = user.user_avatar;
                    loginUserDto.RoleId    = user.role_id;
                    loginUserDto.DeptName  = user.dept_name;
                    loginUserDto.Gender    = user.gender;
                    loginUserDto.Phone     = user.mobile_phone;
                    loginUserDto.Email     = user.email;
                    if (user.role_id > 0)
                    {
                        sys_role role = _roleApp.GetRoleById(user.role_id);
                        if (role != null)
                        {
                            loginUserDto.RoleCode = role.role_code;
                            loginUserDto.RoleName = role.role_name;
                        }
                    }

                    //插入登录信息
                    loginInfo.login_status  = 1;
                    loginInfo.login_message = "登录成功";
                    loginInfoApp.InsertLoginInfo(loginInfo);

                    //设置cookie
                    // FormsAuthentication.SetAuthCookie(loginUserDto.AccountName, false);
                    string claimstr = loginUserDto.ToJson();
                    RequestHelper.SetCookie(claimstr);


                    return(Redirect("/admin/Home/Index"));
                }

                //插入登录信息
                loginInfo.login_message = "用户名或密码错误";
                loginInfoApp.InsertLoginInfo(loginInfo);
                ModelState.AddModelError("err", "用户名或密码错误");
            }
            catch (Exception e)
            {
                LoggerHelper.Exception(e);
                ModelState.AddModelError("err", "登录异常");
            }

            return(View("Index", loginModel));
        }