asp.net core ActionExecutedContext 获取Request.Body内容

网友投稿 981 2022-11-10

asp- core ActionExecutedContext 获取Request.Body内容

asp- core ActionExecutedContext 获取Request.Body内容

实体类:

public class ApiActionDescriptorModel:ICloneable { public string ActionName { get; set; } public string ControllerName { get; set; } public ApiParameterDescriptorModel[] Parameters { get; set; } public ApiReturnAttributeModel[] apiReturnAttributes { get; set; } public string RequestIp { get; set; } public string HttpMethod { get; set; } public string RequestPort { get; set; } public long ExecutTime { get; set; } public string Operator { get; set; } public DateTime OperaTime { get; set; } public string RequestBody { get; set; } public string QueryString { get; set; } public string ResposeBody { get; set; } public int ResposeStatusCode { get; set; } public string UserId { get; set; } public object Clone() { return new ApiActionDescriptorModel { ActionName = this.ActionName, RequestIp = this.RequestIp, HttpMethod = this.HttpMethod, RequestPort = this.RequestPort, ExecutTime = this.ExecutTime, Parameters = this.Parameters.Select(t => (ApiParameterDescriptorModel)t.Clone()).ToArray() }; } }

过滤器:

public class ExceptionFilter : IExceptionFilter { [Import] private ILoggerHelper loggerHelper { get; set; } public ExceptionFilter(IoC ioc) { ioc.Compose(this); } public void OnException(ExceptionContext context) { if (context.ExceptionHandled == false) { var jsonobj = Dr.Soc.Common.Helpers.JsonResult.Error(context.Exception.Message); context.Result = new JsonResult(jsonobj); } loggerHelper.Error($"全局捕获异常:" + context.Exception.ToString()+"====="+ "入参参数:"+ GetExceptionRequestBody(context)+context?.HttpContext?.Request?.QueryString); context.ExceptionHandled = true; } public Task OnExceptionAsync(ExceptionContext context) { OnException(context); return Task.CompletedTask; } private string GetExceptionRequestBody(ExceptionContext context) { string requestBody = ""; if (context != null) { var request = context?.HttpContext?.Request; if (request != null) { request.Body.Position = 0; StreamReader stream = new StreamReader(request.Body); requestBody = stream.ReadToEnd(); request.Body.Position = 0; } } return requestBody; } }

public class ActionFilter : IActionFilter { private Stopwatch timer; [Import] private IAduitService aduitService { get; set; } public ActionFilter(IoC ioc) { ioc.Compose(this); } public void OnActionExecuting(ActionExecutingContext context) { this.timer = new Stopwatch(); this.timer.Start(); } public void OnActionExecuted(ActionExecutedContext context) { this.timer.Stop(); aduitService.HandleApiAction(GetActionDescriptor(context)); } private ApiActionDescriptorModel GetActionDescriptor(ActionExecutedContext context) { ApiActionDescriptorModel descriptor = new ApiActionDescriptorModel(); var userInfo= ((System.Security.Claims.ClaimsIdentity)context.HttpContext.User.Identity).Claims?.ToList(); if (context != null) { descriptor.ActionName = ((ControllerActionDescriptor)context.ActionDescriptor)?.ActionName; descriptor.ControllerName = ((ControllerActionDescriptor)context.ActionDescriptor)?.ControllerName; descriptor.RequestIp = context.HttpContext.Request.Host.Host.ToString(); descriptor.RequestPort = context.HttpContext.Request.Host.Port.ToString(); descriptor.HttpMethod = context.HttpContext.Request.Method.ToString(); descriptor.ExecutTime = this.timer.ElapsedMilliseconds; descriptor.Operator = userInfo?.Where(t => t.Type == "UserName")?.FirstOrDefault()?.Value; descriptor.UserId = userInfo?.Where(t => t.Type == "UserId")?.FirstOrDefault()?.Value; descriptor.OperaTime = DateTime.Now; descriptor.RequestBody = ReadActionExcutedRequestBody(context); descriptor.QueryString = context.HttpContext.Request?.QueryString.ToString(); descriptor.ResposeBody = GetActionExcutedResponseBody(context); descriptor.ResposeStatusCode = context.HttpContext.Response.StatusCode; } return descriptor; } private string ReadActionExcutedRequestBody(ActionExecutedContext context) { string requestBody = ""; if (context != null) { var request = context.HttpContext.Request; request.Body.Position = 0; StreamReader stream = new StreamReader(request.Body); requestBody = stream.ReadToEnd(); request.Body.Position = 0; } return requestBody; } private string GetActionExcutedResponseBody(ActionExecutedContext context) { string responseBody = ""; if (context.Result != null) { if (context.Result is ObjectResult) responseBody = JsonConvert.SerializeObject(((ObjectResult)context.Result).Value); if (context.Result is JsonResult) responseBody = JsonConvert.SerializeObject(((JsonResult)context.Result).Value); } return responseBody; } }

