/// <summary> /// 在Action执行之前由 MVC 框架调用。 /// </summary>x /// <param name="context"></param> public override void OnActionExecuting(ActionExecutingContext context) { base.OnActionExecuting(context); //封装对象 Robj <object> obj = new Robj <object>(); _userRoleAppService = context.HttpContext.RequestServices.GetService(typeof(IUserRoleAppService)) as UserRoleAppService; _rolesAppService = context.HttpContext.RequestServices.GetService(typeof(IRoleAppService)) as RoleAppService; var token = string.Empty; var isCookieToken = false; //UserInfo userInfo = new UserInfo { }; var userRoles = new List <UserRoleDto>(); //获取相关信息 var method = context.HttpContext.Request.Method; var isHttps = context.HttpContext.Request.IsHttps; //post方法处理 if (method.ToLower() == "post") { // 使request.body可以读取多次 HttpRequest request = context.HttpContext.Request; request.EnableBuffering(); request.Body.Position = 0; //读取body var reader = new StreamReader(context.HttpContext.Request.Body); var contentFromBody = reader.ReadToEnd(); if (contentFromBody.IsNullOrEmpty() == false) { //序列化 dynamic dyObj = Newtonsoft.Json.JsonConvert.DeserializeObject(contentFromBody); if (dyObj.Token != null || dyObj.token != null) { token = dyObj.Token == null ? dyObj.token.Value : dyObj.Token.Value; } } } //get方法处理 else { var query = context.HttpContext.Request.Query["token"]; if (query.Count > 0) { token = query.ToString(); } } //token为空 取cookie方法 if (token.IsNullOrEmpty() == true) { context.HttpContext.Request.Cookies.TryGetValue("token", out token); isCookieToken = true; } //判断token if (!string.IsNullOrEmpty(token)) { //验证有效性 //var _orgUserAppService = ServiceContainer.GetService<IOrgUserAppService>(); //var result = _orgUserAppService.GetUserInfoByToken(token); //if (result.Result == 0 && result.Data != null) //{ // //用户信息 // userInfo = result.Data; // //非token中获取 // if (!isCookieToken) // { // context.HttpContext.Response.Cookies.Append("token", token, new CookieOptions { Expires = DateTime.Now.AddHours(8) }); // } // //角色 // userRoles = _userRoleAppService.GetUserRoleByUserId(userInfo.ID); // //更新加密token信息 // var userInfoValue = Newtonsoft.Json.JsonConvert.SerializeObject(new Authorization.IdentityKey // { // UserId = userInfo.ID, // UserName = userInfo.Name, // RolesId = userRoles.Select(m => m.Id).ToList() // }); // var decryptValue = DESEncrypt.Encrypt(userInfoValue, token); // //更新密钥 // context.HttpContext.Response.Cookies.Append("identitykey", decryptValue, new CookieOptions { Expires = DateTime.Now.AddHours(8), HttpOnly = true }); //} //else //{ // context.HttpContext.Response.Cookies.Delete("token"); // context.HttpContext.Response.Cookies.Delete("identitykey"); //} } //没有登录 //if (userInfo == null || userInfo.IdentityId == 0) //{ // obj.Error("没有登录", RCode.NeedLogin); // context.Result = new ObjectResult(obj); // return; //} //权限判断 if (this.Permissions != null && Permissions.Length > 0) { //没有角色 if (userRoles == null || userRoles.Count == 0) { obj.Error("没有角色", RCode.NoRole); context.Result = new ObjectResult(obj); return; } //个人权限集合 var listPermission = new List <string>(); foreach (var item in userRoles) { listPermission.AddRange(item.Role.Permissions.Select(m => m.Name).ToList()); listPermission = listPermission.Distinct().ToList(); } bool checkPermission = true; //全部匹配还是单项匹配 if (RequireAllPermissions) { //全部匹配 foreach (var item in Permissions) { if (listPermission.FirstOrDefault(m => m == item) == null) { checkPermission = false; break; } } } else { checkPermission = false; //全部匹配 foreach (var item in Permissions) { if (listPermission.FirstOrDefault(m => m == item) != null) { checkPermission = true; break; } } } if (checkPermission == false) { obj.Error("没有权限", RCode.NoAuthorize); context.Result = new ObjectResult(obj); return; } } }