/// <summary>登录</summary> /// <param name="sender"></param> /// <param name="e"></param> protected void BtnLogin_Click(object sender, EventArgs e) { var ip = IpHelper.GetUserIp(); #region 获取用户输入的参数,并进行数据初步处理 //获取用户名,并进行危险字符过滤 var username = StringHelper.Left(txtUserName.Text, 50); //获取用户密码 var userpass = txtPassword.Text; //获取验证码 var strCode = StringHelper.Left(txtCaptcha.Text, 5); #endregion #region 初步验证 //开发测试使用,不用每次都输入帐号与密码 //username = "******"; //userpass = "******"; //strCode = "12345"; //用户名验证 if (string.IsNullOrEmpty(username.Trim())) { txtUserName.Focus(); FineUI.Alert.ShowInTop("用户名不能为空,请仔细检查您输入的用户名!", FineUI.MessageBoxIcon.Error); return; } //密码验证 if (string.IsNullOrEmpty(userpass.Trim())) { txtPassword.Focus(); FineUI.Alert.ShowInTop("密码不能为空,请仔细检查您输入的密码!", FineUI.MessageBoxIcon.Error); return; } //验证码验证 if (string.IsNullOrEmpty(strCode)) { txtCaptcha.Focus(); FineUI.Alert.ShowInParent("验证码不能为空!", FineUI.MessageBoxIcon.Error); return; } //判断验证码是否正确 //if (Session["vcode"] == null || !Session["vcode"].ToString().Equals(strCode, StringComparison.InvariantCultureIgnoreCase)) //{ // SessionHelper.RemoveSession("vcode"); // txtpass.Focus(); // //JsHelper.Alert("验证码错误!"); // FineUI.Alert.ShowInParent("验证码错误!", FineUI.MessageBoxIcon.Error); // return; //} //else //{ // //验证码正确,删除验证码Session // SessionHelper.RemoveSession("vcode"); //} #endregion #region 数据库验证 //通过用户给的用户名获取相关实体类 var userinfo = Manager.SingleOrDefault(x => x.LoginName == username); //判断用户是否存在 if (userinfo == null) { LoginLogBll.GetInstence().Save(0, "账号【" + username + "】不存在,登录失败!"); txtUserName.Focus(); FineUI.Alert.ShowInParent("用户名不存在,请仔细检查您输入的用户名!", FineUI.MessageBoxIcon.Error); return; } //密码不匹配 if (!userinfo.LoginPass.Equals(Encrypt.Md5(Encrypt.Md5(userpass)))) { LoginLogBll.GetInstence().Save(userinfo.Id, "账号【" + userinfo.LoginName + "】的用户【" + userinfo.CName + "】登录失败!登录密码错误。"); txtPassword.Focus(); FineUI.Alert.ShowInParent("您输入的用户密码错误!", FineUI.MessageBoxIcon.Error); return; } if (userinfo.IsWork == 0) { //添加用户登陆日志 LoginLogBll.GetInstence().Save(userinfo.Id, "离职用户登录失败!用户【" + userinfo.CName + "】试图登录系统"); FineUI.Alert.ShowInParent("您已经没有权限登录本系统!", FineUI.MessageBoxIcon.Error); return; } //判断当前账号是否被启用 if (userinfo.IsEnable == 0) { //添加登录日志记录 LoginLogBll.GetInstence().Save(userinfo.Id, "账号【" + userinfo.LoginName + "】的用户【" + userinfo.CName + "】登录失败!用户账号被禁用。"); FineUI.Alert.ShowInParent("当前账号未被启用,请联系管理人员激活!", FineUI.MessageBoxIcon.Error); return; } #endregion #region 存储在线用户资料 #region 获取用户操作权限 if (string.IsNullOrEmpty(userinfo.Position_Id)) { Session["PagePower"] = ""; Session["ControlPower"] = ""; LoginLogBll.GetInstence().Save(0, "账号【" + username + "】未绑定职位,请管理员进行配置!"); FineUI.Alert.ShowInParent("您的账号未绑定职位,请与管理员联系!", FineUI.MessageBoxIcon.Error); return; } else { //获取用户权限并存储到用户Session里 PositionBll.GetInstence().SetUserPower(userinfo.Position_Id); } #endregion #region 当前用户在线信息 //当前时间 var localTime = DateTime.Now.ToLocalTime(); //创建客户端信息获取实体 var clientHelper = new ClientHelper(Request); //创建在线用户实体 var onlineUser = new OnlineUsers(); //当前用户的Id编号 onlineUser.Manager_Id = userinfo.Id; onlineUser.Manager_LoginName = userinfo.LoginName; onlineUser.Manager_LoginPass = userinfo.LoginPass; onlineUser.Manager_CName = userinfo.CName; onlineUser.LoginTime = localTime; onlineUser.LoginIp = ip; //生成密钥 onlineUser.UserKey = RandomHelper.GetRndNum(32, true); //Md5(密钥+登陆帐号+密码+IP+密钥.Substring(6,8)) onlineUser.Md5 = OnlineUsersBll.GetInstence().GenerateMd5(onlineUser); HttpContext.Current.Session[OnlineUsersTable.Md5] = onlineUser.Md5; onlineUser.UpdateTime = localTime; onlineUser.Sex = userinfo.Sex; onlineUser.Branch_Id = userinfo.Branch_Id; onlineUser.Branch_Code = userinfo.Branch_Code; onlineUser.Branch_Name = userinfo.Branch_Name; onlineUser.Position_Id = userinfo.Position_Id; onlineUser.Position_Name = userinfo.Position_Name; onlineUser.CurrentPage = ""; onlineUser.CurrentPageTitle = ""; //SessionId onlineUser.SessionId = Session.SessionID; onlineUser.UserAgent = StringHelper.FilterSql(HttpContext.Current.Request.Headers["User-Agent"] + ""); onlineUser.OperatingSystem = clientHelper.GetSystem(); onlineUser.TerminalType = clientHelper.IsMobileDevice(onlineUser.UserAgent) ? 1 : 0; onlineUser.BrowserName = clientHelper.GetBrowserName(); onlineUser.BrowserVersion = clientHelper.GetBrowserVersion(); #endregion #region 记录当前用户UserId //定义HashTable表里Key的名称UserId string userHashKey = ""; //判断当前用户帐户是否支持同一帐号在不同地方登陆功能,取得用户在HashTable表里Key的名称 //不支持则 if (userinfo.IsMultiUser == 0) { userHashKey = userinfo.Id + ""; } //支持则 else { userHashKey = userinfo.Id + "_" + onlineUser.SessionId; } //记录用户的HashTable Key onlineUser.UserHashKey = userHashKey; Session[OnlineUsersTable.UserHashKey] = userHashKey; #endregion #region 将在线用户信息存入全局变量中 //运行在线数据加载函数,如果缓存不存在,则尝试加载数据库中的在线表记录到缓存中 //——主要用于IIS缓存被应用程序池或其他原因回收后,对在线数据进行重新加载,而不会使所有用户都被迫退出系统 var onlineUsersList = OnlineUsersBll.GetInstence().GetList(); //判断缓存中["OnlineUsers"]是否存在,不存在则直接将在线实体添加到缓存中 if (onlineUsersList == null || onlineUsersList.Count == 0) { //清除在线表里与当前用户同名的记录 OnlineUsersBll.GetInstence().Delete(this, x => x.Manager_LoginName == onlineUser.Manager_LoginName); //将在线实体保存到数据库的在线表中 OnlineUsersBll.GetInstence().Save(this, onlineUser, null, true, false); } //存在则将它取出HashTable并进行处理 else { //将HashTable里存储的前一登陆帐户移除 //获取在线缓存实体 var onlineModel = OnlineUsersBll.GetInstence().GetOnlineUsersModel(userHashKey); if (onlineModel != null) { //添加用户下线记录 LoginLogBll.GetInstence().Save(userHashKey, "用户【{0}】的账号已经在另一处登录,本次登陆下线!在线时间【{1}】"); //清除在线表里与当前用户同名的记录 OnlineUsersBll.GetInstence().Delete(this, x => x.Manager_Id == onlineUser.Manager_Id); } //将在线实体保存到数据库的在线表中 OnlineUsersBll.GetInstence().Save(this, onlineUser, null, true, false); } //检查在线列表数据,将不在线人员删除 OnlineUsersBll.GetInstence().CheckOnline(); #endregion #endregion #region 更新用户登陆信息 userinfo.LoginIp = ip; userinfo.LoginCount = ++userinfo.LoginCount; userinfo.LoginTime = localTime; ManagerBll.GetInstence().Save(this, userinfo, string.Format("用户【{0}】登陆成功,更新登陆信息", userinfo.CName)); #endregion #region 添加用户登录成功日志 LoginLogBll.GetInstence().Save(userHashKey, string.Format("账号【{0}】的用户【{1}】登录成功", userinfo.LoginName, userinfo.CName)); #endregion #region 写Cookies //写入用户的HashTable Key CookieHelper.SetCookie(OnlineUsersTable.UserHashKey, userHashKey); //写入加密值 CookieHelper.SetCookie(OnlineUsersTable.Md5, onlineUser.Md5); #endregion //跳转进入主页面 Response.Redirect("Main.aspx"); }
/// <summary> /// 更新IIS缓存中指定Id记录 /// </summary> /// <param name="model">记录实体</param> public void SetModelForCache(OnlineUsers model) { SetModelForCache(Transform(model)); }
/// <summary> /// 将OnlineUsers记录实体(SubSonic实体)转换为普通的实体(DataAccess.Model.OnlineUsers) /// </summary> /// <param name="model">SubSonic插件生成的实体</param> /// <returns>DataAccess.Model.OnlineUsers</returns> public DataAccess.Model.OnlineUsers Transform(OnlineUsers model) { if (model == null) return null; return new DataAccess.Model.OnlineUsers { Id = model.Id, UserHashKey = model.UserHashKey, Manager_Id = model.Manager_Id, Manager_LoginName = model.Manager_LoginName, Manager_LoginPass = model.Manager_LoginPass, Manager_CName = model.Manager_CName, LoginTime = model.LoginTime, LoginIp = model.LoginIp, UserKey = model.UserKey, Md5 = model.Md5, UpdateTime = model.UpdateTime, Sex = model.Sex, Branch_Id = model.Branch_Id, Branch_Code = model.Branch_Code, Branch_Name = model.Branch_Name, Position_Id = model.Position_Id, Position_Name = model.Position_Name, CurrentPage = model.CurrentPage, CurrentPageTitle = model.CurrentPageTitle, SessionId = model.SessionId, UserAgent = model.UserAgent, OperatingSystem = model.OperatingSystem, TerminalType = model.TerminalType, BrowserName = model.BrowserName, BrowserVersion = model.BrowserVersion, }; }
/// <summary> /// 添加与编辑OnlineUsers记录 /// </summary> /// <param name="page">当前页面指针</param> /// <param name="model">OnlineUsers表实体</param> /// <param name="content">更新说明</param> /// <param name="isCache">是否更新缓存</param> /// <param name="isAddUseLog">是否添加用户操作日志</param> public void Save(Page page, OnlineUsers model, string content = null, bool isCache = true, bool isAddUseLog = true) { try { //保存 model.Save(); //判断是否启用缓存 if (CommonBll.IsUseCache() && isCache) { SetModelForCache(model); } if (isAddUseLog) { if (string.IsNullOrEmpty(content)) { content = "{0}" + (model.Id == 0 ? "添加" : "编辑") + "OnlineUsers记录成功,ID为【" + model.Id + "】"; } //添加用户访问记录 UseLogBll.GetInstence().Save(page, content); } } catch (Exception e) { var result = "执行OnlineUsersBll.Save()函数出错!"; //出现异常,保存出错日志信息 CommonBll.WriteLog(result, e); } }
/// <summary> /// 生成加密串——用户加密密钥计算 /// </summary> /// <param name="model">在线实体</param> /// <returns></returns> public string GenerateMd5(OnlineUsers model) { if (model == null) { return RandomHelper.GetRndKey(); } else { return Encrypt.Md5(model.UserKey + model.Manager_LoginName + model.Manager_LoginPass + model.UserKey.Substring(6, 8)); } }
public static void Setup(int testItems) { SetTestRepo(); for(int i=0;i<testItems;i++){ OnlineUsers item=new OnlineUsers(); _testRepo._items.Add(item); } }
public static void Setup(OnlineUsers item) { SetTestRepo(); _testRepo._items.Add(item); }