Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

c# - 防止用户直接调用操作方法(Prevent the user from calling action method directly)

I have 3 View like this:

(我有3个这样的View :)

  public ActionResult Index()
    {
         return View();
    }

    public ActionResult Step2()
    {

         return View();
    }
    public ActionResult Step3()
    {
         return View();
    }        

And 3 HttpPost Actions

(和3个HttpPost动作)

    //Step 1
    [HttpPost]
    [ValidateAntiForgeryToken]
    public JsonResult Index(string number){}
    //Step 2
    [HttpPost]
    [ValidateAntiForgeryToken]
    public JsonResult Step2(string number){}
    //Step 3
    [HttpPost]
    [ValidateAntiForgeryToken]
    public JsonResult Step3(string number){}

For each HttpPost Action Method I have created a HTML Form and I want that the user submits each form step by step ( step 1 -> step 2 -> step 3 )

(我为每个HttpPost Action Method创建了一个HTML Form ,我希望用户逐步提交每个表单( step 1 > step 2 > step 3 ))

Everything is OK but I do not want users can go to redirect domain/controller/step2 or domain/controller/step3 .

(一切正常,但是我不希望用户可以重定向domain/controller/step2domain/controller/step3 。)

I mean, user must follow my router step 1 -> step 2 -> step3

(我的意思是,用户必须按照我的路由器step 1 - > step 2 - > step3)

  ask by Alex translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are ways to solve your issue.

(有多种方法可以解决您的问题。)

One way to achieve this is by using TempData before your redirect command and check the TempData value in your HttpGet Action Method .

(实现此目的的一种方法是在重定向命令之前使用TempData并检查HttpGet Action MethodTempData值。)

For example for your Step 2 check you can do this:

(例如,对于您的第2步检查,您可以执行以下操作:)

[HttpPost]
    [ValidateAntiForgeryToken]
    public JsonResult Index(string number)
    {
        //your business code 

        TempData["FirstStepDone"] = true;

        // return RedirectTo()
    }

    public ActionResult Step2()
    {
        if (TempData["FirstStepDone"] == null)
            //return error

        return View();
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...