在startup中注册:

app.Use(next => new RequestDelegate( async context => { context.Request.EnableBuffering(); await next(context); } ));

就可以在请求之后获取到请求体了。

public async Task HandleApiAction(ApiActionDescriptorModel apiActionDescriptorModel) { bool result = false; var cacheAuditRule = auditRuleLoader.GetAllAuditRule(); try { if (cacheAuditRule == null || cacheAuditRule.Count == 0) return result; if (string.IsNullOrEmpty(apiActionDescriptorModel?.RequestBody) || string.IsNullOrEmpty(apiActionDescriptorModel?.ActionName) || string.IsNullOrEmpty(apiActionDescriptorModel?.ControllerName) || string.IsNullOrEmpty(apiActionDescriptorModel?.HttpMethod)) return result; List list = HandleApiAction(apiActionDescriptorModel, cacheAuditRule); if (list == null || list.Count == 0) return result; result = await auditDataAccessor.BatchAddAudit(list.Adapt>()).ConfigureAwait(false); return result; } catch (Exception ex) { loggerHelper.Error(apiActionDescriptorModel.ControllerName + apiActionDescriptorModel.ActionName + "插入审计失败,失败原因:" + ex.ToString()); return result; } } private List HandleApiAction(ApiActionDescriptorModel apiActionDescriptorModel, List auditRuleModels) { int apiEnum = (int)EnumHelper.GetValue(typeof(HttpMethodEnum), apiActionDescriptorModel.HttpMethod?.ToLower()); var auditRuleModel = auditRuleModels.Where(t => t.ControllerName == apiActionDescriptorModel.ControllerName && t.ActionName == apiActionDescriptorModel.ActionName && t.HttpMethod == apiEnum)?.FirstOrDefault(); List list = new List(); if (auditRuleModel == null) return list; if (!string.IsNullOrEmpty(auditRuleModel.AuditRuleDescr)) { var audits = GetAudits(apiActionDescriptorModel, auditRuleModel); if (audits != null && audits.Count > 0) list = GetAudits(apiActionDescriptorModel, auditRuleModel, audits); } return list; } private List GetAudits(ApiActionDescriptorModel apiActionDescriptorModel, AuditRuleModel auditRuleModel, List audits) { List auditModels = new List(); int responseCode = GetResponseCode(apiActionDescriptorModel?.ResposeBody); for (int i = 0; i < audits.Count; i++) { AuditModel auditModel = new AuditModel(); auditModel.UserName = apiActionDescriptorModel.Operator; auditModel.AddTime = apiActionDescriptorModel.OperaTime; auditModel.Ip = apiActionDescriptorModel.RequestIp.Replace("localhost", "127.0.0.1"); auditModel.ResponseCode = responseCode; auditModel.ModelType = auditRuleModel.ModelType; auditModel.Type = EnumHelper.Parse(auditRuleModel.OperateType); auditModel.IncidenceLevel = EnumHelper.Parse(auditRuleModel.IncidenceLevel); auditModel.Detail = audits[i]; auditModel.UserId = apiActionDescriptorModel.UserId; auditModel.UserName = apiActionDescriptorModel.Operator; auditModels.Add(auditModel); } return auditModels; } private List GetAudits(ApiActionDescriptorModel apiActionDescriptorModel, AuditRuleModel auditRuleModel) { var jsonDics = GetKeyValues(apiActionDescriptorModel.RequestBody); var urlDics = GetKeyValues(apiActionDescriptorModel.QueryString); foreach (var item in urlDics) { if (item != null && item.Count > 0) { foreach (var keyValue in item) jsonDics.ForEach(t => t.Add(keyValue.Key, keyValue.Value)); } } List audits = new List(); foreach (var item in jsonDics) { var ruleStr = auditRuleModel.AuditRuleDescr; string[] rules = RegularHelper.FindByRegexRemoveRegexs(ruleStr, "【\\${.*?}】", "[【】\\${}]+"); if (rules.Length > 0) { for (int i = 0; i < rules.Length; i++) { if (!item.Keys.Contains(rules[i])) continue; string value = item[rules[i]]; if (rules[i] == auditRuleModel.AssetIdMappingField) value = assetAccountLoader.GetAssetCacheByCode(value).Name; ruleStr = ruleStr.Replace("${" + rules[i] + "}", value); } } audits.Add(ruleStr); } return audits; } private List> GetKeyValues(string txt) { List> dics = new List>(); if (JsonHelper.StringIsJson(txt)) { string text = RegularHelper.FindFirstByRegex(txt, @"^\[.*?\]$"); List jObjects = string.IsNullOrEmpty(text) ? new List() { JsonConvert.DeserializeObject(txt) } : JsonConvert.DeserializeObject>(txt); foreach (var item in jObjects) dics.Add(item.Properties().ToDictionary(x => x.Name, y => y.Value.ToString())); } else { NameValueCollection collection = HttpUtility.ParseQueryString(txt); var dic = new Dictionary(); foreach (var item in collection.AllKeys) dic.Add(item, collection[item]); dics.Add(dic); } return dics; } private static int GetResponseCode(string resposeBody) { int responseCode = -1; int.TryParse(RegularHelper.FindContentBetweenTwoRegex(resposeBody, "code\":", ","), out responseCode); return responseCode; } private List HandleIsJsonActionToAudit(ApiActionDescriptorModel apiActionDescriptorModel, AuditRuleModel auditRuleModel) { string text = RegularHelper.FindFirstByRegex(apiActionDescriptorModel.RequestBody, @"^\[.*?\]$"); List jObjects = null; if (!string.IsNullOrEmpty(text)) jObjects = JsonConvert.DeserializeObject>(apiActionDescriptorModel.RequestBody); else jObjects = new List() { JsonConvert.DeserializeObject(apiActionDescriptorModel.RequestBody) }; string[] rules = RegularHelper.SplitByRegex(auditRuleModel.AuditRuleDescr, "【.*?】"); List list = null; for (int i = 0; i < jObjects.Count; i++) { list = new List(){ new AuditModel() { UserName=apiActionDescriptorModel.Operator, ModelType=auditRuleModel.ModelType, Type =EnumHelper.Parse(auditRuleModel.OperateType), IncidenceLevel =EnumHelper.Parse(auditRuleModel.IncidenceLevel), UserId=apiActionDescriptorModel.UserId, Detail = GetAuditDetailByRuleRule(jObjects[i], rules).ToString(), AddTime = apiActionDescriptorModel.OperaTime, Ip = apiActionDescriptorModel.RequestIp.Replace("localhost","127.0.0.1"), ResponseCode=GetResponseCode(apiActionDescriptorModel?.ResposeBody), } }; } return list; } private List HandleNotJsonActionToAduit(ApiActionDescriptorModel apiActionDescriptorModel, AuditRuleModel auditRuleModel) { string[] rules = RegularHelper.SplitByRegex(auditRuleModel.AuditRuleDescr, "【.*?】"); List list = null; if (rules.Length >= 2) { list = new List(){ new AuditModel() { UserName=apiActionDescriptorModel.Operator, ModelType=auditRuleModel.ModelType, Type =EnumHelper.Parse(auditRuleModel.OperateType), IncidenceLevel =EnumHelper.Parse(auditRuleModel.IncidenceLevel), Detail = GetAuditDetailByRuleRule(apiActionDescriptorModel.RequestBody,rules).ToString(), AddTime = apiActionDescriptorModel.OperaTime, Ip = apiActionDescriptorModel.RequestIp.Replace("localhost","127.0.0.1"), ResponseCode=GetResponseCode(apiActionDescriptorModel?.ResposeBody), } }; } return list; } private StringBuilder GetAuditDetailByRuleRule(string text, string[] rules) { StringBuilder sb = new StringBuilder(); sb.Append(rules[0]); for (int i = 1; i < rules.Length; i++) sb.Append( RegularHelper.RegexReplace( rules[i], "【.*?】", "【" + RegularHelper.FindContentBetweenTwoRegex( text, "[\\?&]" + RegularHelper.FindContentBetweenTwoRegex(rules[i], @"【\${", "}】") + "=", "(&|$)") + "】" )); return sb; } private StringBuilder GetAuditDetailByRuleRule(JObject jObject, string[] rules) { StringBuilder sb = new StringBuilder(); sb.Append(rules[0]); for (int i = 1; i < rules.Length; i++) { try { sb.Append( RegularHelper.RegexReplace(rules[i], "【.*?】", "【" + jObject[RegularHelper.FindContentBetweenTwoRegex(rules[i], @"【\${", "}】")].ToString() + "】" )); } catch (Exception ex) { loggerHelper.Error("在" + rules[i] + "查找失败,具体原因:" + ex.ToString()); throw; } } return sb; }

public static string[] FindByRegexRemoveRegexs(string html, string strReg, string remReg) { string[] strs = FindByRegex(html, strReg); List result = new List(); if (strs.Length > 0) { for (int i = 0; i < strs.Length; i++) result.Add(RemoveByRegex(strs[i], remReg)); } return result.ToArray(); }

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Asp.NetCore 读取配置文件帮助类
下一篇:Vue脚手架③
相关文章

 发表评论

暂时没有评论,来抢沙发吧~