MVC4URL绕过
我正在创建一个web应用程序,其中有五个步骤。 主页1第2页审核确认。 在url中,它类似于localhost:22112/Home/Page1第2页等等。 我的问题是,如果有人复制localhost:22112/Home/Page2,那么它会跳过所有内容 直接跳到第二页。那么,我怎样才能阻止它呢?我做了以下操作,但没有正常工作。 任何建议都会很有帮助 在控制器中MVC4URL绕过,url,asp.net-mvc-4,Url,Asp.net Mvc 4,我正在创建一个web应用程序,其中有五个步骤。 主页1第2页审核确认。 在url中,它类似于localhost:22112/Home/Page1第2页等等。 我的问题是,如果有人复制localhost:22112/Home/Page2,那么它会跳过所有内容 直接跳到第二页。那么,我怎样才能阻止它呢?我做了以下操作,但没有正常工作。 任何建议都会很有帮助 在控制器中 private bool IsFromIndexPage() { if (Session["IsFromI
private bool IsFromIndexPage()
{
if (Session["IsFromIndex"] != null)
{
return true;
}
else
{
return false;
}
}
对于actionresult的每一页,我都是这样写的
[HttpGet]
public ActionResult Page1()
{
if (!IsFromIndexPage())
{
return RedirectToAction("Index");
}
.....other methods..
}
[HttpPost]
public ActionResult Page1(Information model, string command)
{
if (!IsFromIndexPage())
{
return RedirectToAction("Index");
}
.....other methods..
}
[HttpGet]
public ActionResult Page2()
{
if (!IsFromIndexPage())
{
return RedirectToAction("Index");
}
.....other methods..
}
[HttpPost]
public ActionResult Page2(Information model, string command)
{
if (!IsFromIndexPage())
{
return RedirectToAction("Index");
}
.....other methods..
}
如果使用会话来存储步骤的进度,则应检查会话变量以验证请求是否针对给定页面,否则将用户重定向到第一个/当前完成的页面 您可以为此编写自定义请求处理程序,使会话验证代码与控制器代码分开 有关如何实现基本功能的信息,请参见本文 编辑:
这里有一个稍微不同的方法,关于如何使用ajax使用asp.net MVC创建向导 您的url将在每个步骤上显示为/Home/Wizard。由于使用了AjaxOnly属性,因此无法访问步骤1、步骤2等(请参见底部的AjaxOnly参考) 控制器:
public ActionResult Wizard()
{
return View();
}
[AjaxOnly]
public ActionResult Step1()
{
return PartialView("Step1");
}
[AjaxOnly]
public PartialViewResult Step2(FormCollection coll)
{
Session["FullName"] = coll["FullName"]!= null ? coll["FullName"].ToString() : string.Empty;
return PartialView("Step2");
}
[AjaxOnly]
public PartialViewResult Confirm(FormCollection coll)
{
WizardModel model = new WizardModel() { Name = Session["FullName"].ToString(), Phone = coll["Phone"] != null ? coll["Phone"].ToString() : string.Empty };
return PartialView("Confirm", model);
}
最后一步的模型:
public class WizardModel
{
public string Phone { get; set; }
public string Name { get; set; }
}
确保在页面/布局页面中引用jquery.unobtrusive-ajax
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
Wizard.cshtml
@{
ViewBag.Title = "Wizard";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Wizard - Overview</h2>
@using (Ajax.BeginForm("Step1", new AjaxOptions { HttpMethod="Get", UpdateTargetId = "wizardcontainer" }))
{
<input type="submit" value="Start wizard" />
}
<div id="wizardcontainer"></div>
@{
ViewBag.Title=“向导”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
向导-概述
@使用(Ajax.BeginForm(“Step1”,新的AjaxOptions{HttpMethod=“Get”,UpdateTargetId=“wizardcontainer”}))
{
}
步骤1.cshtml
<div>
<h2>Wizard - Step 1</h2>
<br />
@using(Ajax.BeginForm("Step2", new AjaxOptions { UpdateTargetId = "wizardcontainer" }))
{
@Html.Label("FullName")
@Html.TextBox("FullName")
<input type="submit" value="Next >>" />
}
</div>
向导-步骤1
@使用(Ajax.BeginForm(“Step2”,新的AjaxOptions{UpdateTargetId=“wizardcontainer”}))
{
@Html.Label(“全名”)
@Html.TextBox(“全名”)
}
步骤2.cshtml
<div>
<h2>Wizard - Step 2</h2>
@using(Ajax.BeginForm("Confirm", new AjaxOptions { UpdateTargetId = "wizardcontainer" }))
{
@Html.Label("Phone")
@Html.TextBox("Phone")
@Ajax.ActionLink("<< Previous", "Step1", new AjaxOptions { UpdateTargetId = "wizardcontainer" })
<input type="submit" value="Next >>" />
}
</div>
向导-步骤2
@使用(Ajax.BeginForm(“确认”,新的AjaxOptions{UpdateTargetId=“wizardcontainer”}))
{
@Html.Label(“电话”)
@Html.TextBox(“电话”)
@Ajax.ActionLink(“>”/>
}
Confirm.cshtml
@model MvcApplication2.Controllers.WizardModel
<div>
<h2>Wizard - Final Stage</h2>
Name: @Model.Name
<br />
Phone: @Model.Phone
@Ajax.ActionLink("<< Previous", "Step2", new AjaxOptions { UpdateTargetId = "wizardcontainer" })
</div>
@model mvcapapplication2.Controllers.WizardModel
向导-最后阶段
名称:@Model.Name
电话:@Model.Phone
@Ajax.ActionLink("我正在使用sql server会话并为每个页面(如第1页和第2页)创建会话,在这些页面中我会进行输入。如果可能的话,有什么方法可以完全隐藏url,只显示站点名称并隐藏步骤,这样人们就不会跳转页面??因此,在所有步骤中,始终都是abc.com。我会这样做方法是检查完成的哪个阶段,并为每个步骤编写一个私有方法来返回actionresult,但如果您使用的是强类型模型,并且每个步骤的模型都会发生变化,那么这可能会变得复杂,如果您能设法保持模型不变,那么只需简单地使用此模式即可(currentStep){案例1:返回步骤1(模型)中断;案例2:返回步骤2(模型)中断;默认值:返回新的HttpNotFoundResult();中断;}等等…等等…这个向导教程可能也值得一看。我不会使用会话。我要么检查Request.urlReferer,要么使用cookie存储最后一页。投票反对不选择答案
@model MvcApplication2.Controllers.WizardModel
<div>
<h2>Wizard - Final Stage</h2>
Name: @Model.Name
<br />
Phone: @Model.Phone
@Ajax.ActionLink("<< Previous", "Step2", new AjaxOptions { UpdateTargetId = "wizardcontainer" })
</div>