Example #1
0
        public ActionResult Login(string account, string pwd)
        {
            bool   success = false;
            string msg     = "";

            EmployeeBll employeeBll = new EmployeeBll();

            DtoEmployee     employee = employeeBll.Login(account, pwd);
            CookieUserModel user     = null;

            if (employee != null)
            {
                success         = true;
                msg             = "登录成功";
                user            = new CookieUserModel();
                user.UserId     = employee.Bem_Id;
                user.UserName   = employee.Bem_Name;
                user.RoleId     = employee.Bro_Id;
                user.RoleName   = employee.Bro_Name;
                user.Grades     = employee.Bem_Grades;
                user.GradesList = CustomEnumHelper.ParseBinaryAnd(typeof(GradeEnum), employee.Bem_Grades).Keys.ToList();
            }
            else
            {
                msg = "登录失败";
            }
            LoginCookieHelper.SetCurrentUser(user);

            return(Json(new JsonSimpleResponse()
            {
                State = success, ErrorMsg = msg
            }));
        }
        public override bool ValidateUser(string username, string password)
        {
            EmployeeWithAuthes employeeWithAuthes = EmployeeBll.Login(username, password);

            if (employeeWithAuthes.employee != null && employeeWithAuthes.employee.id > 0)
            {
                if (username.Equals("admin"))
                {
                    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, username, DateTime.Now, DateTime.Now.AddMinutes(20), true, "{\"0\":\"all\"}", "/");
                    var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket));
                    cookie.HttpOnly = true;
                    HttpContext.Current.Response.Cookies.Add(cookie);
                    addCookieForEmployee(employeeWithAuthes.employee);

                    HttpContext.Current.Session["login_code"] = 0;
                    return(true);
                }
                else if (employeeWithAuthes.roleAuthes != null && employeeWithAuthes.roleAuthes.Length > 0)
                {
                    Dictionary <string, string> purviews = new Dictionary <string, string>(employeeWithAuthes.roleAuthes.Length);
                    foreach (RoleAuth auth in employeeWithAuthes.roleAuthes)
                    {
                        purviews.Add(auth.menu_id.ToString(), auth.purview);
                    }
                    //add userid
                    purviews.Add("-1", employeeWithAuthes.employee.id.ToString());
                    string roleString = Json.Encode(purviews);
                    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, username, DateTime.Now, DateTime.Now.AddMinutes(20), true, roleString, "/");
                    var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket));
                    cookie.HttpOnly = true;
                    HttpContext.Current.Response.Cookies.Add(cookie);
                    addCookieForEmployee(employeeWithAuthes.employee);

                    HttpContext.Current.Session["login_code"] = 0;
                    return(true);
                }
                else
                {
                    //未激活或权限未分配
                    HttpContext.Current.Session["login_code"] = -2;
                }
            }
            else
            {
                //用户名或密码错误
                HttpContext.Current.Session["login_code"] = -1;
            }
            return(false);
        }