Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only allow access to action if redirected from specific action

Is there a good way to restrict the access to an action, so you can only access it, if you were redirected from another action. For example:

    [HttpPost]
    public virtual ActionResult Create(MyViewModel vm)
    {            
        if (ModelState.IsValid)
        {
            // do some work

            return RedirectToAction("CreateSuccess");
        }
        else
        {
            return View(vm);
        }
    }


    public virtual ActionResult CreateSuccess()
    {
        // only allow execution if you were redirected from Action "Create" 
    }
like image 760
Fabiano Avatar asked Oct 05 '10 14:10

Fabiano


2 Answers

An easy way would be to store a flag in TempData in the first method and check that the flag exists in the method that is redirected to.
TempData is there to pass state information between action requests and will only last the duration of the request so you will not need to worry about clearing it down.

like image 99
Andy Rose Avatar answered Nov 15 '22 07:11

Andy Rose


There is no way to know the "from" action unless you include parameters indicating such. The easiest way is to append a "SourceAction" or "FromAction" parameter and check it in the "destination" action.

like image 32
Dave Swersky Avatar answered Nov 15 '22 06:11

Dave Swersky