Click here to Skip to main content
15,889,505 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

In MVC , how do we access user entry in the filters(OnAuthorization and OnActionExecuting) methods

. My requirements are

1. Need to validate credential(form authentication) in my Custom Authorization filter
2. Need to do some kind of validation on the input values in OnActionExecuting filter

Thanks
Naufal.
Posted
Updated 28-Jan-14 22:07pm
v2

C#
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[^]
 
Share this answer
 
v2
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";
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900