C# 实体记录安全

C# 实体记录安全,c#,asp.net-mvc,C#,Asp.net Mvc,它可以正常工作,但当用户通过firebug将“ProductDTO.Property1”字段名更改为“ProductDTO.Property2”时,DTO的Property2设置将作为客户端请求。同时,我并不想知道DTO,但当我将一个实体映射到页面进行编辑时,客户端可以更改db记录 我想用角色保护一些属性。用户不能更改,但管理员可以更改 有这样的解决办法吗 [Secure(Role="Admin")] public string Property2 { get; set; } DTO: 在as

它可以正常工作,但当用户通过firebug将“ProductDTO.Property1”字段名更改为“ProductDTO.Property2”时,DTO的Property2设置将作为客户端请求。同时,我并不想知道DTO,但当我将一个实体映射到页面进行编辑时,客户端可以更改db记录

我想用角色保护一些属性。用户不能更改,但管理员可以更改

有这样的解决办法吗

[Secure(Role="Admin")]
public string Property2 { get; set; }
DTO:

在aspx中:

<%@ Control Language="C#" AutoEventWireup="true" Inherits="System.Web.Mvc.ViewUserControl<CmTest.Web.Controllers.ProductController.ProductFormViewModel>" %>

<% using (Html.BeginForm()) { %>
<%= Html.AntiForgeryToken() %>
<label for="Product_Property1">Property1:</label>
<div>
    <%= Html.TextBox("ProductDTO.Property1", (ViewData.Model.ProductDTO != null) ? ViewData.Model.ProductDTO.Property1 : "")%>
</div>
<% } %>

我很难理解您的要求,但如果您担心批量转让,您可以将Property2排除在约束范围之外:

public ActionResult Edit([Bind(Exclude = "Property2")]ProductDTO productDTO)

或者更好地使用Include来创建可绑定属性的白名单。

感谢您的反馈,它看起来是正确的,但当我更改属性名称时,编译器不会抛出错误。再次感谢。不,编译器不会抛出错误,它将在运行时从绑定中排除该属性,即使有人调整HTTP请求并尝试设置值,该属性将始终具有其默认值。谢谢,我想只有此解决方案才能修复安全漏洞。
[Transaction]
public ActionResult Edit(int id)
{
    ProductFormViewModel viewModel = ProductFormViewModel.CreateProductFormViewModel();
    viewModel.ProductDTO = productRepository.GetDTO(id);

    return View(viewModel);
}

[ValidateAntiForgeryToken]
[Transaction]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(ProductDTO productDTO)
{
    //debugging
}

public class ProductFormViewModel
{
    private ProductFormViewModel() { }

    public static ProductFormViewModel CreateProductFormViewModel()
    {
        ProductFormViewModel viewModel = new ProductFormViewModel();

        return viewModel;
    }

    public ProductDTO ProductDTO { get; internal set; }
}
public ActionResult Edit([Bind(Exclude = "Property2")]ProductDTO productDTO)