using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Controllers; using Microsoft.AspNetCore.Mvc.Filters; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Security.Claims; using System.Text.Encodings.Web; using System.Text.Json; using System.Text.Unicode; using Znyc.Admin.AspNetCore.Common; using Znyc.Admin.AspNetCore.Entitys; using Znyc.Admin.AspNetCore.Mvc; using Znyc.Admin.AspNetCore.Mvc.Filter; using Znyc.Admin.Commons.Cache; using Znyc.Admin.Commons.Entitys; using Znyc.Admin.Commons.Extensions; using Znyc.Admin.Commons.Helpers; using Znyc.Admin.Commons.Json; using Znyc.Admin.Commons.Log; using Znyc.Admin.Commons.Pages; using Znyc.Admin.Security.Dtos; namespace Znyc.Admin.AspNetCore.Controllers { /// /// WebApi控制器基类 /// [ApiController] [EnableCors("Cors")] public class ApiController : Controller { /// /// 当前登录的用户属性 /// public AdminCurrentUser CurrentUser; #region /// /// 重写基类在Action执行之前的事情 /// 根据token获得当前用户,允许匿名的不需要获取用户 /// /// 重写方法的参数 public override void OnActionExecuting(ActionExecutingContext context) { try { ControllerActionDescriptor controllerActionDescriptor = context.ActionDescriptor as ControllerActionDescriptor; //匿名访问,不需要token认证、签名和登录 Attribute allowanyone = controllerActionDescriptor.MethodInfo.GetCustomAttribute(typeof(AllowAnonymousAttribute), true); if (allowanyone != null) { return; } CommonResult result = new CommonResult(); //需要token认证 string authHeader = context.HttpContext.Request.Headers["Authorization"];//Header中的token if (string.IsNullOrEmpty(authHeader)) { result.ErrCode = "40004"; result.ErrMsg = ErrCode.err40004; context.Result = ToJsonContent(result); return; } else { string token = string.Empty; if (authHeader != null && authHeader.StartsWith("Bearer ", StringComparison.Ordinal)) { token = authHeader.Substring(7); } TokenProvider tokenProvider = new TokenProvider(); result = tokenProvider.ValidateToken(token); //token验证失败 if (!result.Success) { context.Result = ToJsonContent(result); } else { #region 是否需要验证用户登录以及相关的功能权限 //是否需要用户登录 Attribute isDefined = controllerActionDescriptor.MethodInfo.GetCustomAttribute(typeof(NoPermissionRequiredAttribute)); //不需要登录 if (isDefined != null) { return; } //需要登录和验证功能权限 if (result.ResData != null) { List claimlist = result.ResData as List; string userId = claimlist[3].Value; Claim[] claims = new[] { new Claim(ZnycClaimTypes.UserId,userId), new Claim(ZnycClaimTypes.UserName,claimlist[2].Value), new Claim(ZnycClaimTypes.Role,claimlist[4].Value) }; ClaimsIdentity identity = new ClaimsIdentity(claims); ClaimsPrincipal principal = new ClaimsPrincipal(identity); context.HttpContext.User = principal; CacheHelper yuebonCacheHelper = new CacheHelper(); AdminCurrentUser user = yuebonCacheHelper.Get("login_user_" + userId); if (user != null) { CurrentUser = user; } bool isAdmin = Permission.IsAdmin(user); if (!isAdmin) { IEnumerable authorizeAttributes = controllerActionDescriptor.MethodInfo.GetCustomAttributes(typeof(FunctionAuthorizeAttribute), true).OfType(); if (authorizeAttributes.FirstOrDefault() != null) { string function = authorizeAttributes.First().Function; if (!string.IsNullOrEmpty(function)) { string functionCode = controllerActionDescriptor.ControllerName + "/" + function; bool bl = Permission.HasFunction(functionCode, userId.ToInt()); if (!bl) { result.ErrCode = "40006"; result.ErrMsg = ErrCode.err40006; context.Result = ToJsonContent(result); } } } } return; } else { result.ErrCode = "40008"; result.ErrMsg = ErrCode.err40008; context.Result = ToJsonContent(result); } #endregion 是否需要验证用户登录以及相关的功能权限 } return; } } catch (Exception ex) { Log4NetHelper.Error("", ex); } } #endregion /// /// 把object对象转换为ContentResult /// /// /// [HttpPost] [Route("api/ToJsonContent")] protected IActionResult ToJsonContent(object obj) { return Content(obj.ToJson()); } /// /// 把object对象转换为ContentResult /// /// 转换对象 /// 是否忽略空值 /// [HttpPost] [Route("api/ToJsonContent")] protected IActionResult ToJsonContent(object obj, bool isNull = false) { JsonSerializerOptions options = new JsonSerializerOptions() { WriteIndented = true, //格式化json字符串 AllowTrailingCommas = true, //可以结尾有逗号 IgnoreNullValues = true, //可以有空值,转换json去除空值属性 IgnoreReadOnlyProperties = true, //忽略只读属性 PropertyNameCaseInsensitive = true, //忽略大小写 Encoder = JavaScriptEncoder.Create(UnicodeRanges.All) }; options.Converters.Add(new DateTimeJsonConverter()); return Content(JsonSerializer.Serialize(obj, options)); } /// /// 根据Request参数获取分页对象数据 /// /// protected virtual PagerInfo GetPagerInfo() { int pageSize = Request.Query["length"].ToString() == null ? 1 : Request.Query["length"].ToString().ToInt(); int pageIndex = 1; string currentPage = Request.Query["CurrentPage"].ToString(); if (string.IsNullOrWhiteSpace(currentPage)) { string start = Request.Query["start"].ToString(); if (!string.IsNullOrWhiteSpace(start)) { pageIndex = (start.ToInt() / pageSize) + 1; } } else { pageIndex = currentPage.ToInt(); } PagerInfo pagerInfo = new PagerInfo { CurrenetPageIndex = pageIndex, PageSize = pageSize }; return pagerInfo; } /// /// 获取token /// /// [HttpGet("GetToken")] [HiddenApi] public string GetToken() { string token = HttpContext.Request.Query["Token"]; if (!string.IsNullOrEmpty(token)) { return token; } string authHeader = HttpContext.Request.Headers["Authorization"];//Header中的token if (authHeader != null && authHeader.StartsWith("Bearer")) { token = authHeader.Substring("Bearer ".Length).Trim(); return token; } string cookie = HttpContext.Request.Cookies["Token"]; return cookie == null ? string.Empty : cookie; } } }