views:

32

answers:

2

Folks:

My ASP.NET MVC 1.0 application is more like a workflow.

Means: Parent controller's action method (authentication) --> Child 1 Action method --> Child 2 Action method --> Child n Action

Now once the visitor completes the Authentication through the parent controller's action method he can manupulate the URL and jump directly to the child 2 action method. We want to avoid this and in this case we want them to a error page.

How can we implement to restrict the user from jumpin from 1 to another action method ?

+1  A: 

You could use TempData providing some key and if that value isnt there, you could redirect the user back to the previous step.

Or you could decorate the subsequent action methods with [HttpPost], set each Form action to the next action method in the controller, and the actions wouldnt be available to GET requests.

Jonathan Bates
Thanks many !I was thinking of having more advanced solution. We have many workflow like these. So the work involved would be tremendous.Can we decorate the Action method so that we can achieve the same?
amit
You could use the `HandleErrorAttribute`, i.e. : `[HandleError(View = "YourErrorPageView", ExceptionType = typeof(TriedToGoToStepNotAuthorizedForException))]`.You could also make a custom `FilterAttribute` (or extend `HandleErrorAttribute`) that would allow you to specify whatever criteria you need in order for the method to execute, such as HttpMethods, QueryString key/values, etc.I believe that using attributes this way would allow you the ActionMethod to focus on its own execution, while the `FilterAttribute` can handle deciding if the method should be executed.
Jonathan Bates
Is there a way where we have the Previous visited (controller/action name) something like referrer ?So that I can check in the child action method if the referrer is the Parent or not ! Please advise.
amit
Is there a way where we have the Previous visited (controller/action name) something like referrer ? So that I can check in the child action method if the referrer is the Parent or not ! Please advise if the below ServerVariable can be used without any isses.HttpContext.Request.ServerVariables["HTTP_REFERER"]
amit
You could use that or the convenience method of `Request.UrlReferrer` which wraps the same thing.
Jonathan Bates
Yes..it worked as of now i am using HttpContext.Request.ServerVariables["HTTP_REFERER"]
amit
A: 

Make your child action methods private so that they can only be accessed via the parent action.

[Authorize]
public ActionResult Parent(string color)
{
    if(color=="Red")
        return Child1();
    return Child2();
}

private ActionResult Child1()
{
    return View("this");
}

private ActionResult Child2()
{
    return View("that");
}

~/Controller/Parent routes to Controller.Parent().

~/Controller/Child1 routes to 404: Not Found. ~/Controller/Child2 routes to 404: Not Found.

Byron Sommardahl
Dear Byron..You suggestion may not work:Action method are Public Methods to be accessed over the web.IF you make it private you cannot navigate from any action method through RedirectToAction
amit
Exactly. As long as you parent method is in the same class as your children methods, you can call return ChildMethod().
Byron Sommardahl