/// <summary> /// Authenticates a user against a database, web service, etc. /// </summary> /// <param name="username">Username</param> /// <param name="password">Password</param> /// <returns>User</returns> public static List<Ticket> AuthenticateUser(string username, string password) { List<Ticket> currentTicketList = new List<Ticket>(); OperatorRule operatorRule = new OperatorRule(); List<dynamic> userList = operatorRule.Login(username, password); if (userList == null || userList.Count == 0) { return null; } else { 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); } //Cache["currentUserList"] = currentTicketList; return currentTicketList; } }
/// <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); }
public static bool ChangeRole(Ticket currentTicket, HttpResponseBase response) { bool result = false; // Create the authentication ticket with custom user data. var serializer = new JavaScriptSerializer(); string userData = serializer.Serialize(currentTicket); FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, currentTicket.UserName, DateTime.Now, DateTime.Now.AddDays(30), true, userData, FormsAuthentication.FormsCookiePath); // Encrypt the ticket. string encTicket = FormsAuthentication.Encrypt(ticket); // Create the cookie. response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket)); return true; }