Hi there I am looking for a solution to solve this problem.I am building an Web app with Asp.net mvc3. I want to make a custom Action Filter ([KeyAuthorization]) so that only authorized key will access the controllers.
This is my default Route
routes.MapRoute(
"Default", // Route name
"{guid}/{controller}/{action}", // URL with parameters
new { guid = UrlParameter.Optional, controller = "Home", action = "Index", }
So let say someone access the www.example.com/34safd-234s/home/index
if key:34safd-234s is correct (authorized) than the user will see the content otherwise the web app will throw an 404.
How can achieve this?
Thank you,
How can I build and action filter that checkes that guid parametre exist in the
You can do it by creating an ActionFilterAttribute
. This should get you going in the right direction. You'll need to do your validation where commented.
public class KeyAuthorizationAttribute : ActionFilterAttribute {
public override void OnActionExecuting(ActionExecutingContext filterContext) {
var guidValue = filterContext.RouteData.Values["guid"];
if (guidValue != null) {
Guid guid = new Guid(guidValue.ToString());
bool notValid = true;
//do validation
//set notValid = false if authorization passes
if (notValid) {
filterContext.Result = new HttpNotFoundResult();
}
}
}
}
You would then apply this to your action.
[KeyAuthorization]
public ActionResult Index() {
}