I believe you can still access action parameters using the passed in AuthorizationContext
depending on how your attribute is setup, doing something similar to this. I am inheriting from AuthorizeAttribute
but this should work for other types of attributes as long as you can get access to the Request
object.
You could also make the "module"
property name dynamic by passing it into the attribute:
public class CustomAuthenticationAttribute : AuthorizeAttribute {
public string ParamName { get; set; }
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) {
if (!string.IsNullOrEmpty(ParamName)) {
if (filterContext.RequestContext.HttpContext.Request.QueryString[ParamName] != null) { // If using GET
int module = int.Parse(filterContext.RequestContext.HttpContext.Request.QueryString[ParamName]);
// Do something with module
} else if (filterContext.RequestContext.HttpContext.Request.Form[ParamName] != null) { // If using POST (I have not tested Form before myself)
int module = int.Parse(filterContext.RequestContext.HttpContext.Request.Form[ParamName]);
// Do something with module
}
}
}
}
And then you would use it like this:
[CustomAuthentication(ParamName = "module")]
public ActionResult Index(int module) { }