/// <summary>
        /// 用户登录返回令牌
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public string GetUserToken(UserInfo user)
        {
            string uagin = actionContext.Request.Headers.UserAgent.TryToString().MD5();
            string rm    = Utils.GenPsw(11, 11);
            long   time  = Utils.GetUnixTime();
            string code  = string.Format("{0}-{1}-{2}-{3}", user.ID, uagin, rm, time);
            string token = EncryptUtil.Base64(code);
            string key   = (user.ID + uagin + time).MD5();

            RedisBase.Item_Set(key, user);
            RedisBase.ExpireEntryAt(key, DateTime.Now.AddDays(2));
            return(token);
        }
Beispiel #2
0
        public ActionResult DoLogin()
        {
            string userName = Request["LoginCode"];
            string userPwd  = Request["LoginPwd"];

            if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(userPwd))
            {
                return(new RedirectResult("/Login/Index"));
            }
            //new MailSender().SendMail();

            var account = new AccountService().Login(userName, EncryptUtil.Base64(userPwd));

            if (account == null)
            {
                //LoggerHelper.Log(Server.MapPath($"/Log/{DateTime.Now.ToString("yyyyMMdd")}.log"),LogType.Warning, $"用户名:{userName}的用户登录失败!原因:用户名密码不正确!\n");
                logger.Error($"用户名:{userName}的用户登录失败!原因:用户名密码不正确!");
                return(Json(new { Status = "FAIL" }));
            }
            else
            {
                //LoggerHelper.Log(Server.MapPath($"/Log/{DateTime.Now.ToString("yyyyMMdd")}.log"), LogType.Info, $"用户名:{userName}的用户登录成功!\n");
                logger.Info($"用户名:{userName}的用户登录成功!");
                //Session["CurrentAccount"] = account;  session容易失效


                //创建cookie对象
                HttpCookie CurrentAccountCookie = new HttpCookie("CurrentAccount");

                //将序列化之后的Json串以UTF-8编码,再存入Cookie
                CurrentAccountCookie.Value = HttpUtility.UrlEncode(JsonConvert.SerializeObject(account), Encoding.GetEncoding("UTF-8"));

                //将cookie写入到客户端
                System.Web.HttpContext.Current.Response.SetCookie(CurrentAccountCookie);

                //设置cookie保存时间
                CurrentAccountCookie.Expires = DateTime.Now.AddDays(2);
                return(Json(new { LoginAccount = account, Status = "OK" }));
            }
        }
Beispiel #3
0
        public ActionResult AddAccount([Bind(Exclude = "Repassword")] Account account)
        {
            Regex PasswordRegex = new Regex("^[a-zA-Z0-9]{6,12}$");


            //密码不为空
            if (!string.IsNullOrEmpty(account.Password))
            {
                if (!PasswordRegex.IsMatch(account.Password))
                {
                    return(Json(new { Status = "ERROR", Message = "密码必须为6~12有效字母和数字组合" }));
                }
            }
            else
            {
                account.Password = "******";
            }
            account.Password = EncryptUtil.Base64(account.Password);
            account.HomePage = account.HomePage ?? "";
            var no = 0;

            int.TryParse(Request["ICnumber"], out no);
            account.ICNumber     = accountService.LoadEntities(a => a.ID > 0).OrderByDescending(a => a.ICNumber).First().ICNumber + 1;
            account.Description  = account.Description ?? "";
            account.RegisterTime = DateTime.Now;
            account.EmployeeID   = employeeService.LoadEntities(e => e.EmployeeName == account.Username).FirstOrDefault().ID;
            if (accountService.AddEntity(account) != null)
            {
                var emp  = employeeService.LoadEntities(a => a.EmployeeName == account.Username).FirstOrDefault();
                var role = roleService.LoadEntities(a => a.ID == account.RoleID).FirstOrDefault();
                logger.Warn($"用户【{CookieHelper.GetCurrentAccount().Username}】 添加系统帐号为{account.Username},姓名为{emp.NickName} 角色为{role.RoleName},的{employeeService.GetDepartment(emp.DepartmentID).DepartmentName} 的系统帐号!\n");
                return(Json(new { Status = "OK" }));
            }

            return(Json(new { Status = "ERROR" }));
        }