/// <summary> /// 根据用户Id和路径查询按钮集合并且返回(使用redis实现) /// </summary> /// <returns></returns> public async Task <IViewComponentResult> InvokeAsync() { UserSession userSession = JsonNetHelper.DeserializeObject <UserSession>(HttpContext.Session.GetString(KeyUtil.user_info)); List <MenuActionInfo> result = null; if (redisHelp._conn != null) { string key = string.Format(RedisKeyUtil.login_admin_menu, userSession.user_id); if (redisHelp.KeyExists(key)) { result = JsonNetHelper.DeserializeObject <List <MenuActionInfo> >(await redisHelp.StringGetAsync(key)); } else { result = await userService.GetMenuInfo(userSession.user_id); await redisHelp.StringSetAsync(key, JsonNetHelper.SerializeObject(result), new TimeSpan(30, 12, 60)); } } else { result = await userService.GetMenuInfo(userSession.user_id); } return(View("MenuInfo", result)); }
/// <summary> /// 验证登录等信息 /// </summary> /// <param name="context"></param> public override void OnActionExecuting(ActionExecutingContext context) { if (context.HttpContext == null || context.HttpContext.Session == null) { throw new Exception("此特性只适合于Web应用程序使用或者您的服务器Session不可用!"); } //首先读取用户登录的Session信息进行判断 string userJson = context.HttpContext.Session.GetString(KeyUtil.user_info_front); if (!string.IsNullOrEmpty(userJson)) { UserSession userSession = JsonNetHelper.DeserializeObject <UserSession>(userJson); if (string.IsNullOrEmpty(userSession.user_id)) { context.HttpContext.Session.SetString(KeyUtil.user_info, ""); context.Result = new RedirectToActionResult("Index", "Login", null); } else { base.OnActionExecuting(context); } } else { context.Result = new RedirectToActionResult("Index", "Login", null); } }
/// <summary> /// 验证登录等信息 /// </summary> /// <param name="context"></param> public override void OnActionExecuting(ActionExecutingContext context) { if (context.HttpContext == null || context.HttpContext.Session == null) { throw new Exception("此特性只适合于Web应用程序使用或者您的服务器Session不可用!"); } //首先判断用户是否登陆读取到Session,如果没有读取则直接返回登陆页面 string user_json = context.HttpContext.Session.GetString(KeyUtil.user_info); if (!string.IsNullOrEmpty(user_json)) { UserSession userSession = JsonNetHelper.DeserializeObject <UserSession>(user_json); //根据用户Id和请求路劲查询此页面是否可以访问,如果不能访问,则跳转到登录页面 ControllerActionDescriptor action = (ControllerActionDescriptor)context.ActionDescriptor; string actionurl = string.Format("/{0}/{1}/{2}", action.RouteValues["area"], action.RouteValues["controller"], action.RouteValues["action"]).ToLower(); //判断是否含有访问这个字段的权限,如果有,则继续,否则跳转到登录页 if (userSession.action_url == null || !userSession.action_url.Contains(actionurl)) { context.HttpContext.Session.SetString(KeyUtil.user_info, ""); context.Result = new RedirectToActionResult("Index", "Login", null); } else { base.OnActionExecuting(context); } } else { context.Result = new RedirectToActionResult("Index", "Login", null); } }
/// <summary> /// 根据用户Id和路径查询按钮集合并且返回 /// </summary> /// <returns></returns> public async Task <IViewComponentResult> InvokeAsync() { UserSession userSession = JsonNetHelper.DeserializeObject <UserSession>(HttpContext.Session.GetString(KeyUtil.user_info)); string current_url = HttpContext.Request.Path; var result = await userService.getButtionInfo(userSession.user_id, current_url); return(View("ButtionInfo", result)); }
/// <summary> /// 返回用户登录的Session信息 /// </summary> /// <returns></returns> public UserSession GetUserSession() { string userJson = HttpContext.Session.GetString(KeyUtil.user_info); if (string.IsNullOrEmpty(userJson)) { return(null); } UserSession userSession = JsonNetHelper.DeserializeObject <UserSession>(userJson); return(userSession); }
/// <summary> /// 返回登录用户的user_id /// </summary> /// <returns></returns> public string return_front_userid() { string userJson = HttpContext.Session.GetString(KeyUtil.user_info_front); if (string.IsNullOrEmpty(userJson)) { return(""); } UserSession userSession = JsonNetHelper.DeserializeObject <UserSession>(userJson); return(userSession.user_id); }
public async Task <BaseResult <bool> > Login(string login_name_in, string user_pwd_in) { if (string.IsNullOrEmpty(login_name_in) || string.IsNullOrEmpty(user_pwd_in)) { return(new BaseResult <bool>(808, false)); } //这里可以用邮箱和手机号登陆,需要判断使用什么方式登录,查询用户信息之后验证是否可以访问 Expression <Func <UserEntity, bool> > where = LinqUtil.True <UserEntity>(); where = RegexUtil.Email(login_name_in) ? where.AndAlso(c => c.user_email == login_name_in) : where.AndAlso(c => c.user_phone == login_name_in); where = where.AndAlso(c => c.user_pwd == CommonUtil.Md5(user_pwd_in)); UserEntity userEntity = await userRepository.GetAsync(where); if (userEntity == null) { return(new BaseResult <bool>(1000, false)); } if (userEntity.disable == (int)DisableStatus.disable_true) { return(new BaseResult <bool>(1004, false)); } if (userEntity.user_activation == (int)DisableStatus.disable_true) { return(new BaseResult <bool>(1005, false)); } if (userEntity.user_visit == (int)DisableStatus.disable_true) { return(new BaseResult <bool>(1006, false)); } //用户登录正常,修改用户登录时间并且将登录的信息保存到Session中 await userRepository.UpdateAsync(new UserEntity() { user_id = userEntity.user_id, last_time = DateTime.Now }, true, true, c => c.last_time); //处理信息,如果redis连接成功,则直接判断是否存在值,如果存在,则直接使用,否则直接查询并且保存 ,如果连接失败,则直接查询 List <string> buttionActions = null; if (redisHelp._conn != null) { string key = string.Format(RedisKeyUtil.login_admin, userEntity.user_id); if (redisHelp.KeyExists(key)) { buttionActions = JsonNetHelper.DeserializeObject <List <string> >(await redisHelp.StringGetAsync(key)); } else { buttionActions = buttonActionRepository.GetMenuInfo(userEntity.user_id, c => c.action_type != (int)ActionType.front && c.action_url != null && c.disable == (int)DisableStatus.disable_false).Select(c => c.action_url).ToList(); await redisHelp.StringSetAsync(key, JsonNetHelper.SerializeObject(buttionActions), new TimeSpan(30, 12, 60)); } } else { buttionActions = buttonActionRepository.GetMenuInfo(userEntity.user_id, c => c.action_type != (int)ActionType.front && c.action_url != null && c.disable == (int)DisableStatus.disable_false).Select(c => c.action_url).ToList(); } UserSession userSession = new UserSession { user_id = userEntity.user_id, user_name = userEntity.user_name + userEntity.user_code, user_image = userEntity.user_image, full_name = userEntity.full_name, action_url = buttionActions == null ? null : buttionActions }; httpContextUtil.setObjectAsJson(KeyUtil.user_info, userSession); return(new BaseResult <bool>(200, true)); }