本文介绍了使用MVC中的过滤器进行用户输入验证(OnAuthorization和OnActionExecuting过滤器方法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好, 在MVC中,我们如何访问过滤器中的用户条目(OnAuthorization和OnActionExecuting)方法 。我的要求是 1.需要在我的自定义授权过滤器中验证凭证(表单身份验证) 2.需要做某种对OnActionExecuting过滤器中输入值的验证 谢谢 Naufal。解决方案 public sealed class CustomCheckAttribute:ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { var request = filterContext.HttpContext.Request; var response = filterContext.HttpContext.Response; // 验证在这里 if (请求[ LoginId]!= null ) { ... } } } 和授权试用此 http:// stackoverflow。 com / questions / 9366085 / why-is-onauthorization-execution-before-authentication [ ^ ] 以上解决方案在OnAuthorization上也能正常工作...... 公共类CustomAuthorizationAttribute:FilterAttribute,IAuthorizationFilter { 无效IAuthorizationFilter.OnAuthorization(AuthorizationContext filterContext) { var request = filterContext.HttpContext.Request; string userName = request [txtName]; filterContext.Controller.ViewBag.OnAuthorization =IAuthorizationFilter.OnAuthorization filter called; } } Hi All,In MVC , how do we access user entry in the filters(OnAuthorization and OnActionExecuting) methods. My requirements are1. Need to validate credential(form authentication) in my Custom Authorization filter2. Need to do some kind of validation on the input values in OnActionExecuting filterThanksNaufal. 解决方案 public sealed class CustomCheckAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { var request = filterContext.HttpContext.Request; var response = filterContext.HttpContext.Response; //validation goes here if (request["LoginId"] != null) { ... } } }and for Authorization try this http://stackoverflow.com/questions/9366085/why-is-onauthorization-executing-before-authentication[^]Above solution works fine on OnAuthorization too... public class CustomAuthorizationAttribute : FilterAttribute, IAuthorizationFilter { void IAuthorizationFilter.OnAuthorization(AuthorizationContext filterContext) { var request = filterContext.HttpContext.Request; string userName = request["txtName"]; filterContext.Controller.ViewBag.OnAuthorization = "IAuthorizationFilter.OnAuthorization filter called"; } } 这篇关于使用MVC中的过滤器进行用户输入验证(OnAuthorization和OnActionExecuting过滤器方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-19 12:30