如何确保对象 ID 未被我的 asp.net mvc Web 应用程序中的攻击者用户修改



我有两个具有一对多关系的对象(LabTest & LabTestDetail)。当我想添加新的LabTestDetail对象时,我将LabTest ID(父 ID)传递给Ajax.actionlink中的LabTestDetail Create action method,如下所示

@Ajax.ActionLink("+ Add New Lab Test Detail",
      "Create", "LabTestDetail",
      new { labtestid = Model.LabTestID },
new AjaxOptions
{
    InsertionMode = InsertionMode.Replace,
    HttpMethod = "Get",
    UpdateTargetId = "replace",
    LoadingElementId = "progress"
})

LabTestDetail Create Action method如下所示; 我将labtestid的值存储在viewbag中的地方:-

[Authorize(Roles = "Doctor")]
public ActionResult Create(int labtestid)
            {
                ViewBag.labtestid = labtestid;
                LabTestDetail ltr = new LabTestDetail();
                return PartialView("_Create", ltr);
            }

然后在create view我将Viewbag的值存储在hidden html field上,如下所示:

<input type= "hidden" name = "labtestid" value = @ViewBag.labtestid />

最后LabTestDetail Post Create action方法如下所示:-

[Authorize(Roles = "Doctor")]    
[HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Create(LabTestDetail ltr, int labtestid)
            {
                try
                {
                    if (ModelState.IsValid)
                    {
                        ltr.LabTestID = labtestid;
                        repository.AddLabTestDetail(ltr);
                        repository.Save();
                        return PartialView("_datails", ltr);
                    }
                }
    //code goes here

当我,,,测试它时,上述内容对我来说效果很好,但我最关心的是如何确保labtestid的值在以下操作期间未被攻击者修改:-

  1. 当我将labtestid值从ajax.action link传递到create action method

  2. 当我将labtestid值作为视图袋传递时

  3. 最后,当我将labtestid值分配给html hidden field时.

此致敬意提前感谢任何帮助。BR

如果使用身份验证并且只有经过身份验证的用户才能执行这些操作,则可以在应该执行更新的操作中验证当前经过身份验证的用户 ( User.Identity.Name ) 是否是实验室测试的所有者。为此,这意味着您需要将这些实验室测试 ID 与用户相关联。

最新更新