Example #1
0
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <returns></returns>
        public ActionResult GoLogin(string userName, string pwd, string validateCode)
        {
            AjaxResult result = new AjaxResult();
            OperatorRule operatorRule = new OperatorRule();
            #if DEBUG
            validateCode = Session["ValidateCode"].ToString();
            #endif
            if (validateCode != Session["ValidateCode"].ToString())
            {
                result.Success = false;
                result.Message = "验证码输入错误。";
            }
            else
            {
                Logon logon = new Logon() { Password = pwd, Username = userName };
                if (UserManager.ValidateUser(logon, Response))
                {
                    List<Ticket> currentTicketList = new List<Ticket>();
                    if (HttpContext.Cache["UserList"] != null)
                    {
                        currentTicketList = HttpContext.Cache["UserList"] as List<Ticket>;
                    }
                    if (currentTicketList.Count == 1)
                    {
                        //MyTicket.CurrentTicket = currentTicketList[0]; //唯一角色的用户直接进入系统
                        result.Success = true;
                        result.Url = "/Home/Index";
                        //记录登录日志
                        LoginLog log = new LoginLog();
                        log.OperatorID = MyTicket.CurrentTicket.UserID;
                        log.CreateTime = DateTime.Now;
                        log.Type = 1;
                        log.ID = WebHelper.GetNewGuidUpper();
                        new LoginLogRule().Add(log);
                        return Json(result, JsonRequestBehavior.AllowGet);
                    }
                    else
                    {
                        return Json(currentTicketList, JsonRequestBehavior.AllowGet);
                    }
                }
                else
                {
                    result.Success = false;
                    result.Message = "用户名或者密码错误。";
                    return Json(result, JsonRequestBehavior.AllowGet);
                }
                List<dynamic> userList = operatorRule.Login(userName, pwd);
                if (userList == null || userList.Count == 0)
                {
                    result.Success = false;
                    result.Message = "用户名或者密码错误。";

                }
                else
                {
                    List<Ticket> currentTicketList = new List<Ticket>();
                    foreach (dynamic t in userList)
                    {
                        if (currentTicketList.Count<Ticket>(ct => ct.GroupName == t.GROUPNAME) > 0)
                        {
                            continue;//同一用户多账号相同角色去重复
                        }
                        Ticket myTicket = new Ticket();
                        myTicket.DeptID = t.DEPTID;
                        myTicket.DeptName = t.DEPTNAME;
                        myTicket.EmployeeID = t.EMPID;
                        myTicket.EmployeeName = t.EMPNAME;
                        myTicket.GroupID = t.GROUPID;
                        myTicket.GroupName = t.GROUPNAME;
                        myTicket.UserID = t.ID;
                        myTicket.UserName = t.OPERNAME;
                        myTicket.IsAdmin = (t.ISADMIN == "1") ? true : false;
                        //myTicket.VoteList = new GroupVoteRule().GetOperVotes(t.GROUPID, t.ID);//获取权限列表
                        myTicket.VoteDic = new Dictionary<string, int>();
                        foreach (OperatorVote item in new GroupVoteRule().GetOperVotes(t.GROUPID, t.ID))
                        {
                            myTicket.VoteDic.Add(item.PoupID, item.VoteType);
                        }
                        //myTicket.CurrentOperator = operatorRule.GetModel(t.ID);
                        currentTicketList.Add(myTicket);
                    }
                    if (currentTicketList.Count == 1)
                    {
                        //MyTicket.CurrentTicket = currentTicketList[0];//唯一角色的用户直接进入系统
                        result.Success = true;
                        result.Url = "/Home/Index";
                        //记录登录日志
                        LoginLog log = new LoginLog();
                        log.OperatorID = MyTicket.CurrentTicket.UserID;
                        log.CreateTime = DateTime.Now;
                        log.Type = 1;
                        log.ID = WebHelper.GetNewGuidUpper();
                        new LoginLogRule().Add(log);
                    }
                    else
                    {
                        Session["currentUserList"] = currentTicketList;//记录角色列表,等待用户选择
                        return Json(currentTicketList, JsonRequestBehavior.AllowGet);
                    }
                }
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }
Example #2
0
        /// <summary>
        /// Authenticates a user via the MembershipProvider and creates the associated forms authentication ticket.
        /// </summary>
        /// <param name="logon">Logon</param>
        /// <param name="response">HttpResponseBase</param>
        /// <returns>bool</returns>
        public static bool ValidateUser(Logon logon, HttpResponseBase response)
        {
            bool result = false;

            if (Membership.ValidateUser(logon.Username, logon.Password))
            {
                // Create the authentication ticket with custom user data.
                var serializer = new JavaScriptSerializer();
                string userData = serializer.Serialize(UserManager.User);

                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
                        logon.Username,
                        DateTime.Now,
                        DateTime.Now.AddDays(30),
                        true,
                        userData,
                        FormsAuthentication.FormsCookiePath);

                // Encrypt the ticket.
                string encTicket = FormsAuthentication.Encrypt(ticket);

                //encTicket = ZipLib.Zip(encTicket);
                // Create the cookie.

                HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName);
                cookie.Expires = DateTime.Now.AddDays(1);
                cookie.Value = encTicket;
                response.AppendCookie(cookie);

                //response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));

                result = true;
            }

            return result;
        }