public UserLoginResponse Login(String username, String password, HttpRequest request) { try { VPBLL.Users.UsersHelper helper = new VPBLL.Users.UsersHelper(); List<VPBLL.Users.ABiAdminUsers> list = helper.getUsers(username, password); if (list != null && list.Count > 0 && list[0].Status) { VPBLL.Users.ABiAdminUsers user = list[0]; if (user.LoginStatus == "1" && user.LastLogin >= DateTime.Now.AddMinutes(-15) && user.IPAddress != request.UserHostAddress) { return new UserLoginResponse() { ResponseCode = (int)APIResponseCode.Login_Account_Is_Logining, ResponseMessage = "该帐号处于登陆状态", UserID = 0 }; } else { VPBLL.Roles.AdminUsersRolesHelper roleHelper = new VPBLL.Roles.AdminUsersRolesHelper(); List<VPBLL.Roles.ABiAdminUsersRoles> roleList = roleHelper.getUsersRolesByUserID(user.ABi_Admin_Users_ID); if (roleList != null && roleList.Count > 0) { user.LoginStatus = "1"; user.LastLogin = DateTime.Now; user.IPAddress = request.UserHostAddress; helper.updateUsersLastLogin(user); return new UserLoginResponse() { ResponseCode = (int)APIResponseCode.Success, ResponseMessage = "Success", UserID = user.ABi_Admin_Users_ID, RoleID = roleList[0].ABi_Admin_Roles_ID }; } } } } catch (Exception ex) { return new UserLoginResponse() { ResponseCode = (int)APIResponseCode.System_Error, ResponseMessage = ex.Message, UserID = 0 }; } return new UserLoginResponse() { ResponseCode = (int)APIResponseCode.Login_Failed, ResponseMessage = "登录失败", UserID = 0 }; }
public void GetUserRoleTest() { UsersHelper target = new UsersHelper(); // TODO: Initialize to an appropriate value int userId = 1513; // TODO: Initialize to an appropriate value ABiRole expected = new ABiRole() { RoleID = 5, RoleName = "PTP管理员" }; // TODO: Initialize to an appropriate value ABiRole actual; actual = target.GetUserRole(userId); Assert.AreEqual(expected.RoleID, actual.RoleID); Assert.AreEqual(expected.RoleName, actual.RoleName); expected = new ABiRole() { RoleID = 1, RoleName = "管理员" }; actual = target.GetUserRole(1); Assert.AreEqual(expected.RoleID, actual.RoleID); Assert.AreEqual(expected.RoleName, actual.RoleName); expected = new ABiRole() { RoleID = 4, RoleName = "供应商" }; actual = target.GetUserRole(2); Assert.AreEqual(expected.RoleID, actual.RoleID); Assert.AreEqual(expected.RoleName, actual.RoleName); expected = new ABiRole() { RoleID = 3, RoleName = "采购员" }; actual = target.GetUserRole(7); Assert.AreEqual(expected.RoleID, actual.RoleID); Assert.AreEqual(expected.RoleName, actual.RoleName); expected = new ABiRole() { RoleID = 2, RoleName = "采购经理" }; actual = target.GetUserRole(27); Assert.AreEqual(expected.RoleID, actual.RoleID); Assert.AreEqual(expected.RoleName, actual.RoleName); }
public BaseAPIResponse CheckLoginStatus(int userId, string ip) { var result = new BaseAPIResponse(); try { UsersHelper help = new UsersHelper(); List<ABiAdminUsers> list = help.getUsersByID(userId); if (list != null && list.Count > 0) { ABiAdminUsers obj = list[0]; if (!obj.Status) { result.ResponseCode = (int)APIResponseCode.User_Locked; result.ResponseMessage = "用户被锁或用户不存在"; } else if (obj.LoginStatus != "1" || obj.LastLogin < DateTime.Now.AddMinutes(-15)) { result.ResponseCode = (int)APIResponseCode.Login_Expired; result.ResponseMessage = "登录过期,请重新登录"; } else { obj.LastLogin = DateTime.Now; obj.LoginStatus = "1"; obj.IPAddress = ip;//Request.UserHostAddress; help.updateUsersLastLogin(obj); result.ResponseCode = (int)APIResponseCode.Success; result.ResponseMessage = "Success"; } } } catch (Exception ex) { result.ResponseCode = (int)APIResponseCode.System_Error; result.ResponseMessage = ex.Message; } return result; }
/// <summary> /// 验证登陆信息,单点登陆及防止非法登陆 /// </summary> public void CheckUser(HttpCookie cookie) { UsersHelper help=new UsersHelper(); List<ABiAdminUsers> list = help.getUsersByID(this.UserID); if (list != null && list.Count > 0) { ABiAdminUsers obj = list[0]; if (obj.LastLogin<DateTime.Now.AddMinutes(-15)) this.GoLogin("登陆已失效,您被强制退出"); else { obj.LastLogin = DateTime.Now; obj.LoginStatus = "1"; obj.IPAddress = Request.UserHostAddress; help.updateUsersLastLogin(obj); cookie.Value = obj.LastLogin.ToString(); Response.AppendCookie(cookie); } } else this.GoLogin(String.Format("登陆已失效,请重新登陆")); }