public void OnActionExecuting(ActionExecutingContext context) { //验证数据 if (!context.ModelState.IsValid) { var modelState = context.ModelState.FirstOrDefault(f => f.Value.Errors.Any()); string errorMsg = modelState.Value.Errors.First().ErrorMessage; throw new Exception(errorMsg); } //日志 var controller = context.Controller as ControllerBase; logmodel.Id = Guid.NewGuid().ToString(); logmodel.Namespace = controller.GetType().Namespace; logmodel.ClassName = controller.GetType().Name; logmodel.MethodName = controller.HttpContext.Request.GetAbsoluteUri();//string.Join("/", controller.RouteData.Values.Values); logmodel.Parameter = JsonConvert.SerializeObject(controller.HttpContext.Request.Query); logmodel.LogType = "1"; logmodel.Ip = controller.HttpContext.Connection.RemoteIpAddress?.ToString(); logmodel.Source = "web"; var stopwach = new Stopwatch(); stopwach.Start(); context.HttpContext.Items.Add("StopwachKey", stopwach); //授权验证 var keyValuePairs = new List <KeyValuePair <string, object> >(); controller.HttpContext.Request.Query?.ToList().ForEach( p => { keyValuePairs.Add(new KeyValuePair <string, object>(p.Key, p.Value)); }); context.ActionArguments.ToList().ForEach(p => { if (!p.Value.GetType().Name.Equals("FormFile")) { keyValuePairs.Add(new KeyValuePair <string, object>(p.Key, p.Value)); } }); var ss = SignCreator.CreateSign(keyValuePairs[3].Value); }
public void OnActionExecuting(ActionExecutingContext context) { //验证数据 if (!context.ModelState.IsValid) { var modelState = context.ModelState.FirstOrDefault(f => f.Value.Errors.Any()); string errorMsg = modelState.Value.Errors.First().ErrorMessage; context.Result = new ApplicationErrorResult(errorMsg); context.HttpContext.Response.StatusCode = (int)ResultCode.ParamsValidateFail; return; //throw new Exception(errorMsg); } //日志 var controller = context.Controller as ControllerBase; logmodel.Id = Guid.NewGuid().ToString(); logmodel.Namespace = controller.GetType().Namespace; logmodel.ClassName = controller.GetType().Name; logmodel.MethodName = controller.HttpContext.Request.GetAbsoluteUri();//string.Join("/", controller.RouteData.Values.Values); logmodel.Parameter = JsonConvert.SerializeObject(controller.HttpContext.Request.Query); logmodel.LogType = "1"; logmodel.Ip = controller.HttpContext.Connection.RemoteIpAddress?.ToString(); logmodel.Source = "web"; var stopwach = new Stopwatch(); stopwach.Start(); context.HttpContext.Items.Add("StopwachKey", stopwach); if (Boolean.Parse(_configuration.GetSection("ApiAccessSettings:IsEnabled").Value)) { //授权验证 var keyValuePairs = new List <KeyValuePair <string, object> >(); //controller.HttpContext.Request.Query?.ToList().ForEach( // p => // { // keyValuePairs.Add(new KeyValuePair<string, object>(p.Key, p.Value)); // }); context.ActionArguments.ToList().ForEach(p => { if (!p.Value.GetType().Name.Equals("FormFile")) { keyValuePairs.Add(new KeyValuePair <string, object>(p.Key, p.Value)); } }); if (keyValuePairs.Any()) { var paramModel = keyValuePairs[0].Value as BaseInput; var signKey = SignCreator.CreateSign(keyValuePairs[0].Value, _configuration.GetSection("ApiAccessSettings:Key").Value); if (!paramModel.Sign.Equals(signKey)) { context.Result = new ApplicationErrorResult(ResultCode.SignValidateFail.GetDescription()); context.HttpContext.Response.StatusCode = (int)ResultCode.SignValidateFail; return; } } else { context.Result = new ApplicationErrorResult(ResultCode.ParamsValidateFail.GetDescription()); context.HttpContext.Response.StatusCode = (int)ResultCode.ParamsValidateFail; return; } } }